Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / src / Entity / MetatagDefaults.php
1 <?php
2
3 namespace Drupal\metatag\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\Core\Config\Entity\ConfigEntityInterface;
7 use Drupal\Core\Config\FileStorage;
8 use Drupal\Core\Config\InstallStorage;
9 use Drupal\Core\Config\StorageInterface;
10 use Drupal\metatag\MetatagDefaultsInterface;
11
12 /**
13  * Defines the Metatag defaults entity.
14  *
15  * @ConfigEntityType(
16  *   id = "metatag_defaults",
17  *   label = @Translation("Metatag defaults"),
18  *   handlers = {
19  *     "list_builder" = "Drupal\metatag\MetatagDefaultsListBuilder",
20  *     "form" = {
21  *       "add" = "Drupal\metatag\Form\MetatagDefaultsForm",
22  *       "edit" = "Drupal\metatag\Form\MetatagDefaultsForm",
23  *       "delete" = "Drupal\metatag\Form\MetatagDefaultsDeleteForm",
24  *       "revert" = "Drupal\metatag\Form\MetatagDefaultsRevertForm"
25  *     }
26  *   },
27  *   config_prefix = "metatag_defaults",
28  *   admin_permission = "administer meta tags",
29  *   entity_keys = {
30  *     "id" = "id",
31  *     "label" = "label"
32  *   },
33  *   links = {
34  *     "edit-form" = "/admin/config/search/metatag/{metatag_defaults}/edit",
35  *     "delete-form" = "/admin/config/search/metatag/{metatag_defaults}/delete",
36  *     "revert-form" = "/admin/config/search/metatag/{metatag_defaults}/revert",
37  *     "collection" = "/admin/config/search/metatag"
38  *   }
39  * )
40  */
41 class MetatagDefaults extends ConfigEntityBase implements MetatagDefaultsInterface {
42
43   /**
44    * The Metatag defaults ID.
45    *
46    * @var string
47    */
48   protected $id;
49
50   /**
51    * The Metatag defaults label.
52    *
53    * @var string
54    */
55   protected $label;
56
57   /**
58    * The default tag values.
59    *
60    * @var array
61    */
62   protected $tags = [];
63
64   /**
65    * Returns TRUE if a tag exists.
66    *
67    * @param string $tag_id
68    *   The identifier of the tag.
69    *
70    * @return bool
71    *   TRUE if the tag exists.
72    */
73   public function hasTag($tag_id) {
74     return array_key_exists($tag_id, $this->tags);
75   }
76
77   /**
78    * Returns the value of a tag.
79    *
80    * @param string $tag_id
81    *   The identifier of the tag.
82    *
83    * @return array|null
84    *   Array containing the tag values or NULL if not found.
85    */
86   public function getTag($tag_id) {
87     if (!$this->hasTag($tag_id)) {
88       return NULL;
89     }
90     return $this->tags[$tag_id];
91   }
92
93   /**
94    * Reverts an entity to its default values.
95    */
96   public function revert() {
97     $default_install_path = drupal_get_path('module', 'metatag') . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
98     $storage = new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION);
99     $default_config_data = $storage->read('metatag.metatag_defaults.' . $this->id());
100     if ($default_config_data) {
101       $this->set('tags', $default_config_data['tags']);
102       $this->save();
103     }
104   }
105
106   /**
107    * Overwrite the current tags with new values.
108    */
109   public function overwriteTags(array $new_tags = []) {
110     if (!empty($new_tags)) {
111       // Get the existing tags.
112       $combined_tags = $this->get('tags');
113
114       // Loop over the new tags, adding them to the existing tags.
115       foreach ($new_tags as $tag_name => $data) {
116         $combined_tags[$tag_name] = $data;
117       }
118
119       // Save the combination of the existing tags + the new tags.
120       $this->set('tags', $combined_tags);
121     }
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
128     // Put always Global in 1st place and front page later if available.
129     if ($a->id() == 'global') {
130       return -1;
131     }
132     elseif ($b->id() == 'global') {
133       return 1;
134     }
135     elseif ($a->id() == 'front') {
136       return -1;
137     }
138     elseif ($b->id() == 'front') {
139       return 1;
140     }
141
142     // Use the default sort function.
143     return parent::sort($a, $b);
144   }
145
146 }