drupal 8条件字段

时间:2016-02-13 13:48:07

标签: jquery drupal custom-lists drupal-8

我想在drupal 8中将省和城市字段添加到用户实体。通过更改省份,应更新城市列表。在drupal 7中我用条件字段模块完成了这个,但是这个模块还没有为drupal 8做好准备。 在drupal 8中这样做的正确方法是什么? 我应该添加字段,然后将jquery添加到我的注册模板中,或者是否有更好的标准方法来执行此操作。 谢谢。

3 个答案:

答案 0 :(得分:7)

使用一些(PHP)代码,可以使用Drupal's states processing明确说明哪些字段应该显示或隐藏给定哪些条件。

例如,当在类别字段中选择Design(术语ID 4)等时,它显示了设计类型,设计位置和设计学科字段:

/**
 * Implements hook_form_alter().
 *
 * On Work node add and edit, set only selected category's associated
 * vocabularies as visible.
 */
function mass_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id != 'node_work_form' && $form_id != 'node_work_edit_form') {
    return;
  }
  if (isset($form['field_category'])) {
    $states_when_category_is_design = array(
      'visible' => array(
        ':input[name="field_category"]' => array('value' => '4'),
      ),
    );

    $states_when_category_is_advocacy = array(
      'visible' => array(
        ':input[name="field_category"]' => array('value' => '19'),
      ),
    );

    $states_when_category_is_research = array(
      'visible' => array(
        ':input[name="field_category"]' => array('value' => '25'),
      ),
    );

    if (isset($form['field_design_type'])) {
      $form['field_design_type']['#states'] = $states_when_category_is_design;
    }

    if (isset($form['field_design_location'])) {
      $form['field_design_location']['#states'] = $states_when_category_is_design;
    };

    if (isset($form['field_design_discipline'])) {
      $form['field_design_discipline']['#states'] = $states_when_category_is_design;
    };

    if (isset($form['field_advocacy_type'])) {
      $form['field_advocacy_type']['#states'] = $states_when_category_is_advocacy;
    };

    if (isset($form['field_research_type'])) {
      $form['field_research_type']['#states'] = $states_when_category_is_research;
    };
  }
}

(这段代码是从Mauricio Dinarte,又名dinarcon,我的同事和Agaric的同事所有者那里无耻地偷走的。)

答案 1 :(得分:0)

Drupal提供了Conditional fields模块所需的功能。

作为early adopter,您可以考虑关注this issue并最终通过移植到Drupal 8来支持社区。

今天,只有当我需要的全部或大部分功能都得到稳定的contrib模块支持时,我个人才会使用Drupal 8。否则,我选择Drupal 7。

答案 2 :(得分:0)

我用它在下拉列表选择列表中显示或隐藏具有依赖关系的字段。

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
  if ($form_id == 'node_CONTENT_TYPE_form' || $form_id == 'node_CONTENT_TYPE_edit_form') {
    conditional_field_select(
      $form,
      'field_target',
      'field_controller',
      ['value_a', 'value_b', 'value_c'],
      'visible'
    );
  }
}

function conditional_field_select(array &$form, $targetField, $controlledBy, array $values, $state = 'invisible', $cond = 'or') {
  if (isset($form[$targetField]) && isset($form[$controlledBy])) {
    $form[$targetField]['#states'][$state] = [];
    foreach ($values as $value) {
      array_push($form[$targetField]['#states'][$state], ['select[name=' . $controlledBy . ']' => ['value' => $value]]);
      if (end($values) !== $value) {
        array_push($form[$targetField]['#states'][$state], $cond);
      }
    }
  }
}

可以轻松更改为输入。

array_push($form[$targetField]['#states'][$state], [':input[name=' . $controlledBy . ']' => ['value' => $value]]);    
相关问题