You are here

class XmlSitemapEnginesSettingsForm in XML sitemap 8

Same name and namespace in other branches
  1. 2.x xmlsitemap_engines/src/Form/XmlSitemapEnginesSettingsForm.php \Drupal\xmlsitemap_engines\Form\XmlSitemapEnginesSettingsForm

Configure xmlsitemap engines settings for this site.

Hierarchy

Expanded class hierarchy of XmlSitemapEnginesSettingsForm

1 string reference to 'XmlSitemapEnginesSettingsForm'
xmlsitemap_engines.routing.yml in xmlsitemap_engines/xmlsitemap_engines.routing.yml
xmlsitemap_engines/xmlsitemap_engines.routing.yml

File

xmlsitemap_engines/src/Form/XmlSitemapEnginesSettingsForm.php, line 17

Namespace

Drupal\xmlsitemap_engines\Form
View source
class XmlSitemapEnginesSettingsForm extends ConfigFormBase {

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $date;

  /**
   * Constructs a new XmlSitemapCustomAddForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date
   *   The date service.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state store service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date, StateInterface $state) {
    parent::__construct($config_factory);
    $this->date = $date;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('date.formatter'), $container
      ->get('state'));
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Build the list of support engines for the checkboxes options.
    $engines = xmlsitemap_engines_get_engine_info();
    $engine_options = [];
    foreach ($engines as $engine => $engine_info) {
      $engine_options[$engine] = $engine_info['name'];
    }
    asort($engine_options);
    $form['engines'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Submit the sitemap to the following engines'),
      '#default_value' => $this
        ->config('xmlsitemap_engines.settings')
        ->get('engines'),
      '#options' => $engine_options,
    ];
    $lifetimes = [
      3600,
      10800,
      21600,
      32400,
      43200,
      86400,
      172800,
      259200,
      604800,
      604800 * 2,
      604800 * 4,
    ];
    $lifetimes = array_combine($lifetimes, $lifetimes);
    $format_lifetimes = [];
    foreach ($lifetimes as $value) {
      $format_lifetimes[$value] = $this->date
        ->formatInterval($value);
    }
    $form['minimum_lifetime'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Do not submit more often than every'),
      '#options' => $format_lifetimes,
      '#default_value' => $this
        ->config('xmlsitemap_engines.settings')
        ->get('minimum_lifetime'),
    ];
    $form['xmlsitemap_engines_submit_updated'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Only submit if the sitemap has been updated since the last submission.'),
      '#default_value' => $this->state
        ->get('xmlsitemap_engines_submit_updated'),
    ];
    $form['custom_urls'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Custom submission URLs'),
      '#description' => $this
        ->t('Enter one URL per line. The token [sitemap] will be replaced with the URL to your sitemap. For example: %example-before would become %example-after.', [
        '%example-before' => 'http://example.com/ping?[sitemap]',
        '%example-after' => xmlsitemap_engines_prepare_url('http://example.com/ping?[sitemap]', Url::fromRoute('xmlsitemap.sitemap_xml', [], [
          'absolute' => TRUE,
        ])
          ->toString()),
      ]),
      '#default_value' => $this
        ->config('xmlsitemap_engines.settings')
        ->get('custom_urls'),
      '#rows' => 2,
      '#wysiwyg' => FALSE,
    ];

    // Ensure the xmlsitemap_engines variable gets filtered to a simple array.
    $form['array_filter'] = [
      '#type' => 'value',
      '#value' => TRUE,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    $custom_urls = $form_state
      ->getValue('custom_urls');
    $custom_urls = preg_split('/[\\r\\n]+/', $custom_urls, -1, PREG_SPLIT_NO_EMPTY);
    foreach ($custom_urls as $custom_url) {
      $url = xmlsitemap_engines_prepare_url($custom_url, '');
      if (!UrlHelper::isValid($url, TRUE)) {
        $form_state
          ->setErrorByName($custom_url, $this
          ->t('Invalid URL %url.', [
          '%url' => $custom_url,
        ]));
      }
    }
    $custom_urls = implode("\n", $custom_urls);
    $form_state
      ->setValue('custom_urls', $custom_urls);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $state_variables = xmlsitemap_engines_state_variables();
    $keys = [
      'engines',
      'minimum_lifetime',
      'xmlsitemap_engines_submit_updated',
      'custom_urls',
    ];
    $config = $this
      ->config('xmlsitemap_engines.settings');
    $values = $form_state
      ->getValues();
    foreach ($keys as $key) {
      if (isset($state_variables[$key])) {
        $this->state
          ->set($key, $values[$key]);
      }
      else {
        $config
          ->set($key, $values[$key]);
      }
    }
    $config
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
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. 1
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. Overrides UrlGeneratorTrait::redirect
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.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
XmlSitemapEnginesSettingsForm::$date protected property The date formatter service.
XmlSitemapEnginesSettingsForm::$state protected property The state service.
XmlSitemapEnginesSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
XmlSitemapEnginesSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
XmlSitemapEnginesSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
XmlSitemapEnginesSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
XmlSitemapEnginesSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
XmlSitemapEnginesSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
XmlSitemapEnginesSettingsForm::__construct public function Constructs a new XmlSitemapCustomAddForm object. Overrides ConfigFormBase::__construct