You are here

class XmlSitemapLinkBundleSettingsForm in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 src/Form/XmlSitemapLinkBundleSettingsForm.php \Drupal\xmlsitemap\Form\XmlSitemapLinkBundleSettingsForm

Configure what entities will be included in sitemap.

Hierarchy

Expanded class hierarchy of XmlSitemapLinkBundleSettingsForm

1 string reference to 'XmlSitemapLinkBundleSettingsForm'
xmlsitemap.routing.yml in ./xmlsitemap.routing.yml
xmlsitemap.routing.yml

File

src/Form/XmlSitemapLinkBundleSettingsForm.php, line 16

Namespace

Drupal\xmlsitemap\Form
View source
class XmlSitemapLinkBundleSettingsForm extends ConfigFormBase {

  // @codingStandardsIgnoreStart
  private $entity_type;
  private $bundle_type;

  // @codingStandardsIgnoreEnd

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

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

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $entity = NULL, $bundle = NULL) {
    $this->entity_type = $entity;
    $this->bundle_type = $bundle;
    $request = $this
      ->getRequest();
    $form['#title'] = $this
      ->t('@bundle XML sitemap settings', [
      '@bundle' => $bundle,
    ]);
    xmlsitemap_add_link_bundle_settings($form, $form_state, $entity, $bundle);
    $form['xmlsitemap']['#type'] = 'markup';
    $form['xmlsitemap']['#value'] = '';
    $form['xmlsitemap']['#access'] = TRUE;
    $form['xmlsitemap']['#show_message'] = TRUE;
    $destination = $request
      ->get('destination');
    $form['actions']['cancel'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Cancel'),
      '#href' => isset($destination) ? $destination : 'admin/config/search/xmlsitemap/settings',
      '#weight' => 10,
    ];
    $form = parent::buildForm($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $bundle = $form['xmlsitemap']['#bundle'];

    // Handle new bundles by fetching the proper bundle key value from the form
    // state values.
    if (empty($bundle)) {
      $entity_info = $form['xmlsitemap']['#entity_info'];
      if (isset($entity_info['bundle keys']['bundle'])) {
        $bundle_key = $entity_info['bundle keys']['bundle'];
        if ($form_state
          ->hasValue($bundle_key)) {
          $bundle = $form_state
            ->getValue($bundle_key);
          $form['xmlsitemap']['#bundle'] = $bundle;
        }
      }
    }
    $xmlsitemap = $form_state
      ->getValue('xmlsitemap');
    xmlsitemap_link_bundle_settings_save($this->entity_type, $this->bundle_type, $xmlsitemap, TRUE);
    $this->state
      ->set('xmlsitemap_regenerate_needed', TRUE);
    $entity_info = $form['xmlsitemap']['#entity_info'];
    if (!empty($form['xmlsitemap']['#show_message'])) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('XML sitemap settings for the %bundle have been saved.', [
        '%bundle' => $entity_info['bundles'][$bundle]['label'],
      ]));
    }

    // Unset the form values since we have already saved the bundle settings and
    // we don't want these values to get saved as configuration, depending on
    // how the form saves the form values.
    $form_state
      ->unsetValue('xmlsitemap');
    $form_state
      ->setRedirect('xmlsitemap.admin_settings');
    parent::submitForm($form, $form_state);
  }

}

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.
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.
XmlSitemapLinkBundleSettingsForm::$bundle_type private property
XmlSitemapLinkBundleSettingsForm::$entity_type private property
XmlSitemapLinkBundleSettingsForm::$state protected property The state system.
XmlSitemapLinkBundleSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
XmlSitemapLinkBundleSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
XmlSitemapLinkBundleSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
XmlSitemapLinkBundleSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
XmlSitemapLinkBundleSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
XmlSitemapLinkBundleSettingsForm::__construct public function Constructs a XmlSitemapLinkBundleSettingsForm object. Overrides ConfigFormBase::__construct