diff --git a/localgov_publications.install b/localgov_publications.install index 363d5e8..40627a6 100644 --- a/localgov_publications.install +++ b/localgov_publications.install @@ -42,3 +42,29 @@ function localgov_publications_install() { $wysiwygFormat->save(); } } + +/** + * Fixes error messages after this module is installed. + * + * Remove references to the book content type from the key value store that are + * left over after book's config is removed. + */ +function localgov_publications_update_10001() { + + // If the book content type is still installed in the site, don't do anything. + $entityTypeManager = \Drupal::service('entity_type.manager'); + $bookType = $entityTypeManager->getStorage('node_type')->load('book'); + if ($bookType) { + return; + } + + // See localgov_publications_modules_installed() for what this code does. + // It's added here so it runs on existing installs too. + $kvStore = \Drupal::keyValue('entity.definitions.bundle_field_map'); + $fieldMap = $kvStore->get('node'); + if (isset($fieldMap['body']['bundles']['book'])) { + unset($fieldMap['body']['bundles']['book']); + $kvStore->set('node', $fieldMap); + } + \Drupal::cache('discovery')->delete('entity_field_map'); +} diff --git a/localgov_publications.module b/localgov_publications.module index e1d81f5..cc41e01 100644 --- a/localgov_publications.module +++ b/localgov_publications.module @@ -263,6 +263,18 @@ function localgov_publications_modules_installed($modules, $is_syncing) { \Drupal::configFactory()->getEditable($config_name)->delete(); } \Drupal::configFactory()->getEditable('node.type.book')->delete(); + + // The mapping of fields to bundles is stored in the key-value store. As we + // removed the content type and its fields on a config level, we also need + // to clear out the book data from here, as it won't be done for us like it + // is when you delete a content type via the UI. + $kvStore = \Drupal::keyValue('entity.definitions.bundle_field_map'); + $fieldMap = $kvStore->get('node'); + unset($fieldMap['body']['bundles']['book']); + $kvStore->set('node', $fieldMap); + + // Clear the cache where the full map is stored. + \Drupal::cache('discovery')->delete('entity_field_map'); } }