You are here

abstract class BetterExposedFiltersWidgetBase in Better Exposed Filters 8.5

Same name and namespace in other branches
  1. 8.4 src/Plugin/BetterExposedFiltersWidgetBase.php \Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetBase

Base class for Better exposed filters widget plugins.

Hierarchy

Expanded class hierarchy of BetterExposedFiltersWidgetBase

3 files declare their use of BetterExposedFiltersWidgetBase
FilterWidgetBase.php in src/Plugin/better_exposed_filters/filter/FilterWidgetBase.php
PagerWidgetBase.php in src/Plugin/better_exposed_filters/pager/PagerWidgetBase.php
SortWidgetBase.php in src/Plugin/better_exposed_filters/sort/SortWidgetBase.php

File

src/Plugin/BetterExposedFiltersWidgetBase.php, line 16

Namespace

Drupal\better_exposed_filters\Plugin
View source
abstract class BetterExposedFiltersWidgetBase extends PluginBase implements BetterExposedFiltersWidgetInterface {
  use StringTranslationTrait;

  /**
   * The views executable object.
   *
   * @var \Drupal\views\ViewExecutable
   */
  protected $view;

  /**
   * The views plugin this configuration will affect when exposed.
   *
   * @var \Drupal\views\Plugin\views\ViewsHandlerInterface
   */
  protected $handler;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configuration = NestedArray::mergeDeep($this
      ->defaultConfiguration(), $configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'plugin_id' => $this->pluginId,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setView(ViewExecutable $view) {
    $this->view = $view;
  }

  /**
   * {@inheritdoc}
   */
  public function setViewsHandler(ViewsHandlerInterface $handler) {
    $this->handler = $handler;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Validation is optional.
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Apply submitted form state to configuration.
    $values = $form_state
      ->getValues();
    foreach ($values as $key => $value) {
      if (array_key_exists($key, $this->configuration)) {
        $this->configuration[$key] = $value;
      }
      else {

        // Remove from form state.
        unset($values[$key]);
      }
    }
  }

  /*
   * Helper functions.
   */

  /**
   * Sets metadata on the form elements for easier processing.
   *
   * @param array $element
   *   The form element to apply the metadata to.
   *
   * @see ://www.drupal.org/project/drupal/issues/2511548
   */
  protected function addContext(array &$element) {
    $element['#context'] = [
      '#plugin_type' => 'bef',
      '#plugin_id' => $this->pluginId,
      '#view_id' => $this->view
        ->id(),
      '#display_id' => $this->view->current_display,
    ];
  }

  /**
   * Moves an exposed form element into a field group.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Exposed views form state.
   * @param string $element
   *   The key of the form element.
   * @param string $group
   *   The name of the group element.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   *   If the instance cannot be created, such as if the ID is invalid.
   */
  protected function addElementToGroup(array &$form, FormStateInterface $form_state, $element, $group) {

    // Ensure group is enabled.
    $form[$group]['#access'] = TRUE;

    // Add element to group.
    $form[$element]['#group'] = $group;

    // Persist state of collapsible field-sets with active elements.
    if (empty($form[$group]['#open'])) {

      // Use raw user input to determine if field-set should be open or closed.
      $user_input = $form_state
        ->getUserInput()[$element] ?? [
        0,
      ];

      // Take multiple values into account.
      if (!is_array($user_input)) {
        $user_input = [
          $user_input,
        ];
      }

      // Check if one or more values are set for our current element.
      $options = $form[$element]['#options'] ?? [];
      $default_value = $form[$element]['#default_value'] ?? key($options);
      $has_values = array_reduce($user_input, function ($carry, $value) use ($form, $element, $default_value) {
        return $carry || ($value === $default_value ? '' : $value || $default_value === 0);
      }, FALSE);
      if ($has_values) {
        $form[$group]['#open'] = TRUE;
      }
    }
  }

  /**
   * Returns exposed form action URL object.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Exposed views form state.
   *
   * @return \Drupal\Core\Url
   *   Url object.
   */
  protected function getExposedFormActionUrl(FormStateInterface $form_state) {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $form_state
      ->get('view');
    $display = $form_state
      ->get('display');
    if (isset($display['display_options']['path'])) {
      return Url::fromRoute(implode('.', [
        'view',
        $view
          ->id(),
        $display['id'],
      ]));
    }
    $request = \Drupal::request();
    $url = Url::createFromRequest(clone $request);
    $url
      ->setAbsolute();
    return $url;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BetterExposedFiltersWidgetBase::$handler protected property The views plugin this configuration will affect when exposed.
BetterExposedFiltersWidgetBase::$view protected property The views executable object.
BetterExposedFiltersWidgetBase::addContext protected function Sets metadata on the form elements for easier processing.
BetterExposedFiltersWidgetBase::addElementToGroup protected function Moves an exposed form element into a field group.
BetterExposedFiltersWidgetBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 3
BetterExposedFiltersWidgetBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
BetterExposedFiltersWidgetBase::getExposedFormActionUrl protected function Returns exposed form action URL object.
BetterExposedFiltersWidgetBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
BetterExposedFiltersWidgetBase::setView public function Sets the view object. Overrides BetterExposedFiltersWidgetInterface::setView
BetterExposedFiltersWidgetBase::setViewsHandler public function Sets the exposed view handler plugin. Overrides BetterExposedFiltersWidgetInterface::setViewsHandler
BetterExposedFiltersWidgetBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
BetterExposedFiltersWidgetBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
BetterExposedFiltersWidgetBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
BetterExposedFiltersWidgetInterface::exposedFormAlter public function Manipulate views exposed from element. 3
BetterExposedFiltersWidgetInterface::isApplicable public static function Verify this plugin can be used on the form element. 3
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginFormInterface::buildConfigurationForm public function Form constructor. 36
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.