Created
July 14, 2020 00:46
-
-
Save mstrelan/3a971eed6b234ca4c026b7e1738506ac to your computer and use it in GitHub Desktop.
Update base field definitions for scheduler_content_moderation_integration
This file contains hidden or 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 | |
namespace Drupal\my_module\Controller; | |
use Drupal\Core\Controller\ControllerBase; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Returns responses for my_module routes. | |
*/ | |
class MyModuleController extends ControllerBase { | |
/** | |
* The entity definition update manager. | |
* | |
* @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface | |
*/ | |
protected $entityDefinitionUpdateManager; | |
/** | |
* The entity last installed schema repository. | |
* | |
* @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface | |
*/ | |
protected $entityLastInstalledSchemaRepository; | |
/** | |
* The module handler. | |
* | |
* @var \Drupal\Core\Extension\ModuleHandlerInterface | |
*/ | |
protected $moduleHandler; | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container) { | |
$instance = parent::create($container); | |
$instance->entityDefinitionUpdateManager = $container->get('entity.definition_update_manager'); | |
$instance->entityLastInstalledSchemaRepository = $container->get('entity.last_installed_schema.repository'); | |
$instance->moduleHandler = $container->get('module_handler'); | |
return $instance; | |
} | |
/** | |
* Fix schema after installing scheduler_content_moderation_integration. | |
* | |
* After enabling the module via config sync the publish_state and | |
* unpublish_state fields were missing with no way to restore them and no way | |
* to uninstall the module. This function installs the missing fields to allow | |
* the module to be uninstalled. | |
*/ | |
public function fixSchedulerContentModerationIntegration() { | |
$updated = FALSE; | |
if ($this->moduleHandler->moduleExists('scheduler_content_moderation_integration')) { | |
$entity_type = $this->entityDefinitionUpdateManager->getEntityType('node'); | |
$installed = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type->id()); | |
$definitions = scheduler_content_moderation_integration_entity_base_field_info($entity_type); | |
foreach ($definitions as $field_name => $definition) { | |
if (empty($installed[$field_name])) { | |
$this->entityDefinitionUpdateManager->installFieldStorageDefinition($field_name, $entity_type->id(), 'scheduler_content_workbench_moderation_integration', $definition); | |
$this->messenger()->addMessage($this->t('Field %field_name installed.', [ | |
'%field_name' => $field_name, | |
])); | |
$updated = TRUE; | |
} | |
} | |
} | |
if (!$updated) { | |
$this->messenger()->addMessage($this->t('No schema updates required.')); | |
} | |
return $this->redirect('system.status'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment