From 1dce19d7bd0bfe89328e131ca2bb47e99baa0ee0 Mon Sep 17 00:00:00 2001 From: Joe Gilliland-Lloyd <6943710+joegl@users.noreply.github.com> Date: Tue, 3 Sep 2024 17:43:50 -0500 Subject: [PATCH] SDSS-1360: DEV | Set Featured Events default value for existing content (#482) * SDSS-1360: DEV | Set Featured Events default value for existing content. --- .../sdss/sdss_profile/sdss_profile.install | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/docroot/profiles/sdss/sdss_profile/sdss_profile.install b/docroot/profiles/sdss/sdss_profile/sdss_profile.install index 95251fcf8..7cda3655c 100644 --- a/docroot/profiles/sdss/sdss_profile/sdss_profile.install +++ b/docroot/profiles/sdss/sdss_profile/sdss_profile.install @@ -960,3 +960,49 @@ function sdss_profile_update_10009(&$sandbox) { $sandbox['#finished'] = empty($sandbox['nids']) ? 1 : ($sandbox['count'] - count($sandbox['nids'])) / $sandbox['count']; } + +/** + * Create su_sdss_featured field from configuration. + */ +function sdss_profile_update_10010() { + $config_directory = \Drupal::service('config.storage.sync'); + $field_storage = $config_directory->read('field.storage.node.su_sdss_featured'); + $field_config = $config_directory->read('field.field.node.stanford_event.su_sdss_featured'); + + // Create su_sdss_featured field from exported configuration. + if (!FieldStorageConfig::loadByName($field_storage['entity_type'], $field_storage['field_name'])) { + FieldStorageConfig::create($field_storage)->save(); + } + if (!FieldConfig::loadByName($field_config['entity_type'], $field_config['bundle'], $field_config['field_name'])) { + FieldConfig::create($field_config)->save(); + } +} + +/** + * Set the Featured field to FALSE on all existing Event Nodes. + */ +function sdss_profile_update_10011(&$sandbox) { + $node_storage = \Drupal::entityTypeManager() + ->getStorage('node'); + if (!isset($sandbox['count'])) { + $nids = $node_storage->getQuery() + ->accessCheck(FALSE) + ->condition('type', 'stanford_event') + ->sort('created') + ->execute(); + $sandbox['nids'] = $nids; + $sandbox['count'] = count($sandbox['nids']); + } + + $node_ids = array_splice($sandbox['nids'], 0, 250); + /** @var \Drupal\node\NodeInterface[] $nodes */ + $nodes = $node_storage->loadMultiple($node_ids); + foreach ($nodes as $node) { + if (!$node->hasField('su_sdss_featured')) { + continue; + } + $node->set('su_sdss_featured', FALSE)->save(); + } + + $sandbox['#finished'] = empty($sandbox['nids']) ? 1 : ($sandbox['count'] - count($sandbox['nids'])) / $sandbox['count']; +}