SU-HKKU/cardinal_service_profile

View on GitHub
modules/cardinal_service_profile_helper/cardinal_service_profile_helper.install

Summary

Maintainability
Test Coverage
<?php

/**
 * @file
 * cardinal_service_profile_helper.install
 */

/**
 * Clear migration events hashes to force updates.
 */
function cardinal_service_profile_helper_update_8001(&$sandbox) {
  \Drupal::database()->update('migrate_map_cs_events_importer')
    ->fields(['hash' => ''])->execute();
}

/**
 * Apply the default image to stories if no image exists.
 */
function cardinal_service_profile_helper_update_8002() {
  $node_storage = \Drupal::entityTypeManager()->getStorage('node');
  $node_ids = $node_storage->getQuery()
    ->condition('type', 'su_spotlight')
    ->condition('su_spotlight_feature_image', NULL, 'IS NULL')
    ->execute();
  foreach ($node_storage->loadMultiple($node_ids) as $node) {
    $node->set('su_spotlight_feature_image', 1726)->save();
  }
}

/**
 * Manipulate the data in spotlight fields.
 */
function cardinal_service_profile_helper_update_8003() {
  $node_storage = \Drupal::entityTypeManager()->getStorage('node');
  $node_ids = $node_storage->getQuery()
    ->accessCheck(FALSE)
    ->condition('type', 'su_spotlight')
    ->condition('su_spotlight_grad_area', NULL, 'IS NOT NULL')
    ->condition('su_spotlight_student_name', NULL, 'IS NOT NULL')
    ->execute();

  /** @var \Drupal\node\NodeInterface $node */
  foreach ($node_storage->loadMultiple($node_ids) as $node) {
    $grad = $node->get('su_spotlight_grad_area')->getString();
    $name = $node->get('su_spotlight_student_name')->getString();


    preg_match_all('/[0-9]+/', $grad, $grad_years);
    if (empty($grad_years[0])) {
      continue;
    }

    if (count($grad_years[0]) == 1) {
      $name .= ', \'' . implode(', \'', $grad_years[0]);
      $node->set('su_spotlight_student_name', $name)
        ->set('su_spotlight_grad_area', '')
        ->save();
    }
  }
}

/**
 * Invalidate all imported content to re-import their fields.
 */
function cardinal_service_profile_helper_update_8004() {
  $prefix = 'migrate_plus.migration.';
  $configs = \Drupal::configFactory()->listAll($prefix);
  $database = \Drupal::database();

  foreach ($configs as $config_name) {
    $migration_name = str_replace($prefix, '', $config_name);
    $table = "migrate_map_$migration_name";
    if ($database->schema()->tableExists($table)) {
      $database->update($table)->fields(['hash' => ''])->execute();
    }
  }
}

/**
 * Delete all the nodes that we don't want the with specific sponsors.
 */
function cardinal_service_profile_helper_update_8006() {
  $terms = [
    'Bill Lane Center for the American West',
    'The Bill Lane Center for the American West',
    'Bill Lane Center Internships',
    'Freeman Spogli Institute Global Policy Internships',
    'Freeman Spogli Institute for International Studies',
    'Stanford Global Studies',
    'King Center on Global Development',
    'Schneider Fellowships',
    'Seed Internships',
    'Stanford Arts',
    'Stanford Energy Internships in California and the West',
  ];
  $term_storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  $terms = $term_storage->loadByProperties(['name' => $terms]);

  $node_storage = \Drupal::entityTypeManager()->getStorage('node');
  $nids = $node_storage->getQuery()
    ->condition('su_opp_sponsor', array_keys($terms), 'IN')
    ->execute();
  $node_storage->delete($node_storage->loadMultiple($nids));

  $term_storage->delete($terms);
}

/**
 * Cleanup opportunities.
 */
function cardinal_service_profile_helper_update_8007() {
  cardinal_service_profile_helper_update_8006();
}

/**
 * Delete old opportunities from specific sponsors.
 */
function cardinal_service_profile_helper_update_8008() {
  $xml = file_get_contents('https://solo.stanford.edu/data/opportunities.xml?type=236');
  $dom = new DOMDocument();
  $dom->loadXML($xml);
  $xpath = new DOMXPath($dom);
  $delete_sponsors = [
    '/Bill Lane Center/',
    '/Freeman Spogli/',
    '/Stanford Global Studies/',
    '/King Center on Global Development/',
    '/Schneider Summer Fellowships/',
    '/Seed Internships/',
    '/Stanford Arts/',
    '/Stanford Energy Internships in California and the West/',
  ];

  foreach ($xpath->query('//OPPORTUNITY') as $opportunity) {
    $id = $xpath->query('ID', $opportunity)->item(0)->nodeValue;
    $sponsor = $xpath->query('SPONSOR', $opportunity)->item(0)->nodeValue;
    $sponsor = preg_replace($delete_sponsors, 'DeleteMe', $sponsor);

    if (strpos($sponsor, 'DeleteMe') === FALSE) {
      continue;
    }
    $entity_id = \Drupal::database()
      ->select('migrate_map_solo_opportunities', 'm')
      ->fields('m', ['destid1'])
      ->condition('sourceid1', $id)
      ->execute()
      ->fetchField();
    if ($entity_id) {
      $node = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->load($entity_id);
      $node->delete();
    }
  }
}

/**
 * Delete orphaned taxonomy terms.
 */
function cardinal_service_profile_helper_update_8009(&$sandbox) {
  $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  $node_storage = \Drupal::entityTypeManager()->getStorage('node');

  $vocab_fields = [
    'su_opportunity_location' => 'su_opp_location',
    'su_opportunity_sponsor' => 'su_opp_sponsor',
  ];
  foreach ($vocab_fields as $vid => $field_name) {
    $terms = $term_storage->loadByProperties(['vid' => $vid]);
    foreach ($terms as $term) {
      $nids = $node_storage->getQuery()
        ->condition($field_name, $term->id())
        ->execute();
      if (empty($nids)) {
        \Drupal::messenger()
          ->addStatus(t('Deleted orphaned term: @label', ['@label' => $term->label()]));
        $term->delete();
      }
    }
  }
}

/**
 * Re-save all the resource pages to generate their new aliases.
 */
function cardinal_service_profile_helper_update_8010() {
  drupal_flush_all_caches();
  $node_storage = \Drupal::entityTypeManager()
    ->getStorage('node');
  $nids = $node_storage->getQuery()
    ->condition('su_page_resource_dimension', NULL, 'IS NOT NULL')
    ->execute();
  foreach ($node_storage->loadMultiple($nids) as $node) {
    $node->save();
  }
}

/**
 * Change all the quote formats to minimal html.
 */
function cardinal_service_profile_helper_update_8011() {
  \Drupal::database()
    ->update('node__su_spotlight_quote')
    ->fields(['su_spotlight_quote_format' => 'stanford_minimal_html'])
    ->execute();
  \Drupal::database()
    ->update('node_revision__su_spotlight_quote')
    ->fields(['su_spotlight_quote_format' => 'stanford_minimal_html'])
    ->execute();
  drupal_flush_all_caches();
}

/**
 * Set migration source plugins to avoid config import errors.
 */
function cardinal_service_profile_helper_update_8012() {
  $configs = \Drupal::configFactory()->listAll('migrate_plus.migration.');
  foreach ($configs as $config_name) {
    $config = \Drupal::configFactory()->getEditable($config_name);
    if (!$config->get('source.plugin')) {
      $config->set('source.plugin', 'url')->save();
    }
  }
}