我想在drupal 8中将省和城市字段添加到用户实体.通过更改省份,应更新城市列表.在drupal 7中我用条件字段模块完成了这个,但是这个模块还没有为drupal 8做好准备.在drupal 8中这样做的正确方法是什么?我应该添加字段,然后将jquery添加到我的注册模板中,或者是否有更好的标准方法来执行此操作.谢谢.
使用一点(PHP)代码,可以使用Drupal的状态处理来明确说明哪些字段应该显示或隐藏给定哪些条件.
例如,当在类别字段中选择设计(术语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,也就是我的同事和Agaric的同事兼所有者dinarcon无耻地偷走的.)