You are here

class SimplesitemapEntitiesForm in Simple XML sitemap 8.2

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

Class SimplesitemapEntitiesForm @package Drupal\simple_sitemap\Form

Hierarchy

Expanded class hierarchy of SimplesitemapEntitiesForm

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

File

src/Form/SimplesitemapEntitiesForm.php, line 11

Namespace

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['simple_sitemap_entities']['#prefix'] = $this
      ->getDonationText();
    $form['simple_sitemap_entities']['entities'] = [
      '#title' => $this
        ->t('Sitemap entities'),
      '#type' => 'fieldset',
      '#markup' => '<p>' . $this
        ->t('Simple XML sitemap settings will be added only to entity forms of entity types enabled here. For all entity types featuring bundles (e.g. <em>node</em>) sitemap settings have to be set on their bundle pages (e.g. <em>page</em>).') . '</p>',
    ];
    $form['#attached']['library'][] = 'simple_sitemap/sitemapEntities';
    $form['#attached']['drupalSettings']['simple_sitemap'] = [
      'all_entities' => [],
      'atomic_entities' => [],
    ];
    $entity_type_labels = [];
    foreach ($this->entityHelper
      ->getSupportedEntityTypes() as $entity_type_id => $entity_type) {
      $entity_type_labels[$entity_type_id] = $entity_type
        ->getLabel() ?: $entity_type_id;
    }
    asort($entity_type_labels);
    $this->formHelper
      ->processForm($form_state);
    $bundle_settings = $this->generator
      ->getBundleSettings();
    foreach ($entity_type_labels as $entity_type_id => $entity_type_label) {
      $enabled_entity_type = $this->generator
        ->entityTypeIsEnabled($entity_type_id);
      $atomic_entity_type = $this->entityHelper
        ->entityTypeIsAtomic($entity_type_id);
      $css_entity_type_id = str_replace('_', '-', $entity_type_id);
      $form['simple_sitemap_entities']['entities'][$entity_type_id] = [
        '#type' => 'details',
        '#title' => $entity_type_label,
        '#open' => $enabled_entity_type,
      ];
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable @entity_type_label <em>(@entity_type_id)</em> support', [
          '@entity_type_label' => strtolower($entity_type_label),
          '@entity_type_id' => $entity_type_id,
        ]),
        '#description' => $atomic_entity_type ? $this
          ->t('Sitemap settings for the entity type <em>@entity_type_label</em> can be set below and overridden on its entity pages.', [
          '@entity_type_label' => strtolower($entity_type_label),
        ]) : $this
          ->t('Sitemap settings for the entity type <em>@entity_type_label</em> can be set on its bundle pages and overridden on its entity pages.', [
          '@entity_type_label' => strtolower($entity_type_label),
        ]),
        '#default_value' => $enabled_entity_type,
      ];
      if ($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#default_value']) {
        $bundle_info = '';
        $indexed_bundles = isset($bundle_settings[$entity_type_id]) ? implode(array_keys(array_filter($bundle_settings[$entity_type_id], function ($val) {
          return $val['index'];
        })), ', ') : '';
        if (!$atomic_entity_type) {
          $bundle_info .= '<div id="indexed-bundles-' . $css_entity_type_id . '">' . (!empty($indexed_bundles) ? $this
            ->t("<em>@entity_type_label</em> bundles set to be indexed:", [
            '@entity_type_label' => ucfirst(strtolower($entity_type_label)),
          ]) . ' ' . '<em>' . $indexed_bundles . '</em>' : $this
            ->t('No <em>@entity_type_label</em> bundles are set to be indexed yet.', [
            '@entity_type_label' => strtolower($entity_type_label),
          ])) . '</div>';
        }
        if (!empty($indexed_bundles)) {
          $bundle_info .= '<div id="warning-' . $css_entity_type_id . '">' . ($atomic_entity_type ? $this
            ->t("<strong>Warning:</strong> This entity type's sitemap settings including per-entity overrides will be deleted after hitting <em>Save</em>.") : $this
            ->t("<strong>Warning:</strong> The sitemap settings for <em>@bundles</em> and any per-entity overrides will be deleted after hitting <em>Save</em>.", [
            '@bundles' => $indexed_bundles,
          ])) . '</div>';
        }
        $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#suffix'] = $bundle_info;
      }
      $form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = $css_entity_type_id;
      if ($atomic_entity_type) {
        $this->formHelper
          ->setEntityCategory('bundle')
          ->setEntityTypeId($entity_type_id)
          ->setBundleName($entity_type_id)
          ->displayEntitySettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
        $form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = $css_entity_type_id;
      }
    }
    $this->formHelper
      ->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    foreach ($values as $field_name => $value) {
      if (substr($field_name, -strlen('_enabled')) === '_enabled') {
        $entity_type_id = substr($field_name, 0, -8);
        if ($value) {
          $this->generator
            ->enableEntityType($entity_type_id);
          if ($this->entityHelper
            ->entityTypeIsAtomic($entity_type_id)) {
            $this->generator
              ->setBundleSettings($entity_type_id, $entity_type_id, [
              'index' => TRUE,
              'priority' => $values[$entity_type_id . '_simple_sitemap_priority'],
              'changefreq' => $values[$entity_type_id . '_simple_sitemap_changefreq'],
              'include_images' => $values[$entity_type_id . '_simple_sitemap_include_images'],
            ]);
          }
        }
        else {
          $this->generator
            ->disableEntityType($entity_type_id);
        }
      }
    }
    parent::submitForm($form, $form_state);

    // Regenerate sitemaps according to user setting.
    if ($form_state
      ->getValue('simple_sitemap_regenerate_now')) {
      $this->generator
        ->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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.
SimplesitemapEntitiesForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SimplesitemapEntitiesForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SimplesitemapEntitiesForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SimplesitemapFormBase::$entityHelper protected property
SimplesitemapFormBase::$formHelper protected property
SimplesitemapFormBase::$generator protected property
SimplesitemapFormBase::$languageManager protected property
SimplesitemapFormBase::$pathValidator protected property
SimplesitemapFormBase::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SimplesitemapFormBase::getDonationText protected function
SimplesitemapFormBase::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SimplesitemapFormBase::__construct public function SimplesitemapFormBase constructor. Overrides ConfigFormBase::__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.