Version 1
[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\Core\Language\LanguageInterface;
10 use Drupal\metatag\Entity\MetatagDefaults;
11
12 /**
13  * Implementsations of hook_update_N().
14  */
15
16 /**
17  * The "article:tags" meta tag was renamed to the correct "article:tag".
18  */
19 function metatag_open_graph_update_8101() {
20   /* @var $configs Drupal\metatag\Entity\MetatagDefaults */
21   $configs = MetatagDefaults::loadMultiple();
22
23   foreach ($configs as $config) {
24     $tags = $config->get('tags');
25
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);
30       $config->save();
31     }
32   }
33 }
34
35 /**
36  * The "article_tags" tag config was renamed "article_tag" on content entities.
37  */
38 function metatag_open_graph_update_8102(&$sandbox) {
39   // Update existing content with reference to old article_tags.
40   $etm = Drupal::entityTypeManager();
41
42   if (empty($sandbox)) {
43
44     $field_map = Drupal::getContainer()->get('entity_field.manager')->getFieldMap();
45     $sandbox['todo'] = [];
46     $sandbox['done'] = 0;
47     $sandbox['max'] = 0;
48     $sandbox['#finished'] = 0;
49
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();
58
59           if ($count > 0) {
60             $sandbox['todo'][$entity_type][$field_name] = 0;
61             $sandbox['max'] += $count;
62           }
63         }
64       }
65     }
66
67     if ($sandbox['max'] == 0) {
68       // Nothing to do.
69       $sandbox['#finished'] = 1;
70       return;
71     }
72   }
73
74   foreach ($sandbox['todo'] as $entity_type => $fields) {
75
76     /* @var $def Drupal\Core\Entity\ContentEntityType */
77     $def = Drupal::entityTypeManager()->getDefinition($entity_type);
78
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");
82
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, ">");
87       $q->sort($id_col);
88       $q->pager(20);
89       $res = $q->execute();
90
91       if (empty($res)) {
92         unset($sandbox['todo'][$entity_type][$field_name]);
93         continue;
94       }
95
96       $entities = $etm->getStorage($entity_type)->loadMultiple($res);
97
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));
114                   $trans->save();
115                 }
116               }
117             }
118           }
119         }
120
121         // Store the last pk per entity type and field name.
122         $sandbox['todo'][$entity_type][$field_name] = $entity->id();
123         $sandbox['done']++;
124         $sandbox['#finished'] = $sandbox['done'] / $sandbox['max'];
125       }
126     }
127   }
128 }