"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.9/core/modules/content_moderation/content_moderation.post_update.php" (18 Nov 2020, 8126 Bytes) of package /linux/www/drupal-8.9.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 
    3 /**
    4  * @file
    5  * Post update functions for the Content Moderation module.
    6  */
    7 
    8 use Drupal\Core\Config\Entity\ConfigEntityUpdater;
    9 use Drupal\Core\Entity\Entity\EntityFormDisplay;
   10 use Drupal\Core\Site\Settings;
   11 use Drupal\views\Entity\View;
   12 use Drupal\workflows\Entity\Workflow;
   13 
   14 /**
   15  * Synchronize moderation state default revisions with their host entities.
   16  */
   17 function content_moderation_post_update_update_cms_default_revisions(&$sandbox) {
   18   // For every moderated entity, identify the default revision ID, track the
   19   // corresponding "content_moderation_state" revision and save it as the new
   20   // default revision, if needed.
   21 
   22   // Initialize sandbox info.
   23   $entity_type_id = &$sandbox['entity_type_id'];
   24   if (!isset($entity_type_id)) {
   25     $sandbox['bundles'] = [];
   26     $sandbox['entity_type_ids'] = [];
   27     /** @var \Drupal\workflows\WorkflowInterface $workflow */
   28     foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
   29       /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
   30       $plugin = $workflow->getTypePlugin();
   31       foreach ($plugin->getEntityTypes() as $entity_type_id) {
   32         $sandbox['entity_type_ids'][$entity_type_id] = $entity_type_id;
   33         foreach ($plugin->getBundlesForEntityType($entity_type_id) as $bundle) {
   34           $sandbox['bundles'][$entity_type_id][$bundle] = $bundle;
   35         }
   36       }
   37     }
   38     $sandbox['offset'] = 0;
   39     $sandbox['limit'] = Settings::get('entity_update_batch_size', 50);
   40     $sandbox['total'] = count($sandbox['entity_type_ids']);
   41     $entity_type_id = array_shift($sandbox['entity_type_ids']);
   42   }
   43 
   44   // If there are no moderated bundles or we processed all of them, we are done.
   45   $entity_type_manager = \Drupal::entityTypeManager();
   46   /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $content_moderation_state_storage */
   47   $content_moderation_state_storage = $entity_type_manager->getStorage('content_moderation_state');
   48   if (!$entity_type_id) {
   49     $content_moderation_state_storage->resetCache();
   50     $sandbox['#finished'] = 1;
   51     return;
   52   }
   53 
   54   // Retrieve a batch of moderated entities to be processed.
   55   $storage = $entity_type_manager->getStorage($entity_type_id);
   56   $entity_type = $entity_type_manager->getDefinition($entity_type_id);
   57   $query = $storage->getQuery()
   58     ->accessCheck(FALSE)
   59     ->sort($entity_type->getKey('id'))
   60     ->range($sandbox['offset'], $sandbox['limit']);
   61   $bundle_key = $entity_type->getKey('bundle');
   62   if ($bundle_key && !empty($sandbox['bundles'][$entity_type_id])) {
   63     $bundles = array_keys($sandbox['bundles'][$entity_type_id]);
   64     $query->condition($bundle_key, $bundles, 'IN');
   65   }
   66   $entity_ids = $query->execute();
   67 
   68   // Compute progress status and skip to the next entity type, if needed.
   69   $sandbox['#finished'] = ($sandbox['total'] - count($sandbox['entity_type_ids']) - 1) / $sandbox['total'];
   70   if (!$entity_ids) {
   71     $sandbox['offset'] = 0;
   72     $entity_type_id = array_shift($sandbox['entity_type_ids']) ?: FALSE;
   73     return;
   74   }
   75 
   76   // Load the "content_moderation_state" revisions corresponding to the
   77   // moderated entity default revisions.
   78   $result = $content_moderation_state_storage->getQuery()
   79     ->allRevisions()
   80     ->condition('content_entity_type_id', $entity_type_id)
   81     ->condition('content_entity_revision_id', array_keys($entity_ids), 'IN')
   82     ->execute();
   83   /** @var \Drupal\Core\Entity\ContentEntityInterface[] $revisions */
   84   $revisions = $content_moderation_state_storage->loadMultipleRevisions(array_keys($result));
   85 
   86   // Update "content_moderation_state" data.
   87   foreach ($revisions as $revision) {
   88     if (!$revision->isDefaultRevision()) {
   89       $revision->setNewRevision(FALSE);
   90       $revision->isDefaultRevision(TRUE);
   91       $content_moderation_state_storage->save($revision);
   92     }
   93   }
   94 
   95   // Clear static cache to avoid memory issues.
   96   $storage->resetCache($entity_ids);
   97 
   98   $sandbox['offset'] += $sandbox['limit'];
   99 }
  100 
  101 /**
  102  * Set the default moderation state for new content to 'draft'.
  103  */
  104 function content_moderation_post_update_set_default_moderation_state(&$sandbox) {
  105   \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'workflow', function (Workflow $workflow) {
  106     if ($workflow->get('type') === 'content_moderation') {
  107       $configuration = $workflow->getTypePlugin()->getConfiguration();
  108       $configuration['default_moderation_state'] = 'draft';
  109       $workflow->getTypePlugin()->setConfiguration($configuration);
  110       return TRUE;
  111     }
  112     return FALSE;
  113   });
  114 }
  115 
  116 /**
  117  * Set the filter on the moderation view to be the latest translation affected.
  118  */
  119 function content_moderation_post_update_set_views_filter_latest_translation_affected_revision(&$sandbox) {
  120   $original_plugin_name = 'latest_revision';
  121   $new_plugin_name = 'latest_translation_affected_revision';
  122 
  123   // Check that views is installed and the moderated content view exists.
  124   if (\Drupal::moduleHandler()->moduleExists('views') && $view = View::load('moderated_content')) {
  125     $display = &$view->getDisplay('default');
  126     if (!isset($display['display_options']['filters'][$original_plugin_name])) {
  127       return;
  128     }
  129 
  130     $translation_affected_filter = [
  131       'id' => $new_plugin_name,
  132       'field' => $new_plugin_name,
  133       'plugin_id' => $new_plugin_name,
  134     ] + $display['display_options']['filters'][$original_plugin_name];
  135 
  136     $display['display_options']['filters'] = [$new_plugin_name => $translation_affected_filter] + $display['display_options']['filters'];
  137     unset($display['display_options']['filters'][$original_plugin_name]);
  138 
  139     $view->save();
  140   }
  141 }
  142 
  143 /**
  144  * Update the dependencies of entity displays to include associated workflow.
  145  */
  146 function content_moderation_post_update_entity_display_dependencies(&$sandbox) {
  147   /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
  148   $moderation_info = \Drupal::service('content_moderation.moderation_information');
  149   /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  150   $entity_type_manager = \Drupal::service('entity_type.manager');
  151 
  152   \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'entity_form_display', function (EntityFormDisplay $entity_form_display) use ($moderation_info, $entity_type_manager) {
  153     $associated_entity_type = $entity_type_manager->getDefinition($entity_form_display->getTargetEntityTypeId());
  154 
  155     if ($moderation_info->isModeratedEntityType($associated_entity_type)) {
  156       $entity_form_display->calculateDependencies();
  157       return TRUE;
  158     }
  159     elseif ($moderation_state_component = $entity_form_display->getComponent('moderation_state')) {
  160       // Remove the component from the entity form display, then manually delete
  161       // it from the hidden components list, completely purging it.
  162       $entity_form_display->removeComponent('moderation_state');
  163       $hidden_components = $entity_form_display->get('hidden');
  164       unset($hidden_components['moderation_state']);
  165       $entity_form_display->set('hidden', $hidden_components);
  166       $entity_form_display->calculateDependencies();
  167       return TRUE;
  168     }
  169 
  170     return FALSE;
  171   });
  172 }
  173 
  174 /**
  175  * Update the moderation state views field plugin ID.
  176  */
  177 function content_moderation_post_update_views_field_plugin_id(&$sandbox) {
  178   // If Views is not installed, there is nothing to do.
  179   if (!\Drupal::moduleHandler()->moduleExists('views')) {
  180     return;
  181   }
  182 
  183   \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'view', function ($view) {
  184     /** @var \Drupal\views\ViewEntityInterface $view */
  185     $updated = FALSE;
  186     $displays = $view->get('display');
  187     foreach ($displays as &$display) {
  188       if (empty($display['display_options']['fields'])) {
  189         continue;
  190       }
  191       foreach ($display['display_options']['fields'] as &$display_field) {
  192         if ($display_field['id'] === 'moderation_state' && $display_field['plugin_id'] === 'field') {
  193           $display_field['plugin_id'] = 'moderation_state_field';
  194           $updated = TRUE;
  195         }
  196       }
  197     }
  198     $view->set('display', $displays);
  199     return $updated;
  200   });
  201 }