Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / metatag / metatag_hreflang / src / Plugin / Derivative / HreflangDeriver.php
1 <?php
2
3 namespace Drupal\metatag_hreflang\Plugin\Derivative;
4
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Core\Language\Language;
7 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Create a new hreflang tag plugin for each enabled language.
13  */
14 class HreflangDeriver extends DeriverBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function getDerivativeDefinitions($base_plugin_definition) {
20     // Get a list of all defined languages.
21     $languages = \Drupal::languageManager()
22       ->getLanguages(LanguageInterface::STATE_ALL);
23
24     // Now we loop over them and declare the derivatives.
25     /** @var \Drupal\Core\Language\LanguageInterface $language */
26     foreach ($languages as $langcode => $language) {
27       // Ignore the global values.
28       if ($langcode == Language::LANGCODE_NOT_SPECIFIED) {
29         continue;
30       }
31       elseif ($langcode == Language::LANGCODE_NOT_APPLICABLE) {
32         continue;
33       }
34
35       // The base definition includes the annotations defined in the plugin,
36       // i.e. HreflangPerLanguage. Each one may be overridden.
37       $derivative = $base_plugin_definition;
38
39       // Here we fill in any missing keys on the layout annotation.
40       $derivative['weight']++;
41       $derivative['id'] = 'hreflang_' . $langcode;
42       // The 'name' value is used as the value of the 'hreflang' attribute on
43       // the HTML tag.
44       $derivative['name'] = $langcode;
45       $derivative['label'] = t("URL for a version of this page in %langcode", ['%langcode' => $language->getName()]);
46       $derivative['description'] = '';
47
48       // Reference derivatives based on their UUID instead of the record ID.
49       $this->derivatives[$derivative['id']] = $derivative;
50     }
51
52     return $this->derivatives;
53   }
54
55 }