You are here

PagerWidgetBase.php in Better Exposed Filters 8.5

Same filename and directory in other branches
  1. 8.4 src/Plugin/better_exposed_filters/pager/PagerWidgetBase.php

File

src/Plugin/better_exposed_filters/pager/PagerWidgetBase.php
View source
<?php

namespace Drupal\better_exposed_filters\Plugin\better_exposed_filters\pager;

use Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetBase;
use Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Base class for Better exposed pager widget plugins.
 */
abstract class PagerWidgetBase extends BetterExposedFiltersWidgetBase implements BetterExposedFiltersWidgetInterface {
  use StringTranslationTrait;

  /**
   * List of available exposed sort form element keys.
   *
   * @var array
   */
  protected $pagerElements = [
    'items_per_page',
    'offset',
  ];

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'advanced' => [
        'is_secondary' => FALSE,
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable($handler = NULL, array $options = []) {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = [];
    $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;
  }

  /**
   * {@inheritdoc}
   */
  public function exposedFormAlter(array &$form, FormStateInterface $form_state) {
    $is_secondary = !empty($form['secondary']) && $this->configuration['advanced']['is_secondary'];
    foreach ($this->pagerElements as $element) {

      // Sanity check to make sure the element exists.
      if (empty($form[$element])) {
        continue;
      }
      if ($is_secondary) {
        $this
          ->addElementToGroup($form, $form_state, $element, 'secondary');
      }

      // Finally, add some metadata to the form element.
      $this
        ->addContext($form[$element]);
    }
  }

}

Classes

Namesort descending Description
PagerWidgetBase Base class for Better exposed pager widget plugins.