Commit 4ce2a06c authored by Jonathan Smith's avatar Jonathan Smith
Browse files

Issue #3243205 by jonathan1055, chr.fritsch: Add missing publish_state and...

Issue #3243205 by jonathan1055, chr.fritsch: Add missing publish_state and unpublish_state fields - hook_update and hook_modules_installed
parent 2cf8da34
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Installation file for SCMI module.
 */

/**
 * Add publish state and unpublish state field to newly supported entity types.
 */
function scheduler_content_moderation_integration_update_9001() {
  $output = _scheduler_content_moderation_integration_add_fields();
  return $output ? implode('</li><li>', $output) : t('No update required.');
}
+58 −0
Original line number Diff line number Diff line
@@ -117,6 +117,64 @@ function scheduler_content_moderation_integration_entity_base_field_info(EntityT
  return $fields;
}

/**
 * Add publish state and unpublish state fields to supported entity types.
 *
 * This is called from hook_update_9001 and hook_modules_installed().
 */
function _scheduler_content_moderation_integration_add_fields() {
  $entityUpdateManager = \Drupal::entityDefinitionUpdateManager();
  $list = $entityUpdateManager->getChangeList();
  $output = [];
  foreach ($list as $entity_type_id => $definitions) {
    if (($definitions['field_storage_definitions']['publish_state'] ?? 0) || ($definitions['field_storage_definitions']['unpublish_state'] ?? 0)) {
      $entity_type = $entityUpdateManager->getEntityType($entity_type_id);
      $fields = scheduler_content_moderation_integration_entity_base_field_info($entity_type);
      foreach ($fields as $field_name => $field_definition) {
        $entityUpdateManager->installFieldStorageDefinition($field_name, $entity_type_id, $entity_type_id, $field_definition);
      }
      \Drupal::logger('SCMI')->notice('%entity entity type updated with %publish_state and %unpublish_state fields for Scheduler Content Moderation Integration.', [
        '%entity' => $entity_type->getLabel(),
        '%publish_state' => $fields['publish_state']->getLabel(),
        '%unpublish_state' => $fields['unpublish_state']->getLabel(),
      ]);
      $output[] = (string) $entity_type->getLabel();
    }
  }
  return $output;
}

/**
 * Implements hook_modules_installed().
 */
function scheduler_content_moderation_integration_modules_installed($modules) {
  /** @var \Drupal\scheduler\SchedulerManager $scheduler_manager */
  $scheduler_manager = \Drupal::service('scheduler.manager');
  $scheduler_manager->invalidatePluginCache();

  // If there is a Scheduler plugin for a newly installed module then update
  // the base tables adding publish_state and unpublish_state for that entity
  // type. Third-party modules can provide Scheduler plugins for entity types
  // that are not defined by that module, or that do not have the same id as the
  // module name. Similarly, core modules define entity types for which
  // Scheduler provides the plugin. Hence we need to check both the plugin
  // entity type and the provider and if either of these match a module that is
  // being installed then run the update functions.
  $matches = [];
  $plugin_definitions = $scheduler_manager->getPluginDefinitions();
  foreach ($plugin_definitions as $definition) {
    // If the plugin entity type, provider or dependency match any of the
    // modules being installed then add the entity type to the $matches list.
    if (array_intersect([$definition['entityType'], $definition['provider'], $definition['dependency']], $modules)) {
      $matches[] = $definition['entityType'];
    }
  }
  if (!empty($matches)) {
    // Add the database fields.
    _scheduler_content_moderation_integration_add_fields();
  }
}

/**
 * Implements hook_form_alter().
 */