Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements hook_form_alter().
- *
- * On Work node add and edit, set only selected category's associated
- * vocabularies as visible.
- */
- function mass_form_alter(&$form, DrupalCoreFormFormStateInterface $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;
- };
- }
- }
- /**
- * Implements hook_form_alter().
- */
- function MYMODULE_form_alter(&$form, DrupalCoreFormFormStateInterface &$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]]);
Add Comment
Please, Sign In to add comment