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;
// 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;
// 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;
}
*/
function metatag_page_attachments(array &$attachments) {
if (!metatag_is_current_route_supported()) {
- return;
+ return NULL;
}
$metatag_attachments = &drupal_static('metatag_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.
* 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)) {
$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;
}
}
}
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);
}
}
+ // 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.
*/
function metatag_preprocess_html(&$variables) {
if (!metatag_is_current_route_supported()) {
- return;
+ return NULL;
}
$attachments = drupal_static('metatag_attachments');
}
if (!$attachments) {
- return;
+ return NULL;
}
// Load the page title.
// First, get defaults.
$metatags = metatag_get_default_tags();
if (!$metatags) {
- return;
+ return NULL;
}
// Then, set tag overrides for this particular entity.
}
}
+ // 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);
}
// 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'));
}