You are here

public function ConditionalFieldEditForm::buildEditContextSettings in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/Form/ConditionalFieldEditForm.php \Drupal\conditional_fields\Form\ConditionalFieldEditForm::buildEditContextSettings()

Builds Edit Context Settings block.

1 call to ConditionalFieldEditForm::buildEditContextSettings()
ConditionalFieldEditForm::buildForm in src/Form/ConditionalFieldEditForm.php
Form constructor.

File

src/Form/ConditionalFieldEditForm.php, line 387

Class

ConditionalFieldEditForm
Class ConditionalFieldEditForm.

Namespace

Drupal\conditional_fields\Form

Code

public function buildEditContextSettings(array $form, FormStateInterface $form_state, $condition) {
  $label = array_key_exists('dependee', $condition) ? $condition['dependee'] : '?';
  $settings = array_key_exists('settings', $condition) ? $condition['settings'] : [];
  $form['state'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Form state'),
    '#description' => $this
      ->t('The Javascript form state that is applied to the target field when the condition is met. Note: this has no effect on server-side logic and validation.'),
    '#options' => $this->list
      ->conditionalFieldsStates(),
    '#default_value' => array_key_exists('state', $settings) ? $settings['state'] : 0,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::ajaxAdminStateCallback',
      'wrapper' => 'effects-wrapper',
    ],
  ];
  $effects = $effects_options = [];
  $selected_state = $form_state
    ->getValue('state') ?: $condition['settings']['state'];
  foreach ($this->list
    ->conditionalFieldsEffects() as $effect_name => $effect) {
    if (empty($selected_state)) {
      continue;
    }
    if (in_array($selected_state, $effect['states'])) {
      $effects[$effect_name] = $effect['label'];
      if (isset($effect['options'])) {
        $effects_options[$effect_name] = $effect['options'];
      }
    }
  }
  $form['effects_wrapper'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'effects-wrapper',
    ],
  ];
  $effect = array_key_exists('effect', $settings) ? $settings['effect'] : '';
  $effect = $form_state
    ->hasValue('effect') ? $form_state
    ->getValue('effect') : $effect;
  if (count($effects) == 1) {
    $effects_keys = array_keys($effects);
    $form['effects_wrapper']['effect'] = [
      '#type' => 'hidden',
      '#value' => array_shift($effects_keys),
      '#default_value' => array_shift($effects_keys),
    ];
  }
  elseif (count($effects) > 1) {
    $form['effects_wrapper']['effect'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Effect'),
      '#description' => $this
        ->t('The effect that is applied to the target field when its state is changed.'),
      '#options' => $effects,
      '#default_value' => $effect,
      '#states' => [
        'visible' => [
          ':input[name="state"]' => [
            [
              'value' => 'visible',
            ],
            [
              'value' => '!visible',
            ],
          ],
        ],
      ],
    ];
  }
  $form['effects_wrapper']['effect_options'] = [
    '#tree' => TRUE,
  ];
  foreach ($effects_options as $effect_name => $effect_options) {
    foreach ($effect_options as $effect_option_name => $effect_option) {
      $effect_option += [
        '#title' => $this
          ->t('@effect effect option: @effect_option', [
          '@effect' => $effects[$effect_name],
          '@effect_option' => $effect_option_name,
        ]),
        '#states' => [
          'visible' => [
            ':input[name="effect"]' => [
              [
                'value' => $effect_name,
              ],
            ],
          ],
        ],
      ];
      if (isset($form_state
        ->getValue('effect_options')[$effect_name][$effect_option_name])) {
        $effect_option['#default_value'] = $form_state
          ->getValue('effect_options')[$effect_name][$effect_option_name];
      }
      elseif ($settings['effect'] == $effect_name) {
        $effect_option['#default_value'] = $settings['effect_options'][$effect_name][$effect_option_name];
      }
      $form['effects_wrapper']['effect_options'][$effect_name][$effect_option_name] = $effect_option;
    }
  }
  $form['dependency_advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced edit context settings', [
      '@entity' => $label,
    ]),
    '#open' => FALSE,
  ];
  $selector_description = $this
    ->t('Only use if you know what you are doing, otherwise leave the field empty to let the dependency use an automatically generated selector.');
  $selector_description .= '<br />' . $this
    ->t('You can use the following placeholders:');
  $selector_description .= "<ul>\n";
  $selector_description .= '<li>' . $this
    ->t('%lang: current language of the field.') . "</li>\n";
  $selector_description .= '<li>' . $this
    ->t('%key: part identifier for fields composed of multiple form elements, like checkboxes.') . "</li>\n";
  $selector_description .= '</ul>';
  $form['dependency_advanced']['selector'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom jQuery selector for control field'),
    '#description' => $selector_description,
    '#default_value' => array_key_exists('selector', $settings) ? $settings['selector'] : '',
  ];
  return $form;
}