25ef147248e8d90bbc6f4a4047721a9a20ee90bc
[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\FileStorage;
7 use Drupal\Core\Config\InstallStorage;
8 use Drupal\Core\Config\StorageInterface;
9 use Drupal\metatag\MetatagDefaultsInterface;
10
11 /**
12  * Defines the Metatag defaults entity.
13  *
14  * @ConfigEntityType(
15  *   id = "metatag_defaults",
16  *   label = @Translation("Metatag defaults"),
17  *   handlers = {
18  *     "list_builder" = "Drupal\metatag\MetatagDefaultsListBuilder",
19  *     "form" = {
20  *       "add" = "Drupal\metatag\Form\MetatagDefaultsForm",
21  *       "edit" = "Drupal\metatag\Form\MetatagDefaultsForm",
22  *       "delete" = "Drupal\metatag\Form\MetatagDefaultsDeleteForm",
23  *       "revert" = "Drupal\metatag\Form\MetatagDefaultsRevertForm"
24  *     }
25  *   },
26  *   config_prefix = "metatag_defaults",
27  *   admin_permission = "administer meta tags",
28  *   entity_keys = {
29  *     "id" = "id",
30  *     "label" = "label"
31  *   },
32  *   links = {
33  *     "canonical" = "/admin/structure/metatag_defaults/{metatag_defaults}",
34  *     "edit-form" = "/admin/structure/metatag_defaults/{metatag_defaults}/edit",
35  *     "delete-form" = "/admin/structure/metatag_defaults/{metatag_defaults}/delete",
36  *     "revert-form" = "/admin/structure/metatag_defaults/{metatag_defaults}/revert",
37  *     "collection" = "/admin/structure/metatag_defaults"
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    * @return boolean
70    *   TRUE if the tag exists.
71    */
72   public function hasTag($tag_id) {
73     return array_key_exists($tag_id, $this->tags);
74   }
75
76   /**
77    * Returns the value of a tag.
78    *
79    * @param string $tag_id
80    *   The identifier of the tag.
81    * @return array|NULL
82    *   array containing the tag values or NULL if not found.
83    */
84   public function getTag($tag_id) {
85     if (!$this->hasTag($tag_id)) {
86       return NULL;
87     }
88     return $this->tags[$tag_id];
89   }
90
91   /**
92    * Reverts an entity to its default values.
93    */
94   public function revert() {
95     $config_installer = \Drupal::service('config.installer');
96     $default_install_path = drupal_get_path('module', 'metatag') . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
97     $storage = new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION);
98     $default_config_data = $storage->read('metatag.metatag_defaults.' . $this->id());
99     if ($default_config_data) {
100       $this->set('tags', $default_config_data['tags']);
101       $this->save();
102     }
103   }
104
105   /**
106    * Overwrite the current tags with new values.
107    */
108   public function overwriteTags(array $new_tags = []) {
109     if (!empty($new_tags)) {
110       // Get the existing tags.
111       $combined_tags = $this->get('tags');
112
113       // Loop over the new tags, adding them to the existing tags.
114       foreach ($new_tags as $tag_name => $data) {
115         $combined_tags[$tag_name] = $data;
116       }
117
118       // Save the combination of the existing tags + the new tags.
119       $this->set('tags', $combined_tags);
120     }
121   }
122
123 }