Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / metatag / src / MetatagManager.php
index 170f33d6b4662c789b0c78e36f08152fa91b3608..5f04a088082615369c8abca2d278350d067d4234 100644 (file)
@@ -4,10 +4,9 @@ namespace Drupal\metatag;
 
 use Drupal\Component\Render\PlainTextOutput;
 use Drupal\Core\Entity\ContentEntityInterface;
-use Drupal\Core\Entity\EntityTypeManager;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Language\LanguageInterface;
 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
-use Drupal\field\Entity\FieldConfig;
 use Drupal\metatag\Entity\MetatagDefaults;
 use Drupal\views\ViewEntityInterface;
 
@@ -18,9 +17,24 @@ use Drupal\views\ViewEntityInterface;
  */
 class MetatagManager implements MetatagManagerInterface {
 
+  /**
+   * @var \Drupal\metatag\MetatagGroupPluginManager
+   */
   protected $groupPluginManager;
+
+  /**
+   * @var \Drupal\metatag\MetatagTagPluginManager
+   */
   protected $tagPluginManager;
+
+  /**
+   * @var array
+   */
   protected $metatagDefaults;
+
+  /**
+   * @var \Drupal\metatag\MetatagToken
+   */
   protected $tokenService;
 
   /**
@@ -33,17 +47,22 @@ class MetatagManager implements MetatagManagerInterface {
   /**
    * Constructor for MetatagManager.
    *
-   * @param MetatagGroupPluginManager $groupPluginManager
-   * @param MetatagTagPluginManager $tagPluginManager
-   * @param MetatagToken $token
-   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $channelFactory
-   * @param EntityTypeManager $entityTypeManager
+   * @param Drupal\metatag\MetatagGroupPluginManager $groupPluginManager
+   *   The MetatagGroupPluginManager object.
+   * @param Drupal\metatag\MetatagTagPluginManager $tagPluginManager
+   *   The MetatagTagPluginMÏ€anager object.
+   * @param Drupal\metatag\MetatagToken $token
+   *   The MetatagToken object.
+   * @param Drupal\Core\Logger\LoggerChannelFactoryInterface $channelFactory
+   *   The LoggerChannelFactoryInterface object.
+   * @param Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
+   *   The EntityTypeManagerInterface object.
    */
   public function __construct(MetatagGroupPluginManager $groupPluginManager,
-                              MetatagTagPluginManager $tagPluginManager,
-                              MetatagToken $token,
-                              LoggerChannelFactoryInterface $channelFactory,
-                              EntityTypeManager $entityTypeManager) {
+      MetatagTagPluginManager $tagPluginManager,
+      MetatagToken $token,
+      LoggerChannelFactoryInterface $channelFactory,
+      EntityTypeManagerInterface $entityTypeManager) {
     $this->groupPluginManager = $groupPluginManager;
     $this->tagPluginManager = $tagPluginManager;
     $this->tokenService = $token;
@@ -51,6 +70,24 @@ class MetatagManager implements MetatagManagerInterface {
     $this->metatagDefaults = $entityTypeManager->getStorage('metatag_defaults');
   }
 
+  /**
+   * Returns the list of protected defaults.
+   *
+   * @return array
+   *   Th protected defaults.
+   */
+  public static function protectedDefaults() {
+    return [
+      'global',
+      '403',
+      '404',
+      'node',
+      'front',
+      'taxonomy_term',
+      'user',
+    ];
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -59,7 +96,7 @@ class MetatagManager implements MetatagManagerInterface {
 
     $fields = $this->getFields($entity);
 
-    /* @var FieldConfig $field_info */
+    /* @var \Drupal\field\Entity\FieldConfig $field_info */
     foreach ($fields as $field_name => $field_info) {
       // Get the tags from this field.
       $tags = $this->getFieldTags($entity, $field_name);
@@ -79,7 +116,7 @@ class MetatagManager implements MetatagManagerInterface {
    * {@inheritdoc}
    */
   public function defaultTagsFromEntity(ContentEntityInterface $entity) {
-    /** @var MetatagDefaults $metatags */
+    /** @var \Drupal\metatag\Entity\MetatagDefaults $metatags */
     $metatags = $this->metatagDefaults->load('global');
     if (!$metatags) {
       return NULL;
@@ -185,7 +222,7 @@ class MetatagManager implements MetatagManagerInterface {
       if (!isset($groups[$tag_group])) {
         // If the tag is claiming a group that has no matching plugin, log an
         // error and force it to the basic group.
-        $this->logger->error("Undefined group '%group' on tag '%tag'", ['%group' => $tag_group, '%tag' => $tag_id]);
+        $this->logger->error("Undefined group '%group' on tag '%tag'", ['%group' => $tag_group, '%tag' => $tag_name]);
         $tag['group'] = 'basic';
         $tag_group = 'basic';
       }
@@ -270,10 +307,12 @@ class MetatagManager implements MetatagManagerInterface {
   }
 
   /**
-   * Returns a list of the metatags with values from a field.
+   * Returns a list of the meta tags with values from a field.
    *
-   * @param ContentEntityInterface $entity
+   * @param Drupal\Core\Entity\ContentEntityInterface $entity
+   *   The ContentEntityInterface object.
    * @param string $field_name
+   *   The name of the field to work on.
    */
   protected function getFieldTags(ContentEntityInterface $entity, $field_name) {
     $tags = [];
@@ -291,10 +330,11 @@ class MetatagManager implements MetatagManagerInterface {
   /**
    *
    *
-   * @param ContentEntityInterface $entity
+   * @param Drupal\Core\Entity\ContentEntityInterface $entity
+   *   The entity to work on.
    */
   public function getDefaultMetatags(ContentEntityInterface $entity = NULL) {
-    // Get general global metatags
+    // Get general global metatags.
     $metatags = $this->getGlobalMetatags();
     // If that is empty something went wrong.
     if (!$metatags) {
@@ -310,14 +350,14 @@ class MetatagManager implements MetatagManagerInterface {
     }
 
     // Next check if there is this page is an entity that has meta tags.
-    // @TODO: Think about using other defaults, e.g. views. Maybe use plugins?
+    // @todo Think about using other defaults, e.g. views. Maybe use plugins?
     else {
       if (is_null($entity)) {
         $entity = metatag_get_route_entity();
       }
 
       if (!empty($entity)) {
-        // Get default metatags for a given entity.
+        // Get default meta tags for a given entity.
         $entity_defaults = $this->getEntityDefaultMetatags($entity);
         if ($entity_defaults != NULL) {
           $metatags->set('tags', array_merge($metatags->get('tags'), $entity_defaults));
@@ -330,6 +370,9 @@ class MetatagManager implements MetatagManagerInterface {
 
   /**
    *
+   *
+   * @return array
+   *   The global meta tags.
    */
   public function getGlobalMetatags() {
     return $this->metatagDefaults->load('global');
@@ -337,6 +380,9 @@ class MetatagManager implements MetatagManagerInterface {
 
   /**
    *
+   *
+   * @return array
+   *   The defaults for this page, if it's a special page.
    */
   public function getSpecialMetatags() {
     $metatags = NULL;
@@ -356,6 +402,12 @@ class MetatagManager implements MetatagManagerInterface {
 
   /**
    *
+   *
+   * @param Drupal\Core\Entity\ContentEntityInterface $entity
+   *   The entity to work with.
+   *
+   * @return array
+   *   The appropriate default meta tags.
    */
   public function getEntityDefaultMetatags(ContentEntityInterface $entity) {
     $entity_metatags = $this->metatagDefaults->load($entity->getEntityTypeId());
@@ -376,8 +428,7 @@ class MetatagManager implements MetatagManagerInterface {
   }
 
   /**
-   * Generate the elements that go in the attached array in
-   * hook_page_attachments.
+   * Generate the elements that go in the hook_page_attachments attached array.
    *
    * @param array $tags
    *   The array of tags as plugin_id => value.
@@ -387,7 +438,7 @@ class MetatagManager implements MetatagManagerInterface {
    * @return array
    *   Render array with tag elements.
    */
-  public function generateElements($tags, $entity = NULL) {
+  public function generateElements(array $tags, $entity = NULL) {
     $elements = [];
     $tags = $this->generateRawElements($tags, $entity);
 
@@ -414,7 +465,7 @@ class MetatagManager implements MetatagManagerInterface {
    * @return array
    *   Render array with tag elements.
    */
-  public function generateRawElements($tags, $entity = NULL) {
+  public function generateRawElements(array $tags, $entity = NULL) {
     $rawTags = [];
 
     $metatag_tags = $this->tagPluginManager->getDefinitions();
@@ -427,8 +478,8 @@ class MetatagManager implements MetatagManagerInterface {
       return ($weight_a < $weight_b) ? -1 : 1;
     });
 
-    // Each element of the $values array is a tag with the tag plugin name
-    // as the key.
+    // Each element of the $values array is a tag with the tag plugin name as
+    // the key.
     foreach ($tags as $tag_name => $value) {
       // Check to ensure there is a matching plugin.
       if (isset($metatag_tags[$tag_name])) {
@@ -438,13 +489,13 @@ class MetatagManager implements MetatagManagerInterface {
         // Render any tokens in the value.
         $token_replacements = [];
         if ($entity) {
-          // @TODO: This needs a better way of discovering the context.
+          // @todo This needs a better way of discovering the context.
           if ($entity instanceof ViewEntityInterface) {
             // Views tokens require the ViewExecutable, not the config entity.
             // @todo Can we move this into metatag_views somehow?
             $token_replacements = ['view' => $entity->getExecutable()];
           }
-          else {
+          elseif ($entity instanceof ContentEntityInterface) {
             $token_replacements = [$entity->getEntityTypeId() => $entity];
           }
         }
@@ -452,7 +503,7 @@ class MetatagManager implements MetatagManagerInterface {
         // Set the value as sometimes the data needs massaging, such as when
         // field defaults are used for the Robots field, which come as an array
         // that needs to be filtered and converted to a string.
-        // @see @Robots::setValue().
+        // @see Robots::setValue()
         $tag->setValue($value);
         $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
 
@@ -482,9 +533,11 @@ class MetatagManager implements MetatagManagerInterface {
    * Returns a list of fields handled by Metatag.
    *
    * @return array
+   *   A list of supported field types.
    */
   protected function fieldTypes() {
-    //@TODO: Either get this dynamically from field plugins or forget it and just hardcode metatag where this is called.
+    // @todo Either get this dynamically from field plugins or forget it and
+    // just hardcode metatag where this is called.
     return ['metatag'];
   }