You are here

class HreflangDeriver in Metatag 8

Create a new hreflang tag plugin for each enabled language.

Hierarchy

Expanded class hierarchy of HreflangDeriver

File

metatag_hreflang/src/Plugin/Derivative/HreflangDeriver.php, line 13

Namespace

Drupal\metatag_hreflang\Plugin\Derivative
View 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

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
HreflangDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.