You are here

public function SortWidgetBase::buildConfigurationForm in Better Exposed Filters 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/better_exposed_filters/sort/SortWidgetBase.php \Drupal\better_exposed_filters\Plugin\better_exposed_filters\sort\SortWidgetBase::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

File

src/Plugin/better_exposed_filters/sort/SortWidgetBase.php, line 56

Class

SortWidgetBase
Base class for Better exposed pager widget plugins.

Namespace

Drupal\better_exposed_filters\Plugin\better_exposed_filters\sort

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = [];
  $form['advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced sort options'),
  ];

  // We can only combine sort order and sort by if both options are exposed.
  $form['advanced']['combine'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Combine sort order with sort by'),
    '#default_value' => !empty($this->configuration['advanced']['combine']),
    '#description' => $this
      ->t('Combines the sort by options and order (ascending or decending) into a single list.  Use this to display "Option1 (ascending)", "Option1 (descending)", "Option2 (ascending)", "Option2 (descending)" in a single form element. Sort order should first be exposed by selecting <em>Allow people to choose the sort order</em>.'),
    '#states' => [
      'enabled' => [
        ':input[name="exposed_form_options[expose_sort_order]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['advanced']['combine_rewrite'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Rewrite the text displayed'),
    '#default_value' => $this->configuration['advanced']['combine_rewrite'],
    '#description' => $this
      ->t('Use this field to rewrite the text displayed for combined sort options and sort order. Use the format of current_text|replacement_text, one replacement per line. For example: <pre>
Post date Asc|Oldest first
Post date Desc|Newest first
Title Asc|A -> Z
Title Desc|Z -> A</pre> Leave the replacement text blank to remove an option altogether. The order the options appear will be changed to match the order of options in this field.'),
    '#states' => [
      'visible' => [
        ':input[name="exposed_form_options[bef][sort][configuration][advanced][combine]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['advanced']['reset'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Include a "Reset sort" option'),
    '#default_value' => !empty($this->configuration['advanced']['reset']),
    '#description' => $this
      ->t('Adds a "Reset sort" link; Views will use the default sort order.'),
  ];
  $form['advanced']['reset_label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('"Reset sort" label'),
    '#default_value' => $this->configuration['advanced']['reset_label'],
    '#description' => $this
      ->t('This cannot be left blank if the above option is checked'),
    '#states' => [
      'visible' => [
        ':input[name="exposed_form_options[bef][sort][configuration][advanced][reset]"]' => [
          'checked' => TRUE,
        ],
      ],
      'required' => [
        ':input[name="exposed_form_options[bef][sort][configuration][advanced][reset]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['advanced']['collapsible'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Make sort options collapsible'),
    '#default_value' => !empty($this->configuration['advanced']['collapsible']),
    '#description' => $this
      ->t('Puts the sort options in a collapsible details element.'),
  ];
  $form['advanced']['collapsible_label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Collapsible details element title'),
    '#default_value' => $this->configuration['advanced']['collapsible_label'],
    '#description' => $this
      ->t('This cannot be left blank or there will be no way to show/hide sort options.'),
    '#states' => [
      'visible' => [
        ':input[name="exposed_form_options[bef][sort][configuration][advanced][collapsible]"]' => [
          'checked' => TRUE,
        ],
      ],
      'required' => [
        ':input[name="exposed_form_options[bef][sort][configuration][advanced][collapsible]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $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;
}