3 namespace Drupal\metatag\Entity;
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;
12 * Defines the Metatag defaults entity.
15 * id = "metatag_defaults",
16 * label = @Translation("Metatag defaults"),
18 * "list_builder" = "Drupal\metatag\MetatagDefaultsListBuilder",
20 * "add" = "Drupal\metatag\Form\MetatagDefaultsForm",
21 * "edit" = "Drupal\metatag\Form\MetatagDefaultsForm",
22 * "delete" = "Drupal\metatag\Form\MetatagDefaultsDeleteForm",
23 * "revert" = "Drupal\metatag\Form\MetatagDefaultsRevertForm"
26 * config_prefix = "metatag_defaults",
27 * admin_permission = "administer meta tags",
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"
41 class MetatagDefaults extends ConfigEntityBase implements MetatagDefaultsInterface {
44 * The Metatag defaults ID.
51 * The Metatag defaults label.
58 * The default tag values.
65 * Returns TRUE if a tag exists.
67 * @param string $tag_id
68 * The identifier of the tag.
70 * TRUE if the tag exists.
72 public function hasTag($tag_id) {
73 return array_key_exists($tag_id, $this->tags);
77 * Returns the value of a tag.
79 * @param string $tag_id
80 * The identifier of the tag.
82 * array containing the tag values or NULL if not found.
84 public function getTag($tag_id) {
85 if (!$this->hasTag($tag_id)) {
88 return $this->tags[$tag_id];
92 * Reverts an entity to its default values.
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']);
106 * Overwrite the current tags with new values.
108 public function overwriteTags(array $new_tags = []) {
109 if (!empty($new_tags)) {
110 // Get the existing tags.
111 $combined_tags = $this->get('tags');
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;
118 // Save the combination of the existing tags + the new tags.
119 $this->set('tags', $combined_tags);