class HreflangDeriver in Metatag 8
Create a new hreflang tag plugin for each enabled language.
Hierarchy
- class \Drupal\Component\Plugin\Derivative\DeriverBase implements DeriverInterface
- class \Drupal\metatag_hreflang\Plugin\Derivative\HreflangDeriver uses StringTranslationTrait
Expanded class hierarchy of HreflangDeriver
File
- metatag_hreflang/
src/ Plugin/ Derivative/ HreflangDeriver.php, line 13
Namespace
Drupal\metatag_hreflang\Plugin\DerivativeView source
class HreflangDeriver extends DeriverBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// Get a list of all defined languages.
$languages = \Drupal::languageManager()
->getLanguages(LanguageInterface::STATE_ALL);
// Now we loop over them and declare the derivatives.
/** @var \Drupal\Core\Language\LanguageInterface $language */
foreach ($languages as $langcode => $language) {
// Ignore the global values.
if ($langcode == Language::LANGCODE_NOT_SPECIFIED) {
continue;
}
elseif ($langcode == Language::LANGCODE_NOT_APPLICABLE) {
continue;
}
// The base definition includes the annotations defined in the plugin,
// i.e. HreflangPerLanguage. Each one may be overridden.
$derivative = $base_plugin_definition;
// Here we fill in any missing keys on the layout annotation.
$derivative['weight']++;
$derivative['id'] = 'hreflang_' . $langcode;
// The 'name' value is used as the value of the 'hreflang' attribute on
// the HTML tag.
$derivative['name'] = $langcode;
$derivative['label'] = $this
->t("URL for a version of this page in %langcode", [
'%langcode' => $language
->getName(),
]);
$derivative['description'] = '';
// Reference derivatives based on their UUID instead of the record ID.
$this->derivatives[$derivative['id']] = $derivative;
}
return $this->derivatives;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DeriverBase:: |
protected | property | List of derivative definitions. | 1 |
DeriverBase:: |
public | function |
Gets the definition of a derivative plugin. Overrides DeriverInterface:: |
|
HreflangDeriver:: |
public | function |
Gets the definition of all derivatives of a base plugin. Overrides DeriverBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |