class SitemapSettingsForm in Sitemap 8
Same name and namespace in other branches
- 8.2 src/Form/SitemapSettingsForm.php \Drupal\sitemap\Form\SitemapSettingsForm
- 2.0.x src/Form/SitemapSettingsForm.php \Drupal\sitemap\Form\SitemapSettingsForm
Provides a configuration form for sitemap.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\sitemap\Form\SitemapSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SitemapSettingsForm
1 string reference to 'SitemapSettingsForm'
File
- src/
Form/ SitemapSettingsForm.php, line 18
Namespace
Drupal\sitemap\FormView source
class SitemapSettingsForm extends ConfigFormBase {
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* Constructs a SitemapSettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandler $module_handler
* The module handler.
*/
public function __construct(ConfigFactory $config_factory, ModuleHandler $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$module_handler = $container
->get('module_handler');
$form = new static($container
->get('config.factory'), $module_handler);
if ($module_handler
->moduleExists('book')) {
$form
->setBookManager($container
->get('book.manager'));
}
return $form;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'sitemap_settings';
}
/**
* Set book manager service.
*
* @param \Drupal\book\BookManagerInterface $book_manager
* Book manager service to set.
*/
public function setBookManager(BookManagerInterface $book_manager) {
$this->bookManager = $book_manager;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory
->getEditable('sitemap.settings');
$keys = [
'page_title',
[
'message',
'value',
],
[
'message',
'format',
],
'show_front',
'show_titles',
'show_menus',
'show_menus_hidden',
'show_vocabularies',
'show_description',
'show_count',
'vocabulary_show_links',
'vocabulary_depth',
'term_threshold',
'forum_threshold',
'rss_front',
'show_rss_links',
'rss_taxonomy',
'css',
'order',
];
if ($this->moduleHandler
->moduleExists('book')) {
$keys[] = 'show_books';
$keys[] = 'books_expanded';
}
// Save config.
foreach ($keys as $key) {
if ($form_state
->hasValue($key)) {
if ($key == 'order') {
$order = $form_state
->getValue($key);
asort($order);
$config
->set(is_string($key) ? $key : implode('.', $key), $order);
}
else {
$config
->set(is_string($key) ? $key : implode('.', $key), $form_state
->getValue($key));
}
}
}
$config
->save();
drupal_flush_all_caches();
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'sitemap.settings',
];
}
/**
* Helper function to get all menus.
*
* @return \Drupal\Core\Entity\EntityInterface[]|\Drupal\system\Entity\Menu[]
* The list of menus.
*/
protected function getMenus() {
return Menu::loadMultiple();
}
/**
* Helper function to get all vocabularies.
*
* @return \Drupal\Core\Entity\EntityInterface[]|\Drupal\taxonomy\Entity\Vocabulary[]
* The list of vocabularies.
*/
protected function getVocabularies() {
return Vocabulary::loadMultiple();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SitemapSettingsForm:: |
protected | property | The book manager. | |
SitemapSettingsForm:: |
protected | property | The module handler service. | |
SitemapSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SitemapSettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SitemapSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SitemapSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SitemapSettingsForm:: |
protected | function | Helper function to get all menus. | |
SitemapSettingsForm:: |
protected | function | Helper function to get all vocabularies. | |
SitemapSettingsForm:: |
public | function | Set book manager service. | |
SitemapSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SitemapSettingsForm:: |
public | function |
Constructs a SitemapSettingsForm object. Overrides ConfigFormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |