You are here

class SitemapSettingsForm in Sitemap 2.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/SitemapSettingsForm.php \Drupal\sitemap\Form\SitemapSettingsForm
  2. 8 src/Form/SitemapSettingsForm.php \Drupal\sitemap\Form\SitemapSettingsForm

Provides a configuration form for sitemap.

Hierarchy

Expanded class hierarchy of SitemapSettingsForm

1 string reference to 'SitemapSettingsForm'
sitemap.routing.yml in ./sitemap.routing.yml
sitemap.routing.yml

File

src/Form/SitemapSettingsForm.php, line 16

Namespace

Drupal\sitemap\Form
View source
class SitemapSettingsForm extends ConfigFormBase {

  /**
   * The SitemapMap plugin manager.
   *
   * @var \Drupal\sitemap\SitemapManager
   */
  protected $sitemapManager;

  /**
   * An array of Sitemap plugins.
   *
   * @var \Drupal\sitemap\SitemapInterface[]
   */
  protected $plugins = [];

  /**
   * Constructs a SitemapSettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\sitemap\SitemapManager $sitemap_manager
   *   The Sitemap plugin manager.
   */
  public function __construct(ConfigFactory $config_factory, SitemapManager $sitemap_manager) {
    parent::__construct($config_factory);
    $this->sitemapManager = $sitemap_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $form = new static($container
      ->get('config.factory'), $container
      ->get('plugin.manager.sitemap'));
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'sitemap_settings';
  }

  /**
   * {@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' => 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.'),
    ];

    // Retrieve stored configuration for the plugins.
    $plugins = $config
      ->get('plugins');
    $plugin_config = [];

    // Create plugin instances for all available Sitemap plugins, including both
    // enabled/configured ones as well as new and not yet configured ones.
    $definitions = $this->sitemapManager
      ->getDefinitions();
    foreach ($definitions as $id => $definition) {
      if ($this->sitemapManager
        ->hasDefinition($id)) {
        if (!empty($plugins[$id])) {
          $plugin_config = $plugins[$id];
        }
        $this->plugins[$id] = $this->sitemapManager
          ->createInstance($id, $plugin_config);
      }
    }

    // Plugin status.
    $form['plugins']['enabled'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Enabled plugins'),
      '#prefix' => '<div id="sitemap-enabled-wrapper">',
      '#suffix' => '</div>',
      // This item is used as a pure wrapping container with heading. Ignore its
      // value, since 'plugins' should only contain plugin definitions.
      // See https://www.drupal.org/node/1829202.
      '#input' => FALSE,
    ];

    // Plugin order (tabledrag).
    $form['plugins']['order'] = [
      '#type' => 'table',
      // For sitemap.admin.js.
      '#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',
      ],
    ];

    // Map settings.
    $form['plugin_settings'] = [
      '#type' => 'vertical_tabs',
      '#title' => $this
        ->t('Plugin settings'),
    ];
    $defaultSort = $this->plugins;
    $sorted = $this
      ->sortPlugins($this->plugins);
    foreach ($sorted as $id => $plugin) {

      /* @var $plugin \Drupal\sitemap\SitemapBase */
      $form['plugins']['enabled'][$id] = [
        '#type' => 'checkbox',
        '#title' => $plugin
          ->getLabel(),
        '#default_value' => $plugin->enabled,
        '#parents' => [
          'plugins',
          $id,
          'enabled',
        ],
        '#description' => $plugin
          ->getDescription(),
        // Default sort groups by plugin type.
        '#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',
          ],
        ],
      ];

      // Retrieve the settings form of the Sitemap plugin.
      $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';

    // Sitemap CSS settings.
    $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);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->configFactory
      ->getEditable('sitemap.settings');

    // Save config.
    foreach ($form_state
      ->getValues() as $key => $value) {
      if ($key == 'plugins') {
        foreach ($value as $instance_id => $plugin_config) {

          // Update the plugin configurations.
          $this->plugins[$instance_id]
            ->setConfiguration($plugin_config);
        }

        // Save in sitemap.settings.
        $config
          ->set($key, $value);
      }
      else {
        $config
          ->set($key, $value);
      }
    }
    $config
      ->save();

    //@TODO Is a more targeted cache cleanup possible?
    drupal_flush_all_caches();
    parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'sitemap.settings',
    ];
  }

  /**
   * Sort the plugins by weight.
   *
   * @param $plugins
   *
   * @return array
   */
  protected function sortPlugins($plugins) {

    // We cannot use array_column here because pluginId is protected.

    //$order = array_column($plugins, 'weight', 'publicId');
    $order = [];
    foreach ($plugins as $id => $plugin) {
      $order[$id] = $plugin->weight;
    }
    asort($order);
    foreach ($order as $id => $weight) {
      $order[$id] = $plugins[$id];
    }
    return $order;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SitemapSettingsForm::$plugins protected property An array of Sitemap plugins.
SitemapSettingsForm::$sitemapManager protected property The SitemapMap plugin manager.
SitemapSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SitemapSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SitemapSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SitemapSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SitemapSettingsForm::sortPlugins protected function Sort the plugins by weight.
SitemapSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SitemapSettingsForm::__construct public function Constructs a SitemapSettingsForm object. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.