From e0032fa1e2ae53dc4218a2b3921ae09778a3fae7 Mon Sep 17 00:00:00 2001 From: Eric Bremner <ebremner@uwaterloo.ca> Date: Wed, 19 Oct 2022 07:43:48 -0400 Subject: [PATCH] ISTWCMS-5880: adding update hook for media fields --- uw_cfg_common.install | 171 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) diff --git a/uw_cfg_common.install b/uw_cfg_common.install index 415955ed..f1fc1a35 100644 --- a/uw_cfg_common.install +++ b/uw_cfg_common.install @@ -6,6 +6,7 @@ */ use Drupal\Core\Config\FileStorage; +use Drupal\field\Entity\FieldConfig; use Drupal\node\Entity\Node; use Drupal\taxonomy\Entity\Term; use Drupal\user\Entity\Role; @@ -458,3 +459,173 @@ function uw_cfg_common_update_9105(&$sandbox) { $active_storage = \Drupal::service('config.storage'); $active_storage->write($name, $source->read($name)); } + +/** + * Add new fields for banners above. + */ +function uw_cfg_common_update_9106(&$sandbox) { + + // The content types that are not getting any changes. + $cts_not_to_install = [ + 'uw_ct_sidebar', + 'uw_ct_site_footer', + 'uw_ct_expand_collapse_group', + ]; + + // The content types with media already in it (hero image). + $cts_with_media = [ + 'uw_ct_blog', + 'uw_ct_event', + 'uw_ct_news_item', + ]; + + // Get all the node types. + $node_types = \Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple(); + + // Step through each node type and get the path to + // the module and the machine name. + foreach ($node_types as $node_type) { + + // Get the id from the node type. + $id = $node_type->id(); + + // Ensure we are only getting the node type + // that need updating. + if (!in_array($id, $cts_not_to_install)) { + + // Catalogs and opportunities have different paths + // and ids, so setup the content types array with + // the correct info. + if ($id == 'uw_ct_catalog_item') { + $content_types['uw_ct_catalog'] = $id; + } else if ($id == 'uw_ct_opportunity') { + $content_types['uw_ct_opportunities'] = $id; + } else { + $content_types[$id] = $id; + } + } + } + + // Get the type of media field that we need to udpate. + $type_of_media = current(\Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(array('id' => 'node.field_uw_type_of_media'))); + + // Get the type of media settings. + $settings = $type_of_media->getSettings(); + + // Update the settings to remove the allowed values and + // add the allowed values function. + $settings['allowed_values'] = []; + $settings['allowed_values_function'] = '_uw_cfg_common_allowed_media_types'; + + // Set the update settings. + $type_of_media->setSettings($settings); + + // Save the field. + $type_of_media->save(); + + // The names of the fields we need to install. + $names = [ + 'field.storage.node.field_uw_banner', + 'field.storage.node.field_uw_media_width', + 'field.storage.node.field_uw_slide_speed', + 'field.storage.node.field_uw_transition_speed', + 'field.storage.node.field_uw_text_overlay_style', + ]; + + // Get the path to cfg common config install directory. + $path = \Drupal::service('extension.list.module')->getPath('uw_cfg_common') . '/config/install/'; + $config_dir = new FileStorage($path); + + // Step through each of the field names and install them. + foreach ($names as $name) { + + // Get the config from the yml file into an array. + $config_record = $config_dir->read($name); + + // Get the entity type. + $entity_type = \Drupal::service('config.manager')->getEntityTypeIdByName($name); + + // Get the storage. + $storage = \Drupal::entityTypeManager()->getStorage($entity_type); + + // Create the new field from the converted yml. + $field = $storage->createFromStorageRecord($config_record); + + // Save the new field. + $field->save(); + } + + // Step through each of the content types and install the fields, + // and also setup default values for the fields. + foreach ($content_types as $path => $id) { + + // Get the path to the content type. + $path = \Drupal::service('extension.list.module')->getPath($path) . '/config/install/'; + $config_dir = new FileStorage($path); + + // The names of the fields to install. + $names = [ + 'field.field.node.' . $id . '.field_uw_banner', + 'field.field.node.' . $id . '.field_uw_type_of_media', + 'field.field.node.' . $id . '.field_uw_media_width', + 'field.field.node.' . $id . '.field_uw_text_overlay_style', + 'field.field.node.' . $id . '.field_uw_transition_speed', + ]; + + // Step through each of the fields and install them. + foreach ($names as $name) { + + // Get the config from the yml into an array. + $config_record = $config_dir->read($name); + + // If the field config is not install, install it. + if (!FieldConfig::loadByName( + $config_record['entity_type'], + $config_record['bundle'], + $config_record['field_name']) + ) { + FieldConfig::create($config_record)->save(); + } + } + + // If there is no media yet on the content type, + // then install the type of hero image field. + if (!in_array($id, $cts_with_media)) { + + // Name of the hero image field. + $name = 'field.field.node.' . $id . '.field_uw_hero_image'; + + // Get the config from the yml into an array. + $config_record = $config_dir->read($name); + + // If the field config is not install, install it. + if (!FieldConfig::loadByName($config_record['entity_type'], $config_record['bundle'], $config_record['field_name'])) { + FieldConfig::create($config_record)->save(); + } + } + + // Get all the nodes of the content type. + $nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['type' => $id]); + + // Step through each of the nodes and set the default + // values of the fields. + foreach ($nodes as $node) { + + // Set default values for banners. + $node->field_uw_banner = NULL; + $node->field_uw_media_width = 'uw_lbs_full_width'; + $node->field_uw_slide_speed = 7000; + $node->field_uw_transition_speed = 400; + $node->field_uw_text_overlay_style = 'full-width'; + + // If the content type does not have media yet, + // set the default value for type of media. + if (!in_array($id, $cts_with_media)) { + $node->field_uw_type_of_media = NULL; + } + + // Save the node. + $node->save(); + } + } +} -- GitLab