View source
<?php
namespace Drupal\sitemap\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Config\ConfigFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\sitemap\SitemapManager;
use Drupal\Core\Link;
class SitemapSettingsForm extends ConfigFormBase {
protected $sitemapManager;
protected $plugins = [];
public function __construct(ConfigFactory $config_factory, SitemapManager $sitemap_manager) {
parent::__construct($config_factory);
$this->sitemapManager = $sitemap_manager;
}
public static function create(ContainerInterface $container) {
$form = new static($container
->get('config.factory'), $container
->get('plugin.manager.sitemap'));
return $form;
}
public function getFormId() {
return 'sitemap_settings';
}
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' => Link::fromTextAndUrl($this
->t('sitemap page'), Url::fromRoute('sitemap.page'))
->toString(),
]),
];
$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.'),
];
$plugins = $config
->get('plugins');
$definitions = $this->sitemapManager
->getDefinitions();
foreach ($definitions as $id => $definition) {
if ($this->sitemapManager
->hasDefinition($id)) {
$plugin_config = [];
if (!empty($plugins[$id])) {
$plugin_config = $plugins[$id];
}
$this->plugins[$id] = $this->sitemapManager
->createInstance($id, $plugin_config);
}
}
$form['plugins']['enabled'] = [
'#type' => 'item',
'#title' => $this
->t('Enabled plugins'),
'#prefix' => '<div id="sitemap-enabled-wrapper">',
'#suffix' => '</div>',
'#input' => FALSE,
];
$form['plugins']['order'] = [
'#type' => 'table',
'#attributes' => [
'id' => 'sitemap-order',
],
'#title' => $this
->t('Plugin display order'),
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'plugin-order-weight',
],
],
'#tree' => FALSE,
'#input' => FALSE,
'#theme_wrappers' => [
'form_element',
],
];
$form['plugin_settings'] = [
'#type' => 'vertical_tabs',
'#title' => $this
->t('Plugin settings'),
];
$defaultSort = $this->plugins;
$sorted = $this
->sortPlugins($this->plugins);
foreach ($sorted as $id => $plugin) {
$form['plugins']['enabled'][$id] = [
'#type' => 'checkbox',
'#title' => $plugin
->getLabel(),
'#default_value' => $plugin->enabled,
'#parents' => [
'plugins',
$id,
'enabled',
],
'#description' => $plugin
->getDescription(),
'#weight' => $defaultSort[$id]->weight,
];
$form['plugins']['order'][$id]['#attributes']['class'][] = 'draggable';
$form['plugins']['order'][$id]['#weight'] = $plugin->weight;
$form['plugins']['order'][$id]['filter'] = [
'#markup' => $plugin
->getLabel(),
];
$form['plugins']['order'][$id]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $plugin
->getLabel(),
]),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => $plugin->weight,
'#parents' => [
'plugins',
$id,
'weight',
],
'#attributes' => [
'class' => [
'plugin-order-weight',
],
],
];
$settings_form = [
'#parents' => [
'plugins',
$id,
'settings',
],
'#tree' => TRUE,
];
$settings_form = $plugin
->settingsForm($settings_form, $form_state);
if (!empty($settings_form)) {
$form['plugins']['settings'][$id] = [
'#type' => 'details',
'#title' => $plugin
->getLabel(),
'#open' => TRUE,
'#weight' => $plugin->weight,
'#parents' => [
'plugins',
$id,
'settings',
],
'#group' => 'plugin_settings',
];
$form['plugins']['settings'][$id] += $settings_form;
}
}
$form['#attached']['library'][] = 'sitemap/sitemap.admin';
$form['css'] = [
'#type' => 'details',
'#title' => $this
->t('CSS settings'),
];
$form['css']['include_css'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include sitemap CSS file'),
'#default_value' => $config
->get('include_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' => Link::fromTextAndUrl($this
->t("documentation page"), Url::fromUri('https://www.drupal.org/node/2615568'))
->toString(),
]),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory
->getEditable('sitemap.settings');
foreach ($form_state
->cleanValues()
->getValues() as $key => $value) {
if ($key == 'plugins') {
foreach ($value as $instance_id => $plugin_config) {
$this->plugins[$instance_id]
->setConfiguration($plugin_config);
}
$config
->set($key, $value);
}
else {
$config
->set($key, $value);
}
}
$config
->save();
drupal_flush_all_caches();
parent::submitForm($form, $form_state);
}
protected function getEditableConfigNames() {
return [
'sitemap.settings',
];
}
protected function sortPlugins($plugins) {
$order = [];
foreach ($plugins as $id => $plugin) {
$order[$id] = $plugin->weight;
}
asort($order);
foreach ($order as $id => $weight) {
$order[$id] = $plugins[$id];
}
return $order;
}
}