rewriteStatesSelector example

if ($this->multiple) {
      $form['field_api_classes']['#description'] .= ' ' . $this->t('Checking this option will cause the group Display Type and Separator values to be ignored.');
    }

    // Get the settings form.     $settings_form = ['#value' => []];
    $format = isset($form_state->getUserInput()['options']['type']) ? $form_state->getUserInput()['options']['type'] : $this->options['type'];
    if ($formatter = $this->getFormatterInstance($format)) {
      $settings_form = $formatter->settingsForm($form$form_state);
      // Convert field UI selector states to work in the Views field form.       FormHelper::rewriteStatesSelector($settings_form, "fields[{$field->getName()}][settings_edit_form]", 'options');
    }
    $form['settings'] = $settings_form;
  }

  /** * {@inheritdoc} */
  public function submitFormCalculateOptions(array $options, array $form_state_options) {
    // When we change the formatter type we don't want to keep any of the     // previous configured formatter settings, as there might be schema     // conflict.

  public static function rewriteStatesSelector(array &$elements$search$replace) {
    if (!empty($elements['#states'])) {
      foreach ($elements['#states'] as $state => $ids) {
        static::processStatesArray($elements['#states'][$state]$search$replace);
      }
    }
    foreach (Element::children($elements) as $key) {
      static::rewriteStatesSelector($elements[$key]$search$replace);
    }
  }

  /** * Helps recursively rewrite #states selectors. * * Not to be confused with self::processStates(), which just prepares states * for rendering. * * @param array $conditions * States conditions array. * @param string $search * A partial or entire jQuery selector string to replace in #states. * @param string $replace * The string to replace all instances of $search with. * * @see self::rewriteStatesSelector() */


  /** * Render API callback: builds the formatter settings elements. */
  public function formatterSettingsProcessCallback(array &$element, FormStateInterface $form_state, array &$complete_form) {
    if ($formatter = $this->getFormatter($element['#parents']$form_state)) {
      $element['settings_wrapper']['settings'] = $formatter->settingsForm($complete_form$form_state);
      $element['settings_wrapper']['settings']['#parents'] = array_merge($element['#parents']['settings']);
      $element['settings_wrapper']['third_party_settings'] = $this->thirdPartySettingsForm($formatter$this->getFieldDefinition()$complete_form$form_state);
      $element['settings_wrapper']['third_party_settings']['#parents'] = array_merge($element['#parents']['third_party_settings']);
      FormHelper::rewriteStatesSelector($element['settings_wrapper'], "fields[$this->fieldName][settings_edit_form]", 'settings[formatter]');

      // Store the array parents for our element so that we can retrieve the       // formatter settings in our AJAX callback.       $form_state->set('field_block_array_parents', $element['#array_parents']);
    }
    return $element;
  }

  /** * Adds the formatter third party settings forms. * * @param \Drupal\Core\Field\FormatterInterface $plugin * The formatter. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition * The field definition. * @param array $form * The (entire) configuration form array. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. * * @return array * The formatter third party settings form. */
$this->operatorForm($row$form_state);
      // Force the operator form to be a select box. Some handlers uses       // radios and they occupy a lot of space in a table row.       $row['operator']['#type'] = 'select';
      $row['operator']['#title'] = '';
      $this->valueForm($row$form_state);

      // Fix the dependencies to update value forms when operators changes. This       // is needed because forms are inside a new form and their IDs changes.       // Dependencies are used when operator changes from to 'Between',       // 'Not Between', etc, and two or more widgets are displayed.       FormHelper::rewriteStatesSelector($row['value'], ':input[name="options[operator]"]', ':input[name="options[group_info][group_items][' . $item_id . '][operator]"]');

      // Set default values.       $children = Element::children($row['value']);
      if (!empty($children)) {
        foreach ($children as $child) {
          if (!empty($row['value'][$child]['#states']['visible'])) {
            foreach ($row['value'][$child]['#states']['visible'] as $state) {
              if (isset($state[':input[name="options[group_info][group_items][' . $item_id . '][operator]"]'])) {
                $row['value'][$child]['#title'] = '';

                // Exit this loop and process the next child element.

  public function testRewriteStatesSelector() {

    // Simple selectors.     $value = ['value' => 'medium'];
    $form['foo']['#states'] = [
      'visible' => [
        'select[name="fields[foo-id][settings_edit_form][settings][image_style]"]' => $value,
      ],
    ];
    FormHelper::rewriteStatesSelector($form, 'fields[foo-id][settings_edit_form]', 'options');
    $expected_selector = 'select[name="options[settings][image_style]"]';
    $this->assertSame($form['foo']['#states']['visible'][$expected_selector]$value, 'The #states selector was not properly rewritten.');

    // Complex selectors.     $form = [];
    $form['bar']['#states'] = [
      'visible' => [
        [
          ':input[name="menu[type]"]' => ['value' => 'normal'],
        ],
        [
          
Home | Imprint | This part of the site doesn't use cookies.