2016-02-13 67 views
0

我想在drupal 8中向用戶實體添加省和城市字段。通過更改省份,城市列表應該更新。在drupal 7中,我已經使用條件字段模塊完成了這項工作,但是該模塊尚未準備好用於drupal 8。 在drupal 8中做到這一點的正確方法是什麼? 我應該添加字段,然後將jquery添加到我的註冊模板中來執行此操作,還是有更好和標準的方法來執行此操作。 謝謝。drupal 8條件字段

回答

6

使用一點(PHP)代碼,可以使用Drupal's states processing明確說明在給定哪些條件時應該顯示或隱藏哪些字段。

例如,這示出了設計類型,設計位置和設計學科領域當在類別字段中選擇設計(術語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; 
    }; 
    } 
} 

(此代碼無恥地從毛被盜Dinarte aka dinarcon,我的同事和Agaric的同事。)

+0

重要更新到自身一無所知的形式狀態代碼,這是固體,但,如果你想要這個出現在這兩個節點添加(創建內容)形式以及編輯表單,您需要在Drupal 8中使用兩種不同的形式改變鉤子:node_NODETYPE_form和node_NODETYPE_edit_form。 (答案的代碼已更新以反映此情況。) – mlncn

+1

mass_form_alter「mass」是您的管理主題? – TikaL13

+1

@ TikaL13好問題!這會起作用,但我們不想要自定義管理主題,所以在這種情況下,我們將代碼放入我們的安裝配置文件中,這稱爲「質量」。它也可以在自定義模塊中工作,但正如你的問題明確指出的那樣,如果在管理主題中顯示編輯表單,它將無法在主要主題中起作用! – mlncn

0

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

由於early adopter,你可以考慮按照this issue並最終與移植支持社區Drupal的8

今天,我親自去使用Drupal 8只有當所有 - 或大部分 - 功能我需要的是支持通過穩定的contrib模塊。否則,我會使用Drupal 7.

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]]);