3 namespace Drupal\metatag\Entity;
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;
13 * Defines the Metatag defaults entity.
16 * id = "metatag_defaults",
17 * label = @Translation("Metatag defaults"),
19 * "list_builder" = "Drupal\metatag\MetatagDefaultsListBuilder",
21 * "add" = "Drupal\metatag\Form\MetatagDefaultsForm",
22 * "edit" = "Drupal\metatag\Form\MetatagDefaultsForm",
23 * "delete" = "Drupal\metatag\Form\MetatagDefaultsDeleteForm",
24 * "revert" = "Drupal\metatag\Form\MetatagDefaultsRevertForm"
27 * config_prefix = "metatag_defaults",
28 * admin_permission = "administer meta tags",
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"
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.
71 * TRUE if the tag exists.
73 public function hasTag($tag_id) {
74 return array_key_exists($tag_id, $this->tags);
78 * Returns the value of a tag.
80 * @param string $tag_id
81 * The identifier of the tag.
84 * Array containing the tag values or NULL if not found.
86 public function getTag($tag_id) {
87 if (!$this->hasTag($tag_id)) {
90 return $this->tags[$tag_id];
94 * Reverts an entity to its default values.
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']);
107 * Overwrite the current tags with new values.
109 public function overwriteTags(array $new_tags = []) {
110 if (!empty($new_tags)) {
111 // Get the existing tags.
112 $combined_tags = $this->get('tags');
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;
119 // Save the combination of the existing tags + the new tags.
120 $this->set('tags', $combined_tags);
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') {
132 elseif ($b->id() == 'global') {
135 elseif ($a->id() == 'front') {
138 elseif ($b->id() == 'front') {
142 // Use the default sort function.
143 return parent::sort($a, $b);