You are here

public function FilterWidgetBase::buildConfigurationForm in Better Exposed Filters 8.5

Same name and namespace in other branches
  1. 8.4 src/Plugin/better_exposed_filters/filter/FilterWidgetBase.php \Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter\FilterWidgetBase::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

3 calls to FilterWidgetBase::buildConfigurationForm()
Links::buildConfigurationForm in src/Plugin/better_exposed_filters/filter/Links.php
Form constructor.
RadioButtons::buildConfigurationForm in src/Plugin/better_exposed_filters/filter/RadioButtons.php
Form constructor.
Sliders::buildConfigurationForm in src/Plugin/better_exposed_filters/filter/Sliders.php
Form constructor.
3 methods override FilterWidgetBase::buildConfigurationForm()
Links::buildConfigurationForm in src/Plugin/better_exposed_filters/filter/Links.php
Form constructor.
RadioButtons::buildConfigurationForm in src/Plugin/better_exposed_filters/filter/RadioButtons.php
Form constructor.
Sliders::buildConfigurationForm in src/Plugin/better_exposed_filters/filter/Sliders.php
Form constructor.

File

src/Plugin/better_exposed_filters/filter/FilterWidgetBase.php, line 87

Class

FilterWidgetBase
Base class for Better exposed filters widget plugins.

Namespace

Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
  $filter = $this->handler;
  $filter_widget_type = $this
    ->getExposedFilterWidgetType();
  $form['advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced filter options'),
    '#weight' => 10,
  ];

  // Allow users to sort options.
  $supported_types = [
    'select',
  ];
  if (in_array($filter_widget_type, $supported_types)) {
    $form['advanced']['sort_options'] = [
      '#type' => 'checkbox',
      '#title' => 'Sort filter options',
      '#default_value' => !empty($this->configuration['advanced']['sort_options']),
      '#description' => $this
        ->t('The options will be sorted alphabetically.'),
    ];
  }

  // Allow users to specify placeholder text.
  $supported_types = [
    'entity_autocomplete',
    'textfield',
  ];
  if (in_array($filter_widget_type, $supported_types)) {
    $form['advanced']['placeholder_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Placeholder text'),
      '#description' => $this
        ->t('Text to be shown in the text field until it is edited. Leave blank for no placeholder to be set.'),
      '#default_value' => $this
        ->t($this->configuration['advanced']['placeholder_text']),
    ];
  }

  // Allow rewriting of filter options for any filter. String and numeric
  // filters allow unlimited filter options via textfields, so we can't
  // offer rewriting for those.
  // @todo check other core filter types
  if (!$filter instanceof StringFilter && !$filter instanceof NumericFilter || $filter
    ->isAGroup()) {
    $form['advanced']['rewrite']['filter_rewrite_values'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Rewrite the text displayed'),
      '#default_value' => $this->configuration['advanced']['rewrite']['filter_rewrite_values'],
      '#description' => $this
        ->t('Use this field to rewrite the filter options displayed. Use the format of current_text|replacement_text, one replacement per line. For example: <pre>
  Current|Replacement
  On|Yes
  Off|No
  </pre> Leave the replacement text blank to remove an option altogether. If using hierarchical taxonomy filters, do not including leading hyphens in the current text.
          '),
    ];
  }

  // Allow any filter to be collapsible.
  $form['advanced']['collapsible'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Make filter options collapsible'),
    '#default_value' => !empty($this->configuration['advanced']['collapsible']),
    '#description' => $this
      ->t('Puts the filter options in a collapsible details element.'),
  ];

  // Allow any filter to be moved into the secondary options element.
  $form['advanced']['is_secondary'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('This is a secondary option'),
    '#default_value' => !empty($this->configuration['advanced']['is_secondary']),
    '#states' => [
      'visible' => [
        ':input[name="exposed_form_options[bef][general][allow_secondary]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#description' => $this
      ->t('Places this element in the secondary options portion of the exposed form.'),
  ];
  return $form;
}