5 * Update scripts for the Metatag Open Graph module.
8 use Drupal\Core\Entity\ContentEntityBase;
9 use Drupal\metatag\Entity\MetatagDefaults;
12 * Implementations of hook_update_N().
16 * The "article:tags" meta tag was renamed to the correct "article:tag".
18 function metatag_open_graph_update_8101() {
19 /* @var $configs Drupal\metatag\Entity\MetatagDefaults */
20 $configs = MetatagDefaults::loadMultiple();
22 foreach ($configs as $config) {
23 $tags = $config->get('tags');
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);
35 * The "article_tags" tag config was renamed "article_tag" on content entities.
37 function metatag_open_graph_update_8102(&$sandbox) {
38 // Update existing content with reference to old article_tags.
39 $etm = Drupal::entityTypeManager();
41 if (empty($sandbox)) {
43 $field_map = Drupal::getContainer()->get('entity_field.manager')->getFieldMap();
44 $sandbox['todo'] = [];
47 $sandbox['#finished'] = 0;
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();
59 $sandbox['todo'][$entity_type][$field_name] = 0;
60 $sandbox['max'] += $count;
66 if ($sandbox['max'] == 0) {
68 $sandbox['#finished'] = 1;
73 foreach ($sandbox['todo'] as $entity_type => $fields) {
75 /* @var $def Drupal\Core\Entity\ContentEntityType */
76 $def = Drupal::entityTypeManager()->getDefinition($entity_type);
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");
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, ">");
91 unset($sandbox['todo'][$entity_type][$field_name]);
95 $entities = $etm->getStorage($entity_type)->loadMultiple($res);
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));
120 // Store the last pk per entity type and field name.
121 $sandbox['todo'][$entity_type][$field_name] = $entity->id();
123 $sandbox['#finished'] = $sandbox['done'] / $sandbox['max'];