getSelected example

foreach ($wizard_plugins as $key => $wizard) {
      $options[$key] = $wizard['title'];
    }
    $form['displays']['show']['wizard_key'] = [
      '#type' => 'select',
      '#title' => $this->t('Show'),
      '#options' => $options,
      '#sort_options' => TRUE,
    ];
    $show_form = &$form['displays']['show'];
    $default_value = $this->moduleHandler->moduleExists('node') ? 'node' : 'users';
    $show_form['wizard_key']['#default_value'] = WizardPluginBase::getSelected($form_state['show', 'wizard_key']$default_value$show_form['wizard_key']);
    // Changing this dropdown updates the entire content of $form['displays'] via     // AJAX.     views_ui_add_ajax_trigger($show_form, 'wizard_key', ['displays']);

    // Build the rest of the form based on the currently selected wizard plugin.     $wizard_key = $show_form['wizard_key']['#default_value'];
    $wizard_instance = $this->wizardManager->createInstance($wizard_key);
    $form = $wizard_instance->buildForm($form$form_state);

    return $form;
  }

  
/** * @covers ::getSelected * * @dataProvider providerTestGetSelected */
  public function testGetSelected($expected$element = []$parents = []$user_input = []$not_rebuilding_expected = NULL) {
    $not_rebuilding_expected = $not_rebuilding_expected ?: $expected;
    $form_state = new FormState();
    $form_state->setUserInput($user_input);

    $actual = WizardPluginBase::getSelected($form_state$parents, 'the_default_value', $element);
    $this->assertSame($not_rebuilding_expected$actual);
    $this->assertSame($user_input$form_state->getUserInput());

    $form_state->setRebuild();
    $actual = WizardPluginBase::getSelected($form_state$parents, 'the_default_value', $element);
    $this->assertSame($expected$actual);
    $this->assertSame($user_input$form_state->getUserInput());
  }

  /** * Provides test data for testGetSelected(). */


  /** * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters(). * * Add some options for filter by taxonomy terms. */
  protected function buildFilters(&$form, FormStateInterface $form_state) {
    parent::buildFilters($form$form_state);

    if (isset($form['displays']['show']['type'])) {
      $selected_bundle = static::getSelected($form_state['show', 'type'], 'all', $form['displays']['show']['type']);
    }

    // Add the "tagged with" filter to the view.
    // We construct this filter using taxonomy_index.tid (which limits the     // filtering to a specific vocabulary) rather than     // taxonomy_term_field_data.name (which matches terms in any vocabulary).     // This is because it is a more commonly-used filter that works better with     // the autocomplete UI, and also to avoid confusion with other vocabularies     // on the site that may have terms with the same name but are not used for     // free tagging.
'#title' => $this->t('Page display settings'),
      '#attributes' => ['class' => ['container-inline', 'fieldset-no-legend']],
    ];

    // Create the dropdown for choosing the display format.     $form['displays']['page']['options']['style']['style_plugin'] = [
      '#title' => $this->t('Display format'),
      '#type' => 'select',
      '#options' => $style_options,
    ];
    $style_form = &$form['displays']['page']['options']['style'];
    $style_form['style_plugin']['#default_value'] = static::getSelected($form_state['page', 'style', 'style_plugin'], 'default', $style_form['style_plugin']);
    // Changing this dropdown updates $form['displays']['page']['options'] via     // AJAX.     views_ui_add_ajax_trigger($style_form, 'style_plugin', ['displays', 'page', 'options']);

    $this->buildFormStyle($form$form_state, 'page');
    $form['displays']['page']['options']['items_per_page'] = [
      '#title' => $this->t('Items to display'),
      '#type' => 'number',
      '#default_value' => 10,
      '#min' => 0,
    ];
    
Home | Imprint | This part of the site doesn't use cookies.