You are here

public function SitemapSettingsForm::buildForm in Sitemap 8

Same name and namespace in other branches
  1. 8.2 src/Form/SitemapSettingsForm.php \Drupal\sitemap\Form\SitemapSettingsForm::buildForm()
  2. 2.0.x src/Form/SitemapSettingsForm.php \Drupal\sitemap\Form\SitemapSettingsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SitemapSettingsForm.php, line 82

Class

SitemapSettingsForm
Provides a configuration form for sitemap.

Namespace

Drupal\sitemap\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this->configFactory
    ->get('sitemap.settings');
  $form['page_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Page title'),
    '#default_value' => $config
      ->get('page_title'),
    '#description' => $this
      ->t('Page title that will be used on the @sitemap_page.', [
      '@sitemap_page' => $this
        ->l($this
        ->t('sitemap page'), Url::fromRoute('sitemap.page')),
    ]),
  ];
  $sitemap_message = $config
    ->get('message');
  $form['message'] = [
    '#type' => 'text_format',
    '#format' => isset($sitemap_message['format']) ? $sitemap_message['format'] : NULL,
    '#title' => $this
      ->t('Sitemap message'),
    '#default_value' => $sitemap_message['value'],
    '#description' => $this
      ->t('Define a message to be displayed above the sitemap.'),
  ];
  $form['sitemap_content'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Sitemap content'),
    '#open' => TRUE,
  ];
  $sitemap_ordering = [];
  $form['sitemap_content']['show_front'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show front page'),
    '#default_value' => $config
      ->get('show_front'),
    '#description' => $this
      ->t('When enabled, this option will include the front page in the sitemap.'),
  ];
  $sitemap_ordering['front'] = t('Front page');

  // Build list of books.
  if ($this->moduleHandler
    ->moduleExists('book')) {
    $book_options = [];
    foreach ($this->bookManager
      ->getAllBooks() as $book) {
      $book_options[$book['bid']] = $book['title'];
      $sitemap_ordering['books_' . $book['bid']] = $book['title'];
    }
    $form['sitemap_content']['show_books'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Books to include in the sitemap'),
      '#default_value' => $config
        ->get('show_books'),
      '#options' => $book_options,
      '#multiple' => TRUE,
    ];
  }

  // Build list of menus.
  $menus = $this
    ->getMenus();
  $menu_options = [];
  foreach ($menus as $id => $menu) {
    $menu_options[$id] = $menu
      ->label();
    $sitemap_ordering['menus_' . $id] = $menu
      ->label();
  }
  $form['sitemap_content']['show_menus'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Menus to include in the sitemap'),
    '#default_value' => $config
      ->get('show_menus'),
    '#options' => $menu_options,
    '#multiple' => TRUE,
  ];

  // Build list of vocabularies.
  if ($this->moduleHandler
    ->moduleExists('taxonomy')) {
    $vocab_options = [];
    $vocabularies = $this
      ->getVocabularies();
    foreach ($vocabularies as $vocabulary) {
      $vocab_options[$vocabulary
        ->id()] = $vocabulary
        ->label();
      $sitemap_ordering['vocabularies_' . $vocabulary
        ->id()] = $vocabulary
        ->label();
    }
    $form['sitemap_content']['show_vocabularies'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Vocabularies to include in the sitemap'),
      '#default_value' => $config
        ->get('show_vocabularies'),
      '#options' => $vocab_options,
      '#multiple' => TRUE,
    ];
  }

  // Follows FilterFormatFormBase for tabledrag ordering.
  $form['sitemap_content']['order'] = [
    '#type' => 'table',
    '#attributes' => [
      'id' => 'sitemap-order',
    ],
    '#title' => $this
      ->t('Sitemap order'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'sitemap-order-weight',
      ],
    ],
    '#tree' => FALSE,
    '#input' => FALSE,
    '#theme_wrappers' => [
      'form_element',
    ],
  ];
  $sitemap_order_defaults = $config
    ->get('order');
  foreach ($sitemap_ordering as $content_id => $content_title) {
    $form['sitemap_content']['order'][$content_id] = [
      'content' => [
        '#markup' => $content_title,
      ],
      'weight' => [
        '#type' => 'weight',
        '#title' => t('Weight for @title', [
          '@title' => $content_title,
        ]),
        '#title_display' => 'invisible',
        '#delta' => 50,
        '#default_value' => isset($sitemap_order_defaults[$content_id]) ? $sitemap_order_defaults[$content_id] : -50,
        '#parents' => [
          'order',
          $content_id,
        ],
        '#attributes' => [
          'class' => [
            'sitemap-order-weight',
          ],
        ],
      ],
      '#weight' => isset($sitemap_order_defaults[$content_id]) ? $sitemap_order_defaults[$content_id] : -50,
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
    ];
  }

  // Re-order content drag-and-drop items based on pre-existing config.
  asort($sitemap_order_defaults);
  foreach ($sitemap_order_defaults as $content_id => $weight) {
    if (isset($form['sitemap_content']['order'][$content_id])) {
      $item = $form['sitemap_content']['order'][$content_id];
      unset($form['sitemap_content']['order'][$content_id]);
      $form['sitemap_content']['order'][$content_id] = $item;
    }
  }

  // Re-add new config items.
  $new = array_diff_key($sitemap_ordering, $sitemap_order_defaults);
  foreach ($new as $content_id => $content_title) {
    $item = $form['sitemap_content']['order'][$content_id];
    unset($form['sitemap_content']['order'][$content_id]);
    $form['sitemap_content']['order'][$content_id] = $item;
  }
  $form['#attached']['library'][] = 'sitemap/sitemap.admin';
  $form['sitemap_options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Sitemap settings'),
  ];
  $form['sitemap_options']['show_titles'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show titles'),
    '#default_value' => $config
      ->get('show_titles'),
    '#description' => $this
      ->t('When enabled, this option will show titles. Disable to not show section titles.'),
  ];
  $form['sitemap_options']['sitemap_rss_options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('RSS settings'),
  ];
  $form['sitemap_options']['sitemap_rss_options']['rss_front'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('RSS feed for front page'),
    '#default_value' => $config
      ->get('rss_front'),
    '#description' => $this
      ->t('The RSS feed for the front page, default is rss.xml.'),
  ];
  $form['sitemap_options']['sitemap_rss_options']['show_rss_links'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Include RSS links'),
    '#default_value' => $config
      ->get('show_rss_links'),
    '#options' => [
      0 => $this
        ->t('None'),
      1 => $this
        ->t('Include on the right side'),
      2 => $this
        ->t('Include on the left side'),
    ],
    '#description' => $this
      ->t('When enabled, this option will show links to the RSS feeds for the front page and taxonomy terms, if enabled.'),
  ];
  $form['sitemap_options']['sitemap_rss_options']['rss_taxonomy'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('RSS depth for vocabularies'),
    '#default_value' => $config
      ->get('rss_taxonomy'),
    '#size' => 3,
    '#maxlength' => 10,
    '#description' => $this
      ->t('Specify how many RSS feed links should be displayed with taxonomy terms. Enter "-1" to include with all terms, "0" not to include with any terms, or "1" to show only for top-level taxonomy terms.'),
  ];
  $form['sitemap_options']['sitemap_css_options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('CSS settings'),
  ];
  $form['sitemap_options']['sitemap_css_options']['css'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Include sitemap CSS file'),
    '#default_value' => $config
      ->get('css'),
    '#description' => $this
      ->t("Select this box if you wish to load the CSS file included with the module. To learn how to override or specify the CSS at the theme level, visit the @documentation_page.", [
      '@documentation_page' => $this
        ->l($this
        ->t("documentation page"), Url::fromUri('https://www.drupal.org/node/2615568')),
    ]),
  ];
  if ($this->moduleHandler
    ->moduleExists('book')) {
    $form['sitemap_book_options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Book settings'),
    ];
    $form['sitemap_book_options']['books_expanded'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show books expanded'),
      '#default_value' => $config
        ->get('books_expanded'),
      '#description' => $this
        ->t('When enabled, this option will show all children pages for each book.'),
    ];
  }
  if ($this->moduleHandler
    ->moduleExists('forum')) {
    $form['sitemap_forum_options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Forum settings'),
    ];
    $form['sitemap_forum_options']['forum_threshold'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Forum count threshold'),
      '#default_value' => $config
        ->get('forum_threshold'),
      '#size' => 3,
      '#description' => $this
        ->t('Only show forums whose node counts are greater than this threshold. Set to -1 to disable.'),
    ];
  }
  $form['sitemap_menu_options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Menu settings'),
  ];
  $form['sitemap_menu_options']['show_menus_hidden'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show disabled menu items'),
    '#default_value' => $config
      ->get('show_menus_hidden'),
    '#description' => $this
      ->t('When enabled, hidden menu links will also be shown.'),
  ];
  if ($this->moduleHandler
    ->moduleExists('taxonomy')) {
    $form['sitemap_taxonomy_options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Taxonomy settings'),
    ];
    $form['sitemap_taxonomy_options']['show_description'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show vocabulary description'),
      '#default_value' => $config
        ->get('show_description'),
      '#description' => $this
        ->t('When enabled, this option will show the vocabulary description.'),
    ];
    $form['sitemap_taxonomy_options']['show_count'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show node counts by taxonomy terms'),
      '#default_value' => $config
        ->get('show_count'),
      '#description' => $this
        ->t('When enabled, this option will show the number of nodes in each taxonomy term.'),
    ];
    $form['sitemap_taxonomy_options']['vocabulary_show_links'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Show links for taxonomy terms even if they don't contain any nodes"),
      '#default_value' => $config
        ->get('vocabulary_show_links'),
      '#description' => $this
        ->t('When enabled, this option will turn every taxonomy term into a link.'),
    ];
    $form['sitemap_taxonomy_options']['vocabulary_depth'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Vocabulary depth'),
      '#default_value' => $config
        ->get('vocabulary_depth'),
      '#size' => 3,
      '#maxlength' => 10,
      '#description' => $this
        ->t('Specify how many levels taxonomy terms should be included. Enter "-1" to include all terms, "0" not to include terms at all, or "1" to only include top-level terms.'),
    ];
    $form['sitemap_taxonomy_options']['term_threshold'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Term count threshold'),
      '#default_value' => $config
        ->get('term_threshold'),
      '#size' => 3,
      '#description' => $this
        ->t('Only show taxonomy terms whose node counts are greater than this threshold. Set to -1 to disable.'),
    ];
  }
  return parent::buildForm($form, $form_state);
}