You are here

public function SimpleSitemapDisplayExtender::buildOptionsForm in Simple XML sitemap (Views integration) 8

Provide a form to edit options for this plugin.

Overrides DisplayExtenderPluginBase::buildOptionsForm

File

src/Plugin/views/display_extender/SimpleSitemapDisplayExtender.php, line 94
Contains Simple XML Sitemap display extender.

Class

SimpleSitemapDisplayExtender
Simple XML Sitemap display extender plugin.

Namespace

Drupal\simple_sitemap_views\Plugin\views\display_extender

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  if ($this
    ->hasSitemapSettings() && $form_state
    ->get('section') == 'simple_sitemap') {
    $form['#title'] .= $this
      ->t('Simple XML Sitemap settings for this display');
    $settings = $this
      ->getSitemapSettings();

    // The index section.
    $form['index'] = [
      '#prefix' => '<div class="simple-sitemap-views-index">',
      '#suffix' => '</div>',
    ];

    // Add a checkbox for JS users, which will have behavior attached to it
    // so it can replace the button.
    $form['index']['index'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Index this display'),
      '#default_value' => $settings['index'],
    ];

    // Then add the button itself.
    $form['index']['index_button'] = [
      '#limit_validation_errors' => [],
      '#type' => 'submit',
      '#value' => $this
        ->t('Index this display'),
      '#submit' => [
        [
          $this,
          'displaySitemapSettingsForm',
        ],
      ],
    ];

    // Show the whole form only if indexing is checked.
    if ($this->options['index']) {

      // Main settings fieldset.
      $form['main'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Main settings'),
      ];

      // The sitemap priority.
      $form['main']['priority'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Priority'),
        '#description' => $this
          ->t('The priority displays will have in the eyes of search engine bots.'),
        '#default_value' => $settings['priority'],
        '#options' => $this->formHelper
          ->getPrioritySelectValues(),
      ];

      // The sitemap change frequency.
      $form['main']['changefreq'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Change frequency'),
        '#description' => $this
          ->t('The frequency with which this display changes. Search engine bots may take this as an indication of how often to index it.'),
        '#default_value' => $settings['changefreq'],
        '#options' => $this->formHelper
          ->getChangefreqSelectValues(),
      ];

      // Argument settings fieldset.
      $form['arguments'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Argument settings'),
      ];

      // Get view arguments options.
      if ($arguments_options = $this
        ->getArgumentsOptions()) {

        // Indexed arguments element.
        $form['arguments']['arguments'] = [
          '#type' => 'checkboxes',
          '#title' => $this
            ->t('Indexed arguments'),
          '#options' => $arguments_options,
          '#default_value' => $settings['arguments'],
          '#attributes' => [
            'class' => [
              'indexed-arguments',
            ],
          ],
        ];

        // Max links with arguments.
        $form['arguments']['max_links'] = [
          '#type' => 'number',
          '#title' => $this
            ->t('Maximum links in a sitemap'),
          '#description' => $this
            ->t('The maximum number of links with different argument values ​​for this display, which will be included in the sitemap. If left blank, all links will be included in the sitemap. Use with caution, since a large number of argument values ​​can lead to a significant increase in the number of links in the sitemap.'),
          '#default_value' => $settings['max_links'],
          '#min' => 1,
        ];
      }
      else {
        $form['arguments']['#description'] = $this
          ->t('This display has no arguments.');
      }
    }

    // Attaching script to form.
    $form['#attached']['library'][] = 'simple_sitemap_views/views_ui.admin';
  }
}