From 74cd07a8b06471c895073d020f7b796aa14957a8 Mon Sep 17 00:00:00 2001 From: Joe Gilliland-Lloyd <6943710+joegl@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:13:20 -0500 Subject: [PATCH] SDSS-1379: Set default value for Featured field (#504) * SDSS-1379: Set default value for Featured field. --- ...d.node.stanford_event.su_sdss_featured.yml | 4 ++- .../sdss/sdss_profile/sdss_profile.install | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docroot/profiles/sdss/sdss_profile/config/sync/field.field.node.stanford_event.su_sdss_featured.yml b/docroot/profiles/sdss/sdss_profile/config/sync/field.field.node.stanford_event.su_sdss_featured.yml index 0df605019..177cfe9bc 100644 --- a/docroot/profiles/sdss/sdss_profile/config/sync/field.field.node.stanford_event.su_sdss_featured.yml +++ b/docroot/profiles/sdss/sdss_profile/config/sync/field.field.node.stanford_event.su_sdss_featured.yml @@ -13,7 +13,9 @@ label: Featured description: 'Feature this content in lists and feeds.' required: false translatable: false -default_value: { } +default_value: + - + value: 0 default_value_callback: '' settings: on_label: Featured diff --git a/docroot/profiles/sdss/sdss_profile/sdss_profile.install b/docroot/profiles/sdss/sdss_profile/sdss_profile.install index 1f5cf8281..abe7bc2d2 100644 --- a/docroot/profiles/sdss/sdss_profile/sdss_profile.install +++ b/docroot/profiles/sdss/sdss_profile/sdss_profile.install @@ -1146,3 +1146,34 @@ function sdss_profile_update_10016(&$sandbox) { $sandbox['#finished'] = empty($sandbox['nids']) ? 1 : ($sandbox['count'] - count($sandbox['nids'])) / $sandbox['count']; } + +/** + * Set the Featured field to FALSE on existing Event nodes without a set value. + */ +function sdss_profile_update_10017(&$sandbox) { + $node_storage = \Drupal::entityTypeManager() + ->getStorage('node'); + if (!isset($sandbox['count'])) { + $nids = $node_storage->getQuery() + ->accessCheck(FALSE) + ->condition('type', 'stanford_event') + ->sort('created', 'DESC') + ->execute(); + $sandbox['nids'] = $nids; + $sandbox['count'] = count($sandbox['nids']); + } + + $node_ids = array_splice($sandbox['nids'], 0, 100); + /** @var \Drupal\node\NodeInterface[] $nodes */ + $nodes = $node_storage->loadMultiple($node_ids); + foreach ($nodes as $node) { + if (!$node->hasField('su_sdss_featured')) { + continue; + } + if ($node->get('su_sdss_featured')->isEmpty()) { + $node->set('su_sdss_featured', FALSE)->save(); + } + } + + $sandbox['#finished'] = empty($sandbox['nids']) ? 1 : ($sandbox['count'] - count($sandbox['nids'])) / $sandbox['count']; +}