external_hreflang.module in External Hreflang 8
Module file.
File
external_hreflang.moduleView source
<?php
/**
* @file
* Module file.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\external_hreflang\Plugin\metatag\Tag\ExternalHreflang;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_simple_sitemap_links_alter().
*/
function external_hreflang_simple_sitemap_links_alter(&$links) {
$languages = \Drupal::languageManager()
->getLanguages();
foreach ($languages as $language) {
$tokens_to_process['[current-page:url:relative:' . $language
->getId() . ']'] = $language;
}
foreach ($links as &$link) {
if (isset($link['meta'], $link['meta']['path']) && empty($link['meta']['path'])) {
$tags = _external_hreflang_get_metatags();
$url = Url::fromRoute('<front>');
}
elseif (isset($link['meta'], $link['meta']['entity_info'])) {
$entity = \Drupal::entityTypeManager()
->getStorage($link['meta']['entity_info']['entity_type'])
->load($link['meta']['entity_info']['id']);
if ($entity instanceof EntityInterface) {
$tags = _external_hreflang_get_metatags($entity
->getEntityTypeId(), $entity
->bundle());
$url = Url::fromUserInput('/' . $link['meta']['path']);
}
}
if (!empty($tags)) {
foreach ($tags as $locale => $path) {
foreach ($tokens_to_process as $token => $language) {
if (strpos($path, $token) !== FALSE) {
$url
->setOption('language', $language);
$href = str_replace('/' . $language
->getId(), '', $url
->toString());
$path = str_replace($token, $href, $path);
}
}
$link['alternate_urls'][$locale] = $path;
}
}
}
}
/**
* Load the meta tags by processing the route parameters.
*
* @see metatag_get_tags_from_route()
*
* @return mixed
* Array of hreflang tags or NULL.
*/
function _external_hreflang_get_metatags(string $entity_type_id = '', string $bundle = '') {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $global_metatag_manager */
$global_metatag_manager = \Drupal::entityTypeManager()
->getStorage('metatag_defaults');
// First we load global defaults.
$metatags = $global_metatag_manager
->load('global');
if (!$metatags) {
return NULL;
}
// Than we load for front page.
if ($entity_type_id) {
$entity_metatags = $global_metatag_manager
->load($entity_type_id);
$metatags
->overwriteTags($entity_metatags
->get('tags'));
// Check if bundle overrides should be added.
$bundle_metatags = $global_metatag_manager
->load($entity_type_id . '__' . $bundle);
if ($bundle_metatags != NULL) {
// Merge with existing defaults.
$metatags
->overwriteTags($bundle_metatags
->get('tags'));
}
}
else {
$front_metatags = $global_metatag_manager
->load('front');
$metatags
->overwriteTags($front_metatags
->get('tags'));
}
$hreflag_tags = $metatags
->get('tags')['hreflang_external'] ?? '';
return ExternalHreflang::getHrefLangsArrayFromString($hreflag_tags);
}
/**
* Implements hook_help().
*/
function external_hreflang_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.external_hreflang':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('External Hreflang project allows adding alternate hreflang for external sites (not languages in Drupal).') . '</p>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<p>' . t('To configure just access your metatag configuration and you can check a new field called "External Hreflang".') . '</p>';
$output .= '<p>' . t('Fill this field using key|value sintax using language|url. E.g:".') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('en-US|http://www.google.com') . '</li>';
$output .= '<li>' . t('es-ES|http://www.google.es') . '</li>';
$output .= '</ul>';
return $output;
}
}
Functions
Name | Description |
---|---|
external_hreflang_help | Implements hook_help(). |
external_hreflang_simple_sitemap_links_alter | Implements hook_simple_sitemap_links_alter(). |
_external_hreflang_get_metatags | Load the meta tags by processing the route parameters. |