You are here

class SimplesitemapSettingsForm in Simple XML sitemap 8.3

Same name and namespace in other branches
  1. 8.2 src/Form/SimplesitemapSettingsForm.php \Drupal\simple_sitemap\Form\SimplesitemapSettingsForm

Class SimplesitemapSettingsForm @package Drupal\simple_sitemap\Form

Hierarchy

Expanded class hierarchy of SimplesitemapSettingsForm

1 string reference to 'SimplesitemapSettingsForm'
simple_sitemap.routing.yml in ./simple_sitemap.routing.yml
simple_sitemap.routing.yml

File

src/Form/SimplesitemapSettingsForm.php, line 15

Namespace

Drupal\simple_sitemap\Form
View source
class SimplesitemapSettingsForm extends SimplesitemapFormBase {

  /**
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * SimplesitemapSettingsForm constructor.
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
   * @param \Drupal\simple_sitemap\Form\FormHelper $form_helper
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   */
  public function __construct(Simplesitemap $generator, FormHelper $form_helper, LanguageManagerInterface $language_manager) {
    parent::__construct($generator, $form_helper);
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('simple_sitemap.generator'), $container
      ->get('simple_sitemap.form_helper'), $container
      ->get('language_manager'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['simple_sitemap_settings']['#prefix'] = FormHelper::getDonationText();
    $form['simple_sitemap_settings']['settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Settings'),
    ];
    $form['simple_sitemap_settings']['settings']['cron_generate'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Regenerate the sitemaps during cron runs'),
      '#description' => $this
        ->t('Uncheck this if you intend to only regenerate the sitemaps manually or via drush.'),
      '#default_value' => $this->generator
        ->getSetting('cron_generate', TRUE),
    ];
    $form['simple_sitemap_settings']['settings']['cron_generate_interval'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Sitemap generation interval'),
      '#description' => $this
        ->t('The sitemap will be generated according to this interval.'),
      '#default_value' => $this->generator
        ->getSetting('cron_generate_interval', 0),
      '#options' => FormHelper::getCronIntervalOptions(),
      '#states' => [
        'visible' => [
          ':input[name="cron_generate"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['simple_sitemap_settings']['settings']['xsl'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add styling and sorting to sitemaps'),
      '#description' => $this
        ->t('If checked, sitemaps will be displayed as tables with sortable entries and thus become much friendlier towards human visitors. Search engines will not care.'),
      '#default_value' => $this->generator
        ->getSetting('xsl', TRUE),
    ];
    $form['simple_sitemap_settings']['settings']['languages'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Language settings'),
      '#open' => FALSE,
    ];
    $form['simple_sitemap_settings']['settings']['languages']['disable_language_hreflang'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Remove hreflang markup in HTML'),
      '#description' => $this
        ->t('Google recommends displaying hreflang definitions either in the HTML markup or in the sitemap, but not in both places.<br>If checked, hreflang definitions created by the language module will be removed from the markup reducing its size.'),
      '#default_value' => $this->generator
        ->getSetting('disable_language_hreflang', FALSE),
    ];
    $form['simple_sitemap_settings']['settings']['languages']['skip_untranslated'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Skip non-existent translations'),
      '#description' => $this
        ->t('If checked, entity links are generated exclusively for languages the entity has been translated to as long as the language is not excluded below.<br>Otherwise entity links are generated for every language installed on the site apart from languages excluded below.<br>Bear in mind that non-entity paths like homepage will always be generated for every non-excluded language.'),
      '#default_value' => $this->generator
        ->getSetting('skip_untranslated', FALSE),
    ];
    $language_options = [];
    foreach ($this->languageManager
      ->getLanguages() as $language) {
      if (!$language
        ->isDefault()) {
        $language_options[$language
          ->getId()] = $language
          ->getName();
      }
    }
    $form['simple_sitemap_settings']['settings']['languages']['excluded_languages'] = [
      '#title' => $this
        ->t('Exclude languages'),
      '#type' => 'checkboxes',
      '#options' => $language_options,
      '#description' => !empty($language_options) ? $this
        ->t('There will be no links generated for languages checked here.') : $this
        ->t('There are no languages other than the default language <a href="@url">available</a>.', [
        '@url' => $GLOBALS['base_url'] . '/admin/config/regional/language',
      ]),
      '#default_value' => $this->generator
        ->getSetting('excluded_languages', []),
    ];
    $form['simple_sitemap_settings']['advanced'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced settings'),
      '#open' => TRUE,
    ];
    $variants = $this->generator
      ->getSitemapManager()
      ->getSitemapVariants(NULL, FALSE);
    $default_variant = $this->generator
      ->getSetting('default_variant');
    $form['simple_sitemap_settings']['advanced']['default_variant'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default sitemap variant'),
      '#description' => $this
        ->t('This sitemap variant will be available under <em>/sitemap.xml</em> in addition to its default path <em>/variant-name/sitemap.xml</em>.<br>Variants can be configured <a href="@url">here</a>.', [
        '@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/variants',
      ]),
      '#default_value' => isset($variants[$default_variant]) ? $default_variant : '',
      '#options' => [
        '' => $this
          ->t('- None -'),
      ] + array_map(function ($variant) {
        return $this
          ->t($variant['label']);
      }, $variants),
    ];
    $form['simple_sitemap_settings']['advanced']['base_url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Default base URL'),
      '#default_value' => $this->generator
        ->getSetting('base_url', ''),
      '#size' => 30,
      '#description' => $this
        ->t('On some hosting providers it is impossible to pass parameters to cron to tell Drupal which URL to bootstrap with. In this case the base URL of sitemap links can be overridden here.<br>Example: <em>@url</em>', [
        '@url' => $GLOBALS['base_url'],
      ]),
    ];
    $form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Exclude duplicate links'),
      '#description' => $this
        ->t('Prevent per-sitemap variant duplicate links.<br>Unchecking this may help avoiding PHP memory errors on huge sites.'),
      '#default_value' => $this->generator
        ->getSetting('remove_duplicates', TRUE),
    ];
    $form['simple_sitemap_settings']['advanced']['max_links'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Maximum links in a sitemap'),
      '#min' => 1,
      '#description' => $this
        ->t('The maximum number of links one sitemap can hold. If more links are generated than set here, a sitemap index will be created and the links split into several sub-sitemaps.<br>50 000 links is the maximum Google will parse per sitemap, but choosing a lower value may be needed to avoid PHP memory errors on huge sites.<br>If left blank, all links will be shown on a single sitemap.'),
      '#default_value' => $this->generator
        ->getSetting('max_links'),
    ];
    $form['simple_sitemap_settings']['advanced']['generate_duration'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Sitemap generation max duration'),
      '#min' => 1,
      '#description' => $this
        ->t('The maximum duration <strong>in seconds</strong> the generation task can run during a single cron run or during one batch process iteration.<br>The higher the number, the quicker the generation process, but higher the risk of PHP timeout errors.'),
      '#default_value' => $this->generator
        ->getSetting('generate_duration', 10000) / 1000,
      '#required' => TRUE,
    ];
    $form['simple_sitemap_settings']['advanced']['entities_per_queue_item'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Entities per queue item'),
      '#min' => 1,
      '#description' => $this
        ->t('The number of entities to process in each queue item.<br>Increasing this number will use more memory but will result in less queries improving generation speed.'),
      '#default_value' => $this->generator
        ->getSetting('entities_per_queue_item', 50),
    ];
    $this->formHelper
      ->displayRegenerateNow($form['simple_sitemap_settings']);
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $base_url = $form_state
      ->getValue('base_url');
    $form_state
      ->setValue('base_url', rtrim($base_url, '/'));
    if ($base_url !== '' && !UrlHelper::isValid($base_url, TRUE)) {
      $form_state
        ->setErrorByName('base_url', $this
        ->t('The base URL is invalid.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    foreach ([
      'max_links',
      'cron_generate',
      'cron_generate_interval',
      'remove_duplicates',
      'skip_untranslated',
      'xsl',
      'base_url',
      'default_variant',
      'disable_language_hreflang',
      'entities_per_queue_item',
    ] as $setting_name) {
      $this->generator
        ->saveSetting($setting_name, $form_state
        ->getValue($setting_name));
    }
    $this->generator
      ->saveSetting('excluded_languages', array_filter($form_state
      ->getValue('excluded_languages')));
    $this->generator
      ->saveSetting('generate_duration', $form_state
      ->getValue('generate_duration') * 1000);
    parent::submitForm($form, $form_state);

    // Regenerate sitemaps according to user setting.
    if ($form_state
      ->getValue('simple_sitemap_regenerate_now')) {
      $this->generator
        ->setVariants(TRUE)
        ->rebuildQueue()
        ->generateSitemap();
    }
  }

}

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.
SimplesitemapFormBase::$formHelper protected property
SimplesitemapFormBase::$generator protected property
SimplesitemapFormBase::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SimplesitemapSettingsForm::$languageManager protected property
SimplesitemapSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SimplesitemapSettingsForm::create public static function Instantiates a new instance of this class. Overrides SimplesitemapFormBase::create
SimplesitemapSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SimplesitemapSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SimplesitemapSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
SimplesitemapSettingsForm::__construct public function SimplesitemapSettingsForm constructor. Overrides SimplesitemapFormBase::__construct
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.