Guest User

Untitled

a guest
Jan 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. /**
  2. * Implements hook_form_alter().
  3. *
  4. * On Work node add and edit, set only selected category's associated
  5. * vocabularies as visible.
  6. */
  7. function mass_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  8. if ($form_id != 'node_work_form' && $form_id != 'node_work_edit_form') {
  9. return;
  10. }
  11. if (isset($form['field_category'])) {
  12. $states_when_category_is_design = array(
  13. 'visible' => array(
  14. ':input[name="field_category"]' => array('value' => '4'),
  15. ),
  16. );
  17.  
  18. $states_when_category_is_advocacy = array(
  19. 'visible' => array(
  20. ':input[name="field_category"]' => array('value' => '19'),
  21. ),
  22. );
  23.  
  24. $states_when_category_is_research = array(
  25. 'visible' => array(
  26. ':input[name="field_category"]' => array('value' => '25'),
  27. ),
  28. );
  29.  
  30. if (isset($form['field_design_type'])) {
  31. $form['field_design_type']['#states'] = $states_when_category_is_design;
  32. }
  33.  
  34. if (isset($form['field_design_location'])) {
  35. $form['field_design_location']['#states'] = $states_when_category_is_design;
  36. };
  37.  
  38. if (isset($form['field_design_discipline'])) {
  39. $form['field_design_discipline']['#states'] = $states_when_category_is_design;
  40. };
  41.  
  42. if (isset($form['field_advocacy_type'])) {
  43. $form['field_advocacy_type']['#states'] = $states_when_category_is_advocacy;
  44. };
  45.  
  46. if (isset($form['field_research_type'])) {
  47. $form['field_research_type']['#states'] = $states_when_category_is_research;
  48. };
  49. }
  50. }
  51.  
  52. /**
  53. * Implements hook_form_alter().
  54. */
  55. function MYMODULE_form_alter(&$form, DrupalCoreFormFormStateInterface &$form_state, $form_id) {
  56. if ($form_id == 'node_CONTENT_TYPE_form' || $form_id == 'node_CONTENT_TYPE_edit_form') {
  57. conditional_field_select(
  58. $form,
  59. 'field_target',
  60. 'field_controller',
  61. ['value_a', 'value_b', 'value_c'],
  62. 'visible'
  63. );
  64. }
  65. }
  66.  
  67. function conditional_field_select(array &$form, $targetField, $controlledBy, array $values, $state = 'invisible', $cond = 'or') {
  68. if (isset($form[$targetField]) && isset($form[$controlledBy])) {
  69. $form[$targetField]['#states'][$state] = [];
  70. foreach ($values as $value) {
  71. array_push($form[$targetField]['#states'][$state], ['select[name=' . $controlledBy . ']' => ['value' => $value]]);
  72. if (end($values) !== $value) {
  73. array_push($form[$targetField]['#states'][$state], $cond);
  74. }
  75. }
  76. }
  77. }
  78.  
  79. array_push($form[$targetField]['#states'][$state], [':input[name=' . $controlledBy . ']' => ['value' => $value]]);
Add Comment
Please, Sign In to add comment