+
+/**
+ * Implements hook_entity_base_field_info().
+ */
+function metatag_entity_base_field_info(EntityTypeInterface $entity_type) {
+ $fields = [];
+ $base_table = $entity_type->getBaseTable();
+ $canonical_template_exists = $entity_type->hasLinkTemplate('canonical');
+ // Certain classes are just not supported.
+ $original_class = $entity_type->getOriginalClass();
+ $classes_to_skip = [
+ 'Drupal\comment\Entity\Comment',
+ ];
+
+ // If the entity type doesn't have a base table, has no link template then
+ // there's no point in supporting it.
+ if (!empty($base_table) && $canonical_template_exists && !in_array($original_class, $classes_to_skip)) {
+ $fields['metatag'] = BaseFieldDefinition::create('map')
+ ->setLabel(t('Metatags'))
+ ->setDescription(t('The meta tags for the entity.'))
+ ->setClass('\Drupal\metatag\Plugin\Field\MetatagEntityFieldItemList')
+ ->setQueryable(FALSE)
+ ->setComputed(TRUE)
+ ->setTargetEntityTypeId($entity_type->id());
+ }
+
+ return $fields;
+}
+
+/**
+ * Implements hook_entity_diff_options().
+ */
+function metatag_entity_diff_options($entity_type) {
+ if (metatag_entity_supports_metatags($entity_type)) {
+ $options = [
+ 'metatag' => t('Metatags'),
+ ];
+ return $options;
+ }
+}
+
+/**
+ * Implements hook_entity_diff().
+ */
+function metatag_entity_diff($old_entity, $new_entity, $context) {
+ $result = [];
+ $entity_type = $context['entity_type'];
+ $options = variable_get('diff_additional_options_' . $entity_type, []);
+ if (!empty($options['metatag']) && metatag_entity_supports_metatags($entity_type)) {
+ // Find meta tags that are set on either the new or old entity.
+ $tags = [];
+ foreach (['old' => $old_entity, 'new' => $new_entity] as $entity_key => $entity) {
+ $language = metatag_entity_get_language($entity_type, $entity);
+ if (isset($entity->metatags[$language])) {
+ foreach ($entity->metatags[$language] as $key => $value) {
+ $tags[$key][$entity_key] = $value['value'];
+ }
+ }
+ }
+
+ $init_weight = 100;
+ foreach ($tags as $key => $values) {
+ $id = ucwords('Meta ' . $key);
+ // @todo Find the default values and show these if not set.
+ $result[$id] = [
+ '#name' => $id,
+ '#old' => [empty($values['old']) ? '' : $values['old']],
+ '#new' => [empty($values['new']) ? '' : $values['new']],
+ '#weight' => $init_weight++,
+ '#settings' => [
+ 'show_header' => TRUE,
+ ],
+ ];
+ }
+ }
+ return $result;
+}
+
+/**
+ * Turn the meta tags for an entity into a human readable structure.
+ *
+ * @param object $entity
+ * The entity object.
+ *
+ * @return array
+ * All of the meta tags in a nested structure.
+ */
+function metatag_generate_entity_metatags($entity) {
+ $values = [];
+ $raw = metatag_get_tags_from_route($entity);
+ if (!empty($raw['#attached']['html_head'])) {
+ foreach ($raw['#attached']['html_head'] as $tag) {
+ $values[$tag[1]] = $tag[0];
+ }
+ }
+ return $values;
+}