Added missing modules, including some as submodules.
[yaffs-website] / web / modules / contrib / metatag / metatag.module
index 1c42b85a18190c3c9cf813f963881602b95930b9..0cf71ec0e779b3d8affae1cd4a21302efd8ccb37 100644 (file)
@@ -9,6 +9,7 @@ use Drupal\Core\Entity\ContentEntityInterface;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Language\LanguageInterface;
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\Core\Url;
 
@@ -36,8 +37,8 @@ function metatag_help($route_name, RouteMatchInterface $route_match) {
 
     // The main configuration page.
     case 'entity.metatag_defaults.collection':
-      $output = '<p>' . t('Configure global meta tag default values below. Meta tags may be ') . '</p>';
-      $output .= '<p>' . t('Meta tags inherit from one level to the next unless they are overridden. To view a summary of the meta tags and the inheritance for a specific configuration, click on its name below.') . '</p>';
+      $output = '<p>' . t('Configure global meta tag default values below. Meta tags may be left as the default.') . '</p>';
+      $output .= '<p>' . t('Meta tag patterns are passed down from one level to the next unless they are overridden. To view a summary of the individual meta tags and the pattern for a specific configuration, click on its name below.') . '</p>';
       $output .= '<p>' . t('If the top-level configuration is not specific enough, additional default meta tag configurations can be added for a specific entity type or entity bundle, e.g. for a specific content type.') . '</p>';
       $output .= '<p>' . t('Meta tags can be further refined on a per-entity basis, e.g. for individual nodes, by adding the "Metatag" field to that entity type through its normal field settings pages.') . '</p>';
       return $output;
@@ -45,8 +46,8 @@ function metatag_help($route_name, RouteMatchInterface $route_match) {
 
     // The 'add default meta tags' configuration page.
     case 'entity.metatag_defaults.add_form':
-      $output = '<p>' . t('Use the following form to override the global default meta tags for a specific entity type or entity bundle. In practical terms, this allows the meta tags to be changed for a specific content type or taxonomy vocabulary, so that its content will have different meta tags <em>default values</em> than others.') . '</p>';
-      $output .= '<p>' . t('As a reminder, if the "Metatag" field is added to the entity type through its normal field settings, the meta tags can be further refined on a per entity basis; this allows eg. each node to have its meta tags refined as needed.') . '</p>';
+      $output = '<p>' . t('Use the following form to override the global default meta tags for a specific entity type or entity bundle. In practical terms, this allows the meta tags to be customized for a specific content type or taxonomy vocabulary, so that its content will have different meta tags <em>default values</em> than others.') . '</p>';
+      $output .= '<p>' . t('As a reminder, if the "Metatag" field is added to the entity type through its normal field settings, the meta tags can be further refined on a per entity basis; this allows each node to have its meta tags  customized on an individual basis.') . '</p>';
       return $output;
       break;
   }
@@ -103,7 +104,7 @@ function metatag_form_field_config_edit_form_alter(&$form, FormStateInterface $f
  */
 function metatag_page_attachments(array &$attachments) {
   if (!metatag_is_current_route_supported()) {
-    return;
+    return NULL;
   }
 
   $metatag_attachments = &drupal_static('metatag_attachments');
@@ -112,7 +113,7 @@ function metatag_page_attachments(array &$attachments) {
     // Load the meta tags from the route.
     $metatag_attachments = metatag_get_tags_from_route();
     if (!$metatag_attachments) {
-      return;
+      return NULL;
     }
 
     // If any Metatag items were found, append them.
@@ -134,8 +135,8 @@ function metatag_page_attachments(array &$attachments) {
  * Implements hook_entity_view_alter().
  */
 function metatag_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
-  // Some entities are built with a link rel="canonical" tag attached.
-  // If metatag provides one, remove the one built with the entity.
+  // Some entities are built with a link rel="canonical" and/or link rel="shortlink" tag attached.
+  // If metatag provides them, remove the ones built with the entity.
   if (isset($build['#attached']['html_head_link'])) {
     $metatag_attachments = drupal_static('metatag_attachments');
     if (is_null($metatag_attachments)) {
@@ -143,20 +144,18 @@ function metatag_entity_view_alter(array &$build, EntityInterface $entity, Entit
       $metatag_attachments = metatag_get_tags_from_route();
     }
 
-    // Check to see if the page currently outputs a canonical tag.
+    // Check to see if the page currently outputs a canonical and/or shortlink tag.
     if (isset($metatag_attachments['#attached']['html_head'])) {
       foreach ($metatag_attachments['#attached']['html_head'] as $metatag_item) {
-        if ($metatag_item[1] == 'canonical_url') {
-          // Metatag provides a link rel="canonical" tag.
+        if (in_array($metatag_item[1], array('canonical_url', 'shortlink'))) {
+          // Metatag provides rel="canonical" and/or rel="shortlink" tags.
           foreach ($build['#attached']['html_head_link'] as $key => $item) {
-            if (isset($item[0]['rel']) && $item[0]['rel'] == 'canonical') {
-              // Remove the link rel="canonical" tag from the entity's build
-              // array.
+            if (isset($item[0]['rel']) && in_array($item[0]['rel'], array('canonical', 'shortlink'))) {
+              // Remove the link rel="canonical" or link rel="shortlink" tag
+              // from the entity's build array.
               unset($build['#attached']['html_head_link'][$key]);
-              break;
             }
           }
-          break;
         }
       }
     }
@@ -203,6 +202,15 @@ function metatag_get_route_entity() {
     return $route_match->getParameter($entity_type);
   }
 
+  // Look for a rest entity view page, e.g. node/{nid}?_format=json, etc.
+  $matches = [];
+  // ie: rest.entity.node.GET.json
+  preg_match('/rest\.entity\.(.*)\.(.*)\.(.*)/', $route_name, $matches);
+  if (!empty($matches[1])) {
+    $entity_type = $matches[1];
+    return $route_match->getParameter($entity_type);
+  }
+
   // Look for entity object 'add' pages, e.g. node/add/{bundle}.
   $route_name_matches = [];
   preg_match('/(entity\.)?(.*)\.add(_form)?/', $route_name, $route_name_matches);
@@ -235,6 +243,26 @@ function metatag_get_route_entity() {
     }
   }
 
+  // Look for entity object 'add content translation' pages, e.g.
+  // node/{nid}/translations/add/{source_lang}/{translation_lang}
+  $route_name_matches = [];
+  preg_match('/(entity\.)?(.*)\.content_translation_add/', $route_name, $route_name_matches);
+  if (!empty($route_name_matches[2])) {
+    $entity_type = $route_name_matches[2];
+    $definition = Drupal::entityTypeManager()->getDefinition($entity_type, FALSE);
+    if (!empty($definition)) {
+      $node = $route_match->getParameter($entity_type);
+      $type = $node->bundle();
+      if (!empty($type)) {
+        return \Drupal::entityTypeManager()
+          ->getStorage($entity_type)
+          ->create([
+            $definition->get('entity_keys')['bundle'] => $type,
+          ]);
+      }
+    }
+  }
+
   // Special handling for the admin user_create page. In this case, there's only
   // one bundle and it's named the same as the entity type, so some shortcuts
   // can be used.
@@ -263,7 +291,7 @@ function metatag_get_route_entity() {
  */
 function metatag_preprocess_html(&$variables) {
   if (!metatag_is_current_route_supported()) {
-    return;
+    return NULL;
   }
 
   $attachments = drupal_static('metatag_attachments');
@@ -272,7 +300,7 @@ function metatag_preprocess_html(&$variables) {
   }
 
   if (!$attachments) {
-    return;
+    return NULL;
   }
 
   // Load the page title.
@@ -307,7 +335,7 @@ function metatag_get_tags_from_route() {
   // First, get defaults.
   $metatags = metatag_get_default_tags();
   if (!$metatags) {
-    return;
+    return NULL;
   }
 
   // Then, set tag overrides for this particular entity.
@@ -318,6 +346,13 @@ function metatag_get_tags_from_route() {
     }
   }
 
+  // Trigger hook_metatags_alter().
+  // Allow modules to override tags or the entity used for token replacements.
+  $context = [
+    'entity' => $entity,
+  ];
+  \Drupal::service('module_handler')->alter('metatags', $metatags, $context);
+
   return $metatag_manager->generateElements($metatags, $entity);
 }
 
@@ -333,19 +368,11 @@ function metatag_get_default_tags() {
   // First we load global defaults.
   $metatags = $global_metatag_manager->load('global');
   if (!$metatags) {
-    return;
+    return NULL;
   }
 
   // Check if this is a special page.
-  if (\Drupal::service('path.matcher')->isFrontPage()) {
-    $special_metatags = $global_metatag_manager->load('front');
-  }
-  elseif (\Drupal::service('current_route_match')->getRouteName() == 'system.403') {
-    $special_metatags = $global_metatag_manager->load('403');
-  }
-  elseif (\Drupal::service('current_route_match')->getRouteName() == 'system.404') {
-    $special_metatags = $global_metatag_manager->load('404');
-  }
+  $special_metatags = \Drupal::service('metatag.manager')->getSpecialMetatags();
   if (isset($special_metatags)) {
     $metatags->overwriteTags($special_metatags->get('tags'));
   }