【问题标题】:Disable a Field in a Paragraphs Form禁用段落表单中的字段
【发布时间】:2018-01-01 09:10:00
【问题描述】:

有人知道如何在 Drupal 8 中更改段落 (ajax) 后端表单中的字段吗?我想禁用一个字段,但让它保持可见。

谢谢

【问题讨论】:

    标签: drupal drupal-8 paragraphs


    【解决方案1】:

    您可以使用 hook_form_alter() 或 hook_form_FORM_ID_alter() 禁用表单字段。

    我总是建议你使用 hook_form_FORM_ID_alter()。假设 test 是您的模块名称,而 user_register_form 是表单的 ID。

    test_form_user_register_form_alter(&$form, &$form_state, $form_id) {
      $form['fieldname'] = array(
        '#type' => 'textfield',
        '#title' => t('Text label'),
        '#attributes' => array('disabled' => 'disabled'),
      );
    }
    

    编码愉快!!!

    【讨论】:

    • 有一个补丁添加了段落子表单钩子更改,以便更轻松地定位特定段落类型中的特定字段。查看drupal.org/i/2868155
    【解决方案2】:
    function hook__form_FORM_ID_alter(&$form,\Drupal\Core\Form\FormStateInterface 
    $form_state, $form_id) {
    //output your form structure to know what to target in the form array($form[])
    #kint( $form['title']);
    $form['title']['#disabled'] = TRUE;
    
    }
    

    以上代码禁用了您要修改的“FORM_ID”中的标题字段(Drupal 8.5)。

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 2010-11-26
      • 2011-02-13
      • 1970-01-01
      • 2011-10-09
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多