From 1426f9b1399f93f7d5ed70b17d258ffb1a1dc4ed Mon Sep 17 00:00:00 2001 From: David Valdez Date: Thu, 1 Aug 2019 16:45:27 -0500 Subject: [PATCH] Add the "Geo Original" term. All the articles with citations will have this term. --- scripts/article_repost_term.php | 57 ---------- scripts/article_type_term.php | 103 ++++++++++++++++++ .../upgrade_d7_taxonomy_term_vocabulary_3.yml | 8 +- 3 files changed, 105 insertions(+), 63 deletions(-) delete mode 100755 scripts/article_repost_term.php create mode 100755 scripts/article_type_term.php diff --git a/scripts/article_repost_term.php b/scripts/article_repost_term.php deleted file mode 100755 index 4e47419..0000000 --- a/scripts/article_repost_term.php +++ /dev/null @@ -1,57 +0,0 @@ - $term_name, - 'vid' => 'article_type', - ])->save(); - $term = taxonomy_term_load_multiple_by_name($term_name, 'article_type'); -} -$term = current($term); - -// Add the term to all the entities that hasn't a value in the citation field. -$query = \Drupal::entityQuery('node'); -$query->condition('type', 'article'); -$entity_ids = $query->execute(); -$db = \Drupal::database(); -$query = $db->query(" - SELECT - n.nid - FROM - drupal7.node as n - LEFT JOIN - drupal7.field_data_field_citiations as fdfc - ON - (n.nid = fdfc.entity_id) - WHERE n.type = 'story' - AND - fdfc.field_citiations_value IS null;" -); -$entity_ids = $query->fetchAll(PDO::FETCH_ASSOC); -foreach ($entity_ids as $entity) { - $entity = Node::load($entity['nid']); - $entity_terms = $entity->get('field_article_type')->getValue(); - - // Check if the entity hasn't the term already. - $add_term = TRUE; - foreach ($entity_terms as $term_node) { - if ($term_node['target_id'] == $term->id()) { - $add_term = FALSE; - } - } - if ($add_term == TRUE) { - $entity_terms = array_merge($entity->get('field_article_type')->getValue(), [['target_id' => $term->id()]]); - $entity->set('field_article_type', $entity_terms); - $entity->save(); - } - -} - diff --git a/scripts/article_type_term.php b/scripts/article_type_term.php new file mode 100755 index 0000000..a6a2f18 --- /dev/null +++ b/scripts/article_type_term.php @@ -0,0 +1,103 @@ +id(), $repost_entities); +apply_term($term_geo_original->id(), $original_entities); + +/** + * Return a list of entities depending if it used to have populated the field + * citation in the old site. + */ +function getArticleEntities($has_citation = FALSE) { + if ($has_citation) { + $condition = 'IS NOT NULL'; + } + else { + $condition = 'IS NULL'; + } + + $db = Database::getConnection('default', 'drupal7'); + $query = $db->query(" + SELECT + n.nid + FROM + node as n + LEFT JOIN + field_data_field_citiations as fdfc + ON + (n.nid = fdfc.entity_id) + WHERE n.type = 'story' + AND + fdfc.field_citiations_value " . $condition . ";" + ); + return $query->fetchAll(PDO::FETCH_ASSOC); +} + + +/** + * Return a term and create it if does not exist. + * + * @param string $term_name + * The term that is going to be loaded or created. + * @param string $vocabulary + * The vocabulary of the term. + * + * @return \Drupal\taxonomy\Entity\Term + */ +function create_term_if_not_exists($term_name, $vocabulary) { + $term = taxonomy_term_load_multiple_by_name($term_name, $vocabulary); + // Create the term if it does not exist and load it. + if (empty($term)) { + Term::create([ + 'name' => $term_name, + 'vid' => 'article_type', + ])->save(); + $term = taxonomy_term_load_multiple_by_name($term_name, $vocabulary); + } + return current($term); + +} + +/** + * @param int $term_id + * The term that is going to be applied to all the nodes. + * @param array $entity_ids + * The entities which are going to receive the term. + * + */ +function apply_term($term_id, $entity_ids) { + foreach ($entity_ids as $entity) { + $entity = Node::load($entity['nid']); + $entity_terms = $entity->get('field_article_type')->getValue(); + + // Check if the entity hasn't the term already. + $add_term = TRUE; + foreach ($entity_terms as $term_node) { + if ($term_node['target_id'] == $term_id) { + $add_term = FALSE; + } + } + if ($add_term == TRUE) { + $entity_terms = array_merge($entity->get('field_article_type')->getValue(), [['target_id' => $term_id]]); + $entity->set('field_article_type', $entity_terms); + $entity->save(); + } + + } +} + diff --git a/web/modules/custom/geo_upgrade/migrations/upgrade_d7_taxonomy_term_vocabulary_3.yml b/web/modules/custom/geo_upgrade/migrations/upgrade_d7_taxonomy_term_vocabulary_3.yml index 4babf49..9b95879 100644 --- a/web/modules/custom/geo_upgrade/migrations/upgrade_d7_taxonomy_term_vocabulary_3.yml +++ b/web/modules/custom/geo_upgrade/migrations/upgrade_d7_taxonomy_term_vocabulary_3.yml @@ -23,7 +23,7 @@ process: # Only migrate these terms. value: - 17 # Reviews. - - 50 # Articles (renamed as GEO Original) + # - 50 # Is not going to be used anymore. - plugin: get vid: @@ -33,12 +33,8 @@ process: source: vid name: - - plugin: static_map + plugin: get source: name - # Articles is going to be renamed as 'Geo Original' - map: - 'Articles' : 'GEO Original' - bypass: TRUE description/value: - plugin: get