Guest User

Untitled

a guest
Jan 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. use DrupalCoreFormFormStateInterface;
  2.  
  3. function mymodule_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  4.  
  5. if ($form_id == 'node_page_form' || $form_id == 'node_page_edit_form') {
  6. return;
  7. }
  8.  
  9. if (isset($form['field_test2'])) {
  10. $states_when_category_is_design = array(
  11. 'visible' => array(
  12. ':input[name="field_test2"]' => array('value' => '15'),
  13. ),
  14. );
  15.  
  16. if (isset($form['field_test3'])) {
  17. $form['field_test3']['#states'] = $states_when_category_is_design;
  18. };
  19. }
  20. }
  21.  
  22. /**
  23. * Implements hook_form_alter().
  24. */
  25. function mymodule_form_alter(&$form, &$form_state, $form_id) {
  26. if($form_id == 'node_article_edit_form') {
  27. $form['field_other']['#states'] = [
  28. 'visible' => [
  29. 'select[name="field_reason"]' => ['value' => 'other']
  30. ]
  31. ];
  32. }
  33. }
  34.  
  35. /**
  36. * Implements hook_form_alter().
  37. */
  38. function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  39. if ($form_id == 'node_CONTENT_TYPE_form' || $form_id == 'node_CONTENT_TYPE_edit_form') {
  40. conditional_field_select(
  41. $form,
  42. 'field_target',
  43. 'field_controller',
  44. ['value_a', 'value_b', 'value_c'],
  45. 'visible'
  46. );
  47. }
  48. }
  49.  
  50. function conditional_field_select(array &$form, $targetField, $controlledBy, array $values, $state = 'invisible', $cond = 'or') {
  51. if (isset($form[$targetField]) && isset($form[$controlledBy])) {
  52. $form[$targetField]['#states'][$state] = [];
  53. foreach ($values as $value) {
  54. array_push($form[$targetField]['#states'][$state], ['select[name=' . $controlledBy . ']' => ['value' => $value]]);
  55. if (end($values) !== $value) {
  56. array_push($form[$targetField]['#states'][$state], $cond);
  57. }
  58. }
  59. }
  60. }
  61.  
  62. array_push($form[$targetField]['#states'][$state], [':input[name=' . $controlledBy . ']' => ['value' => $value]]);
Add Comment
Please, Sign In to add comment