Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / metatag_open_graph / metatag_open_graph.install
1 <?php
2
3 /**
4  * @file
5  * Update scripts for the Metatag Open Graph module.
6  */
7
8 use Drupal\Core\Entity\ContentEntityBase;
9 use Drupal\metatag\Entity\MetatagDefaults;
10
11 /**
12  * Implementations of hook_update_N().
13  */
14
15 /**
16  * The "article:tags" meta tag was renamed to the correct "article:tag".
17  */
18 function metatag_open_graph_update_8101() {
19   /* @var $configs Drupal\metatag\Entity\MetatagDefaults */
20   $configs = MetatagDefaults::loadMultiple();
21
22   foreach ($configs as $config) {
23     $tags = $config->get('tags');
24
25     if (array_key_exists("article_tags", $tags)) {
26       $tags['article_tag'] = $tags['article_tags'];
27       unset($tags['article_tags']);
28       $config->set("tags", $tags);
29       $config->save();
30     }
31   }
32 }
33
34 /**
35  * The "article_tags" tag config was renamed "article_tag" on content entities.
36  */
37 function metatag_open_graph_update_8102(&$sandbox) {
38   // Update existing content with reference to old article_tags.
39   $etm = Drupal::entityTypeManager();
40
41   if (empty($sandbox)) {
42
43     $field_map = Drupal::getContainer()->get('entity_field.manager')->getFieldMap();
44     $sandbox['todo'] = [];
45     $sandbox['done'] = 0;
46     $sandbox['max'] = 0;
47     $sandbox['#finished'] = 0;
48
49     foreach ($field_map as $entity_type => $fields) {
50       foreach ($fields as $field_name => $field_def) {
51         if ($field_def['type'] == "metatag") {
52           // We found a metatag field, so query for all the entities of this
53           // type that have "article_tags" in the serialized array.
54           $q = \Drupal::entityQuery($entity_type);
55           $q->condition($field_name, "article_tags", "CONTAINS");
56           $count = $q->count()->execute();
57
58           if ($count > 0) {
59             $sandbox['todo'][$entity_type][$field_name] = 0;
60             $sandbox['max'] += $count;
61           }
62         }
63       }
64     }
65
66     if ($sandbox['max'] == 0) {
67       // Nothing to do.
68       $sandbox['#finished'] = 1;
69       return;
70     }
71   }
72
73   foreach ($sandbox['todo'] as $entity_type => $fields) {
74
75     /* @var $def Drupal\Core\Entity\ContentEntityType */
76     $def = Drupal::entityTypeManager()->getDefinition($entity_type);
77
78     // Grab the primary key field for this entity type
79     // so we can filter and order by it.
80     $id_col = $def->getKey("id");
81
82     foreach ($fields as $field_name => $last) {
83       $q = \Drupal::entityQuery($entity_type);
84       $q->condition($field_name, "article_tags", "CONTAINS");
85       $q->condition($id_col, $last, ">");
86       $q->sort($id_col);
87       $q->pager(20);
88       $res = $q->execute();
89
90       if (empty($res)) {
91         unset($sandbox['todo'][$entity_type][$field_name]);
92         continue;
93       }
94
95       $entities = $etm->getStorage($entity_type)->loadMultiple($res);
96
97       foreach ($entities as $entity) {
98         /* @var $entity ContentEntityBase */
99         if ($entity instanceof ContentEntityBase) {
100           if ($entity->hasField($field_name)) {
101             /* @var LanguageInterface $langcode */
102             foreach ($entity->getTranslationLanguages() as $langcode) {
103               // For each translation of this entity (including the source)...
104               $trans = $entity->getTranslation($langcode->getId());
105               $tags_serialized = $trans->get($field_name)->value;
106               if ($tags_serialized) {
107                 // Change key from article_tags to article_tag.
108                 $tags = unserialize($tags_serialized);
109                 if (array_key_exists("article_tags", $tags)) {
110                   $tags['article_tag'] = $tags['article_tags'];
111                   unset($tags['article_tags']);
112                   $trans->set($field_name, serialize($tags));
113                   $trans->save();
114                 }
115               }
116             }
117           }
118         }
119
120         // Store the last pk per entity type and field name.
121         $sandbox['todo'][$entity_type][$field_name] = $entity->id();
122         $sandbox['done']++;
123         $sandbox['#finished'] = $sandbox['done'] / $sandbox['max'];
124       }
125     }
126   }
127 }