Created
February 20, 2018 04:27
-
-
Save jasonawant/61be761bea2d771ebd14b8c18ee111f4 to your computer and use it in GitHub Desktop.
A Drupal 8 update hook to uninstall workbench moderation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Uninstall workbench_moderation module and its fields, update content overview page. | |
*/ | |
function my_module_update_8001() { | |
if (\Drupal::service('module_handler')->moduleExists('workbench_moderation')) { | |
// Delete moderation_state fields. | |
// @see \Drupal\workbench_moderation\Form\PrepareUninstallForm::submitForm(). | |
// @see https://www.drupal.org/node/2627012 | |
$tables_to_update = []; | |
$moderation_info = \Drupal::service('workbench_moderation.moderation_information'); | |
$entity_type_manager = \Drupal::service('entity_type.manager'); | |
foreach ($entity_type_manager->getDefinitions() as $entity_type) { | |
if ($moderation_info->isModeratableEntityType($entity_type)) { | |
$tables_to_update[] = $entity_type->getDataTable(); | |
$tables_to_update[] = $entity_type->getRevisionDataTable(); | |
} | |
} | |
$db_connection = \Drupal::database(); | |
foreach ($tables_to_update as $table) { | |
if ($table && $db_connection->schema()->fieldExists($table, 'moderation_state')) { | |
$db_connection->update($table) | |
->fields(['moderation_state' => NULL]) | |
->execute(); | |
} | |
} | |
\Drupal::service('module_installer')->uninstall(['workbench_moderation']); | |
// Remove the field storage and instance configuration. | |
$bundles = ['content_type_1', 'content_type_2']; | |
foreach ($bundles as $bundle) { | |
if (FieldConfig::loadByName('node', $bundle, 'field_moderation_state_update')) { | |
FieldConfig::loadByName('node', $bundle, 'field_moderation_state_update')->delete(); | |
} | |
} | |
// After all field instances have been deleted, delete the field storage. | |
if (FieldStorageConfig::loadByName('node', 'field_moderation_state_update')) { | |
FieldStorageConfig::loadByName('node', 'field_moderation_state_update')->delete(); | |
} | |
// Update content overview page with Drupal core node view. | |
$module_handler = \Drupal::moduleHandler(); | |
$config_storage = new FileStorage($module_handler->getModule('node')->getPath() . '/config/optional'); | |
$config_name = 'views.view.content'; | |
$config_record = $config_storage->read($config_name); | |
$entity_type = \Drupal::service('config.manager')->getEntityTypeIdByName($config_name); | |
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */ | |
$storage = \Drupal::entityTypeManager()->getStorage($entity_type); | |
$entity = $storage->createFromStorageRecord($config_record); | |
$entity->save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment