You are here

public function PagererStyleBase::buildConfigurationForm in Pagerer 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/pagerer/PagererStyleBase.php \Drupal\pagerer\Plugin\pagerer\PagererStyleBase::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

4 calls to PagererStyleBase::buildConfigurationForm()
Basic::buildConfigurationForm in src/Plugin/pagerer/Basic.php
Form constructor.
Mini::buildConfigurationForm in src/Plugin/pagerer/Mini.php
Form constructor.
Progressive::buildConfigurationForm in src/Plugin/pagerer/Progressive.php
Form constructor.
Slider::buildConfigurationForm in src/Plugin/pagerer/Slider.php
Form constructor.
4 methods override PagererStyleBase::buildConfigurationForm()
Basic::buildConfigurationForm in src/Plugin/pagerer/Basic.php
Form constructor.
Mini::buildConfigurationForm in src/Plugin/pagerer/Mini.php
Form constructor.
Progressive::buildConfigurationForm in src/Plugin/pagerer/Progressive.php
Form constructor.
Slider::buildConfigurationForm in src/Plugin/pagerer/Slider.php
Form constructor.

File

src/Plugin/pagerer/PagererStyleBase.php, line 112

Class

PagererStyleBase
Base plugin for Pagerer.

Namespace

Drupal\pagerer\Plugin\pagerer

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $config = [];
  $d = $this->configFactory
    ->get('pagerer.style.' . $this
    ->getPluginId())
    ->get('default_config');

  // General variables.
  $display_options = [
    'pages' => $this
      ->t('Pages'),
    'items' => $this
      ->t('Items'),
    'item_ranges' => $this
      ->t('Item ranges'),
  ];
  $config['display'] = [
    '#type' => 'select',
    '#title' => $this
      ->t("Display"),
    '#options' => $display_options,
    '#default_value' => $this->configuration['display'],
    '#description' => $this
      ->t("Select whether to display pages, items, or item ranges."),
    '#required' => TRUE,
  ];

  // Display configuration.
  $config['display_container'] = [
    '#type' => 'details',
    '#title' => $this
      ->t("Main options"),
  ];
  if (isset($d['quantity'])) {
    $config['display_container']['quantity'] = [
      '#type' => 'number',
      '#title' => $this
        ->t("Quantity"),
      '#default_value' => $this->configuration['quantity'],
      '#description' => $this
        ->t("The number of pages in the pager list."),
      '#required' => TRUE,
      '#size' => 3,
      '#maxlength' => 3,
      '#min' => 1,
    ];
  }
  if (isset($d['display_mode'])) {
    $options = [
      'normal' => $this
        ->t('Text elements.'),
    ];
    $config['display_container']['display_mode'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t("Pager mode"),
      '#options' => $options,
      '#default_value' => $this->configuration['display_mode'],
      '#description' => $this
        ->t("Select how to render the pager."),
      '#required' => TRUE,
    ];
  }
  if (isset($d['prefix_display'])) {
    $config['display_container']['prefix_display'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Prefix label"),
      '#default_value' => $this->configuration['prefix_display'],
      '#description' => $this
        ->t("Display a text label (e.g. 'Page') before the pager. Configure the label in the 'Text strings' section below."),
    ];
  }
  if (isset($d['suffix_display'])) {
    $config['display_container']['suffix_display'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Suffix label"),
      '#default_value' => $this->configuration['suffix_display'],
      '#description' => $this
        ->t("Display a text label (e.g. 'of @total') after the pager. Configure the label in the 'Text strings' section below."),
    ];
  }
  if (isset($d['display_restriction'])) {
    $options = [
      2 => $this
        ->t('Display pager if there are at least two pages of results (default).'),
      1 => $this
        ->t('Display pager if there is at least one page of results.'),
      0 => $this
        ->t('Display pager even if the result set is empty.'),
    ];
    $config['display_container']['display_restriction'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t("Restriction"),
      '#options' => $options,
      '#default_value' => $this->configuration['display_restriction'],
      '#required' => TRUE,
    ];
  }
  if (isset($d['progr_links'])) {
    $options = [
      'relative',
      'absolute',
    ];
    $options = array_combine($options, $options);
    $config['display_container']['progr_links'] = [
      '#type' => 'select',
      '#title' => $this
        ->t("Outer pages"),
      '#options' => $options,
      '#default_value' => $this->configuration['progr_links'],
      '#description' => $this
        ->t("Select how to render links to pages far from the current, as 'absolute' page numbers (or items/item ranges), or as 'relative' offsets from current (e.g. +10 +100 +1000)."),
      '#required' => TRUE,
    ];
  }

  // Links configuration.
  $config['links_container'] = [
    '#type' => 'details',
    '#title' => $this
      ->t("Links"),
    '#description' => $this
      ->t("Configure link elements like '« First', '‹ Previous', 'Next ›' and 'Last »'."),
  ];
  if (isset($d['first_link'])) {
    $options = [
      'never',
      'not_on_first',
      'always',
    ];
    $options = array_combine($options, $options);
    $config['links_container']['first_link'] = [
      '#type' => 'select',
      '#title' => $this
        ->t("First"),
      '#options' => $options,
      '#default_value' => $this->configuration['first_link'],
      '#description' => $this
        ->t("Select when to render a link to the first page (e.g. '« First'). Options are 'never' (not displayed), 'not_on_first' (not displayed if current page is the first), 'always' (always displayed)."),
      '#required' => TRUE,
    ];
  }
  if (isset($d['previous_link'])) {
    $options = [
      'never',
      'not_on_first',
      'always',
    ];
    $options = array_combine($options, $options);
    $config['links_container']['previous_link'] = [
      '#type' => 'select',
      '#title' => $this
        ->t("Previous"),
      '#options' => $options,
      '#default_value' => $this->configuration['previous_link'],
      '#description' => $this
        ->t("Select when to render a link to the previous page (e.g. '‹ Previous'). Options are 'never' (not displayed), 'not_on_first' (not displayed if current page is the first), 'always' (always displayed)."),
      '#required' => TRUE,
    ];
  }
  if (isset($d['next_link'])) {
    $options = [
      'never',
      'not_on_last',
      'always',
    ];
    $options = array_combine($options, $options);
    $config['links_container']['next_link'] = [
      '#type' => 'select',
      '#title' => $this
        ->t("Next"),
      '#options' => $options,
      '#default_value' => $this->configuration['next_link'],
      '#description' => $this
        ->t("Select when to render a link to the next page (e.g. 'Next ›'). Options are 'never' (not displayed), 'not_on_last' (not displayed if current page is the last), 'always' (always displayed)."),
      '#required' => TRUE,
    ];
  }
  if (isset($d['last_link'])) {
    $options = [
      'never',
      'not_on_last',
      'always',
    ];
    $options = array_combine($options, $options);
    $config['links_container']['last_link'] = [
      '#type' => 'select',
      '#title' => $this
        ->t("Last"),
      '#options' => $options,
      '#default_value' => $this->configuration['last_link'],
      '#description' => $this
        ->t("Select when to render a link to the last page (e.g. 'Last »'). Options are 'never' (not displayed), 'not_on_last' (not displayed if current page is the last), 'always' (always displayed)."),
      '#required' => TRUE,
    ];
  }

  // Separators configuration.
  $config['separators_container'] = [
    '#type' => 'details',
    '#title' => $this
      ->t("Separators"),
    '#description' => $this
      ->t("Configure separators."),
  ];
  if (isset($d['breaker_display'])) {
    $config['separators_container']['breaker_display'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Page breaker"),
      '#default_value' => $this->configuration['breaker_display'],
      '#description' => $this
        ->t("Display a breaker when the page sequence breaks."),
    ];
  }
  if (isset($d['tags']['page_breaker'])) {
    $config['separators_container']['page_breaker'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t("Breaker"),
      '#default_value' => $this->configuration['tags']['page_breaker'],
      '#description' => $this
        ->t("Text to use as page breaker."),
      '#states' => [
        'visible' => [
          ':input[name="breaker_display"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  if (isset($d['separator_display'])) {
    $config['separators_container']['separator_display'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Page separator"),
      '#default_value' => $this->configuration['separator_display'],
      '#description' => $this
        ->t("Display a separator between the page links."),
    ];
  }
  if (isset($d['tags']['page_separator'])) {
    $config['separators_container']['page_separator'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t("Separator"),
      '#default_value' => $this->configuration['tags']['page_separator'],
      '#description' => $this
        ->t("Text to use as page separator."),
      '#states' => [
        'visible' => [
          ':input[name="separator_display"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }

  // Tags configuration. For each display type, loops through the
  // default config to fetch the tags, and retrieves titles from
  // the config schema.
  $display_tags = $this->typedConfigManager
    ->getDefinition('pagerer.tags_display_config.' . $this
    ->getPluginId());
  $config['tags_container'] = [
    '#tree' => TRUE,
  ];
  foreach ($d['tags'] as $tags_key => $tags_set) {
    if (in_array($tags_key, [
      'pages',
      'items',
      'item_ranges',
    ])) {
      $config['tags_container'][$tags_key] = [
        '#type' => 'details',
        '#title' => $display_options[$tags_key] . ' - ' . $this
          ->t("Text strings"),
        '#description' => $this
          ->t("Configure text strings."),
        '#states' => [
          'visible' => [
            ':input[name="display"]' => [
              'value' => $tags_key,
            ],
          ],
        ],
      ];
      foreach ($tags_set as $tag => $map) {
        $config['tags_container'][$tags_key][$tag] = [
          '#type' => 'textfield',
          '#title' => $display_tags['mapping'][$tag]['label'],
          '#default_value' => $this->configuration['tags'][$tags_key][$tag],
        ];
      }
    }
  }
  return $config;
}