+ /**
+ *
+ *
+ * @param ContentEntityInterface $entity
+ */
+ public function getDefaultMetatags(ContentEntityInterface $entity = NULL) {
+ // Get general global metatags
+ $metatags = $this->getGlobalMetatags();
+ // If that is empty something went wrong.
+ if (!$metatags) {
+ return;
+ }
+
+ // Check if this is a special page.
+ $special_metatags = $this->getSpecialMetatags();
+
+ // Merge with all globals defaults.
+ if ($special_metatags) {
+ $metatags->set('tags', array_merge($metatags->get('tags'), $special_metatags->get('tags')));
+ }
+
+ // Next check if there is this page is an entity that has meta tags.
+ // @TODO: Think about using other defaults, e.g. views. Maybe use plugins?
+ else {
+ if (is_null($entity)) {
+ $entity = metatag_get_route_entity();
+ }
+
+ if (!empty($entity)) {
+ // Get default metatags for a given entity.
+ $entity_defaults = $this->getEntityDefaultMetatags($entity);
+ if ($entity_defaults != NULL) {
+ $metatags->set('tags', array_merge($metatags->get('tags'), $entity_defaults));
+ }
+ }
+ }
+
+ return $metatags->get('tags');
+ }
+
+ /**
+ *
+ */
+ public function getGlobalMetatags() {
+ return $this->metatagDefaults->load('global');
+ }
+
+ /**
+ *
+ */
+ public function getSpecialMetatags() {
+ $metatags = NULL;
+
+ if (\Drupal::service('path.matcher')->isFrontPage()) {
+ $metatags = $this->metatagDefaults->load('front');
+ }
+ elseif (\Drupal::service('current_route_match')->getRouteName() == 'system.403') {
+ $metatags = $this->metatagDefaults->load('403');
+ }
+ elseif (\Drupal::service('current_route_match')->getRouteName() == 'system.404') {
+ $metatags = $this->metatagDefaults->load('404');
+ }
+
+ return $metatags;
+ }
+
+ /**
+ *
+ */
+ public function getEntityDefaultMetatags(ContentEntityInterface $entity) {
+ $entity_metatags = $this->metatagDefaults->load($entity->getEntityTypeId());
+ $metatags = [];
+ if ($entity_metatags != NULL) {
+ // Merge with global defaults.
+ $metatags = array_merge($metatags, $entity_metatags->get('tags'));
+ }
+
+ // Finally, check if we should apply bundle overrides.
+ $bundle_metatags = $this->metatagDefaults->load($entity->getEntityTypeId() . '__' . $entity->bundle());
+ if ($bundle_metatags != NULL) {
+ // Merge with existing defaults.
+ $metatags = array_merge($metatags, $bundle_metatags->get('tags'));
+ }
+
+ return $metatags;
+ }
+