You are here

class MetatagDefaultsForm in Metatag 8

Class MetatagDefaultsForm.

@package Drupal\metatag\Form

Hierarchy

Expanded class hierarchy of MetatagDefaultsForm

File

src/Form/MetatagDefaultsForm.php, line 23

Namespace

Drupal\metatag\Form
View source
class MetatagDefaultsForm extends EntityForm {

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The Metatag manager service.
   *
   * @var \Drupal\metatag\MetatagManagerInterface
   */
  protected $metatagManager;

  /**
   * The Metatag token service.
   *
   * @var \Drupal\metatag\MetatagToken
   */
  protected $metatagToken;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The Metatag tag plugin manager service.
   *
   * @var \Drupal\metatag\MetatagTagPluginManager
   */
  protected $metatagPluginManager;

  /**
   * Constructs a new MetatagDefaultsForm.
   *
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\metatag\MetatagManagerInterface $metatag_manager
   *   The Metatag manager service.
   * @param \Drupal\metatag\MetatagToken $metatag_token
   *   The Metatag token service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\metatag\MetatagTagPluginManager $metatag_plugin_manager
   *   The Metatag tag plugin manager service.
   */
  public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info, MetatagManagerInterface $metatag_manager, MetatagToken $metatag_token, ModuleHandlerInterface $module_handler, MetatagTagPluginManager $metatag_plugin_manager) {
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    $this->metatagManager = $metatag_manager;
    $this->metatagToken = $metatag_token;
    $this->moduleHandler = $module_handler;
    $this->metatagPluginManager = $metatag_plugin_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.bundle.info'), $container
      ->get('metatag.manager'), $container
      ->get('metatag.token'), $container
      ->get('module_handler'), $container
      ->get('plugin.manager.metatag.tag'));
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $metatag_defaults = $this->entity;
    $form['#ajax_wrapper_id'] = 'metatag-defaults-form-ajax-wrapper';
    $ajax = [
      'wrapper' => $form['#ajax_wrapper_id'],
      'callback' => '::rebuildForm',
    ];
    $form['#prefix'] = '<div id="' . $form['#ajax_wrapper_id'] . '">';
    $form['#suffix'] = '</div>';
    $default_type = NULL;
    if (!empty($metatag_defaults)) {
      $default_type = $metatag_defaults
        ->getOriginalId();
    }
    else {
      $form_state
        ->set('default_type', $default_type);
    }
    $token_types = empty($default_type) ? [] : [
      explode('__', $default_type)[0],
    ];

    // Add the token browser at the top.
    $form += $this->metatagToken
      ->tokenBrowser($token_types);

    // If this is a new Metatag defaults, then list available bundles.
    if ($metatag_defaults
      ->isNew()) {
      $options = $this
        ->getAvailableBundles();
      $form['id'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Type'),
        '#description' => $this
          ->t('Select the type of default meta tags you would like to add.'),
        '#options' => $options,
        '#required' => TRUE,
        '#default_value' => $default_type,
        '#ajax' => $ajax + [
          'trigger_as' => [
            'name' => 'select_id_submit',
          ],
        ],
      ];
      $form['select_id_submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Submit'),
        '#name' => 'select_id_submit',
        '#ajax' => $ajax,
        '#attributes' => [
          'class' => [
            'js-hide',
          ],
        ],
      ];
      $values = [];
    }
    else {
      $values = $metatag_defaults
        ->get('tags');
    }

    // Retrieve configuration settings.
    $settings = $this
      ->config('metatag.settings');
    $entity_type_groups = $settings
      ->get('entity_type_groups');

    // Find the current entity type and bundle.
    $metatag_defaults_id = $metatag_defaults
      ->id();
    $type_parts = explode('__', $metatag_defaults_id);
    $entity_type = $type_parts[0];
    $entity_bundle = isset($type_parts[1]) ? $type_parts[1] : NULL;

    // See if there are requested groups for this entity type and bundle.
    $groups = !empty($entity_type_groups[$entity_type]) && !empty($entity_type_groups[$entity_type][$entity_bundle]) ? $entity_type_groups[$entity_type][$entity_bundle] : [];

    // Limit the form to requested groups, if any.
    if (!empty($groups)) {
      $form = $this->metatagManager
        ->form($values, $form, [
        $entity_type,
      ], $groups, NULL, TRUE);
    }
    else {
      $form = $this->metatagManager
        ->form($values, $form, [], NULL, NULL, TRUE);
    }
    $form['status'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Active'),
      '#default_value' => $metatag_defaults
        ->status(),
    ];
    if ($metatag_defaults_id === 'global') {

      // Disabling global prevents any metatags from working.
      // Warn users about this.
      $form['status']['#description'] = $this
        ->t('Warning: disabling the Global default metatag will prevent any metatags from being used.');
    }
    return $form;
  }

  /**
   * Ajax form submit handler that will return the whole rebuilt form.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The form structure.
   */
  public function rebuildForm(array &$form, FormStateInterface $form_state) {
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    if (isset($actions['delete'])) {
      $actions['delete']['#access'] = $actions['delete']['#access'] && !in_array($this->entity
        ->id(), MetatagManager::protectedDefaults());
    }
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getTriggeringElement()['#name'] == 'select_id_submit') {
      $form_state
        ->set('default_type', $form_state
        ->getValue('id'));
      $form_state
        ->setRebuild();
    }
    else {
      parent::submitForm($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $metatag_defaults = $this->entity;
    $metatag_defaults
      ->setStatus($form_state
      ->getValue('status'));

    // Set the label on new defaults.
    if ($metatag_defaults
      ->isNew()) {
      $metatag_defaults_id = $form_state
        ->getValue('id');
      $type_parts = explode('__', $metatag_defaults_id);
      $entity_type = $type_parts[0];
      $entity_bundle = isset($type_parts[1]) ? $type_parts[1] : NULL;

      // Get the entity label.
      $entity_info = $this->entityTypeManager
        ->getDefinitions();
      $entity_label = (string) $entity_info[$entity_type]
        ->get('label');
      if (!is_null($entity_bundle)) {

        // Get the bundle label.
        $bundle_info = $this->entityTypeBundleInfo
          ->getBundleInfo($entity_type);
        if ($entity_type === 'page_variant') {

          // Check if page manager is enabled and try to load the page variant
          // so the label of the variant can be used.
          if ($this->moduleHandler
            ->moduleExists('metatag_page_manager')) {
            $page_variant = PageVariant::load($entity_bundle);
            $page = $page_variant
              ->getPage();
            if ($page_variant) {
              $entity_label .= ': ' . $page
                ->label() . ': ' . $page_variant
                ->label();
            }
          }
        }
        else {
          $entity_label .= ': ' . $bundle_info[$entity_bundle]['label'];
        }
      }

      // Set the label to the config entity.
      $this->entity
        ->set('label', $entity_label);
    }

    // Set tags within the Metatag entity.
    $tags = $this->metatagPluginManager
      ->getDefinitions();
    $tag_values = [];
    foreach ($tags as $tag_id => $tag_definition) {
      if ($form_state
        ->hasValue($tag_id)) {

        // Some plugins need to process form input before storing it. Hence, we
        // set it and then get it.
        $tag = $this->metatagPluginManager
          ->createInstance($tag_id);
        $tag
          ->setValue($form_state
          ->getValue($tag_id));
        if (!empty($tag
          ->value())) {
          $tag_values[$tag_id] = $tag
            ->value();
        }
      }
    }
    $metatag_defaults
      ->set('tags', $tag_values);
    $status = $metatag_defaults
      ->save();
    switch ($status) {
      case SAVED_NEW:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Created the %label Metatag defaults.', [
          '%label' => $metatag_defaults
            ->label(),
        ]));
        break;
      default:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Saved the %label Metatag defaults.', [
          '%label' => $metatag_defaults
            ->label(),
        ]));
    }
    $form_state
      ->setRedirectUrl($metatag_defaults
      ->toUrl('collection'));
  }

  /**
   * Returns an array of available bundles to override.
   *
   * @return array
   *   A list of available bundles as $id => $label.
   */
  protected function getAvailableBundles() {
    $options = [];
    $entity_types = static::getSupportedEntityTypes();
    $metatags_defaults_manager = $this->entityTypeManager
      ->getStorage('metatag_defaults');
    foreach ($entity_types as $entity_type => $entity_label) {
      if (empty($metatags_defaults_manager
        ->load($entity_type))) {
        $options[$entity_label][$entity_type] = "{$entity_label} (Default)";
      }
      $bundles = $this->entityTypeBundleInfo
        ->getBundleInfo($entity_type);
      foreach ($bundles as $bundle_id => $bundle_metadata) {
        $metatag_defaults_id = $entity_type . '__' . $bundle_id;
        if (empty($metatags_defaults_manager
          ->load($metatag_defaults_id))) {
          $options[$entity_label][$metatag_defaults_id] = $bundle_metadata['label'];
        }
      }
    }
    return $options;
  }

  /**
   * Returns a list of supported entity types.
   *
   * @return array
   *   A list of available entity types as $machine_name => $label.
   */
  public static function getSupportedEntityTypes() {
    $entity_types = [];

    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_type_manager = \Drupal::service('entity_type.manager');

    // A list of entity types that are not supported.
    $unsupported_types = [
      // Custom blocks.
      'block_content',
      // Comments.
      'comment',
      // Contact messages are the messages submitted on individual contact forms
      // so obviously shouldn't get meta tags.
      'contact_message',
      // Menu items.
      'menu_link_content',
      // Shortcut items.
      'shortcut',
    ];

    // Make a list of supported content types.
    foreach ($entity_type_manager
      ->getDefinitions() as $entity_name => $definition) {

      // Skip some entity types that we don't want to support.
      if (in_array($entity_name, $unsupported_types)) {
        continue;
      }

      // Identify supported entities.
      if ($definition instanceof ContentEntityType) {

        // Only work with entity types that have a list of links, i.e. publicly
        // viewable.
        $links = $definition
          ->get('links');
        if (!empty($links)) {
          $entity_types[$entity_name] = static::getEntityTypeLabel($definition);
        }
      }
    }
    return $entity_types;
  }

  /**
   * Returns the text label for the entity type specified.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entityType
   *   The entity type to process.
   *
   * @return string
   *   A label.
   */
  public static function getEntityTypeLabel(EntityTypeInterface $entityType) {
    $label = $entityType
      ->getLabel();
    if (is_a($label, 'Drupal\\Core\\StringTranslation\\TranslatableMarkup')) {

      /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
      $label = $label
        ->render();
    }
    return $label;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
EntityForm::$entity protected property The entity being used by this form. 7
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 2
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 10
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties 7
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::__get public function
EntityForm::__set public function
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
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.
MetatagDefaultsForm::$entityTypeBundleInfo protected property The entity type bundle info service.
MetatagDefaultsForm::$metatagManager protected property The Metatag manager service.
MetatagDefaultsForm::$metatagPluginManager protected property The Metatag tag plugin manager service.
MetatagDefaultsForm::$metatagToken protected property The Metatag token service.
MetatagDefaultsForm::$moduleHandler protected property The module handler service. Overrides EntityForm::$moduleHandler
MetatagDefaultsForm::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
MetatagDefaultsForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
MetatagDefaultsForm::form public function Gets the actual form array to be built. Overrides EntityForm::form
MetatagDefaultsForm::getAvailableBundles protected function Returns an array of available bundles to override.
MetatagDefaultsForm::getEntityTypeLabel public static function Returns the text label for the entity type specified.
MetatagDefaultsForm::getSupportedEntityTypes public static function Returns a list of supported entity types.
MetatagDefaultsForm::rebuildForm public function Ajax form submit handler that will return the whole rebuilt form.
MetatagDefaultsForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
MetatagDefaultsForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides EntityForm::submitForm
MetatagDefaultsForm::__construct public function Constructs a new MetatagDefaultsForm.
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.