Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use DrupalCoreFormFormStateInterface;
- function mymodule_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
- if ($form_id == 'node_page_form' || $form_id == 'node_page_edit_form') {
- return;
- }
- if (isset($form['field_test2'])) {
- $states_when_category_is_design = array(
- 'visible' => array(
- ':input[name="field_test2"]' => array('value' => '15'),
- ),
- );
- if (isset($form['field_test3'])) {
- $form['field_test3']['#states'] = $states_when_category_is_design;
- };
- }
- }
- /**
- * Implements hook_form_alter().
- */
- function mymodule_form_alter(&$form, &$form_state, $form_id) {
- if($form_id == 'node_article_edit_form') {
- $form['field_other']['#states'] = [
- 'visible' => [
- 'select[name="field_reason"]' => ['value' => 'other']
- ]
- ];
- }
- }
- /**
- * Implements hook_form_alter().
- */
- function MYMODULE_form_alter(&$form, &$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