5 * Update scripts for the Metatag Open Graph module.
8 use Drupal\Core\Entity\ContentEntityBase;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\metatag\Entity\MetatagDefaults;
13 * Implementsations of hook_update_N().
17 * The "article:tags" meta tag was renamed to the correct "article:tag".
19 function metatag_open_graph_update_8101() {
20 /* @var $configs Drupal\metatag\Entity\MetatagDefaults */
21 $configs = MetatagDefaults::loadMultiple();
23 foreach ($configs as $config) {
24 $tags = $config->get('tags');
26 if (array_key_exists("article_tags", $tags)) {
27 $tags['article_tag'] = $tags['article_tags'];
28 unset($tags['article_tags']);
29 $config->set("tags", $tags);
36 * The "article_tags" tag config was renamed "article_tag" on content entities.
38 function metatag_open_graph_update_8102(&$sandbox) {
39 // Update existing content with reference to old article_tags.
40 $etm = Drupal::entityTypeManager();
42 if (empty($sandbox)) {
44 $field_map = Drupal::getContainer()->get('entity_field.manager')->getFieldMap();
45 $sandbox['todo'] = [];
48 $sandbox['#finished'] = 0;
50 foreach ($field_map as $entity_type => $fields) {
51 foreach ($fields as $field_name => $field_def) {
52 if ($field_def['type'] == "metatag") {
53 // We found a metatag field, so query for all the entities of this
54 // type that have "article_tags" in the serialized array.
55 $q = \Drupal::entityQuery($entity_type);
56 $q->condition($field_name, "article_tags", "CONTAINS");
57 $count = $q->count()->execute();
60 $sandbox['todo'][$entity_type][$field_name] = 0;
61 $sandbox['max'] += $count;
67 if ($sandbox['max'] == 0) {
69 $sandbox['#finished'] = 1;
74 foreach ($sandbox['todo'] as $entity_type => $fields) {
76 /* @var $def Drupal\Core\Entity\ContentEntityType */
77 $def = Drupal::entityTypeManager()->getDefinition($entity_type);
79 // Grab the primary key field for this entity type
80 // so we can filter and order by it.
81 $id_col = $def->getKey("id");
83 foreach ($fields as $field_name => $last) {
84 $q = \Drupal::entityQuery($entity_type);
85 $q->condition($field_name, "article_tags", "CONTAINS");
86 $q->condition($id_col, $last, ">");
92 unset($sandbox['todo'][$entity_type][$field_name]);
96 $entities = $etm->getStorage($entity_type)->loadMultiple($res);
98 foreach ($entities as $entity) {
99 /* @var $entity ContentEntityBase */
100 if ($entity instanceof ContentEntityBase) {
101 if ($entity->hasField($field_name)) {
102 /* @var LanguageInterface $langcode */
103 foreach ($entity->getTranslationLanguages() as $langcode) {
104 // For each translation of this entity (including the source)...
105 $trans = $entity->getTranslation($langcode->getId());
106 $tags_serialized = $trans->get($field_name)->value;
107 if ($tags_serialized) {
108 // Change key from article_tags to article_tag.
109 $tags = unserialize($tags_serialized);
110 if (array_key_exists("article_tags", $tags)) {
111 $tags['article_tag'] = $tags['article_tags'];
112 unset($tags['article_tags']);
113 $trans->set($field_name, serialize($tags));
121 // Store the last pk per entity type and field name.
122 $sandbox['todo'][$entity_type][$field_name] = $entity->id();
124 $sandbox['#finished'] = $sandbox['done'] / $sandbox['max'];