Commit 451d0163 authored by Jonathan Smith's avatar Jonathan Smith Committed by Jonathan Smith
Browse files

Issue #3259200 by jonathan1055: Remove Paragraph plugin

parent adf16fbf
Loading
Loading
Loading
Loading
+14 −0
Changes for scheduler.install: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -224,3 +224,17 @@ function scheduler_update_8204() {
function scheduler_update_8205() {
  return scheduler_update_8201();
}

/**
 * Remove Scheduler fields and third_party_settings for Paragraph entity types.
 */
function scheduler_update_8206() {
  $scheduler_manager = \Drupal::service('scheduler.manager');
  // Check the module is enabled, to avoid 'Unknown entity type' message.
  if (\Drupal::moduleHandler()->moduleExists('paragraph') && ($result = $scheduler_manager->entityRevert(['paragraph']))) {
    return t('%updated.', ['%updated' => implode(', ', $result)]);
  }
  else {
    return t('No Paragraph entity types require reverting.');
  }
}
+19 −1
Changes for src/Commands/SchedulerCommands.php: 19 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ namespace Drupal\scheduler\Commands;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\scheduler\SchedulerManager;
use Drush\Commands\DrushCommands;
use Drush\Utils\StringUtils;

/**
 * Drush 9 Scheduler commands for Drupal Core 8.4+.
@@ -67,7 +68,7 @@ class SchedulerCommands extends DrushCommands {
   * Use the standard drush parameter -q for quiet mode (no terminal output).
   *
   * @command scheduler:entity-update
   * @aliases sch-ent-upd, scheduler-entity-update
   * @aliases sch-ent-upd, sch-upd, scheduler-entity-update
   */
  public function entityUpdate() {
    $result = $this->schedulerManager->entityUpdate();
@@ -75,4 +76,21 @@ class SchedulerCommands extends DrushCommands {
    $this->messenger->addMessage(dt('Scheduler entity update - @updated', ['@updated' => $updated]));
  }

  /**
   * Entity Revert - remove Scheduler fields and third-party-settings.
   *
   * Use the standard drush parameter -q for quiet mode (no terminal output).
   *
   * @option types A comma-delimited list of entity type ids. Default is all
   *    entity types that need reverting.
   *
   * @command scheduler:entity-revert
   * @aliases sch-ent-rev, sch-rev, scheduler-entity-revert
   */
  public function entityRevert(array $options = ['types' => '']) {
    $result = $this->schedulerManager->entityRevert(StringUtils::csvToArray($options['types']));
    $reverted = $result ? implode(', ', $result) : dt('nothing to do');
    $this->messenger->addMessage(dt('Scheduler entity revert - @reverted', ['@reverted' => $reverted]));
  }

}
+0 −96
Changes for src/Event/SchedulerParagraphEvents.php: 0 added lines, 96 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\scheduler\Event;

/**
 * Lists the six events dispatched by Scheduler relating to Paragraph entities.
 */
final class SchedulerParagraphEvents {

  /**
   * The event triggered after a paragraph is published immediately.
   *
   * This event allows modules to react after an entity is published
   * immediately when being saved after editing. The event listener method
   * receives a \Drupal\Core\Entity\EntityInterface instance.
   *
   * @Event
   *
   * @see \Drupal\scheduler\Event\SchedulerEvent
   *
   * @var string
   */
  const PUBLISH_IMMEDIATELY = 'scheduler.paragraph_publish_immediately';

  /**
   * The event triggered after a paragraph is published by cron.
   *
   * This event allows modules to react after an entity is published by Cron.
   * The event listener receives a \Drupal\Core\Entity\EntityInterface instance.
   *
   * @Event
   *
   * @see \Drupal\scheduler\Event\SchedulerEvent
   *
   * @var string
   */
  const PUBLISH = 'scheduler.paragraph_publish';

  /**
   * The event triggered before a paragraph is published immediately.
   *
   * This event allows modules to react before an entity is published
   * immediately when being saved after editing. The event listener method
   * receives a \Drupal\Core\Entity\EntityInterface instance.
   *
   * @Event
   *
   * @see \Drupal\scheduler\Event\SchedulerEvent
   *
   * @var string
   */
  const PRE_PUBLISH_IMMEDIATELY = 'scheduler.paragraph_pre_publish_immediately';

  /**
   * The event triggered before a paragraph is published by cron.
   *
   * This event allows modules to react before an entity is published by Cron.
   * The event listener receives a \Drupal\Core\Entity\EntityInterface instance.
   *
   * @Event
   *
   * @see \Drupal\scheduler\Event\SchedulerEvent
   *
   * @var string
   */
  const PRE_PUBLISH = 'scheduler.paragraph_pre_publish';

  /**
   * The event triggered before a paragraph is unpublished by cron.
   *
   * This event allows modules to react before an entity is unpublished by Cron.
   * The event listener receives a \Drupal\Core\Entity\EntityInterface instance.
   *
   * @Event
   *
   * @see \Drupal\scheduler\Event\SchedulerEvent
   *
   * @var string
   */
  const PRE_UNPUBLISH = 'scheduler.paragraph_pre_unpublish';

  /**
   * The event triggered after a paragraph is unpublished by cron.
   *
   * This event allows modules to react after an entity is unpublished by Cron.
   * The event listener receives a \Drupal\Core\Entity\EntityInterface instance.
   *
   * @Event
   *
   * @see \Drupal\scheduler\Event\SchedulerEvent
   *
   * @var string
   */
  const UNPUBLISH = 'scheduler.paragraph_unpublish';

}
+0 −21
Changes for src/Plugin/Scheduler/ParagraphScheduler.php: 0 added lines, 21 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\scheduler\Plugin\Scheduler;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\scheduler\SchedulerPluginBase;

/**
 * Plugin for Paragraphs entity type.
 *
 * @package Drupal\Scheduler\Plugin\Scheduler
 *
 * @SchedulerPlugin(
 *  id = "paragraph_scheduler",
 *  label = @Translation("Paragraphs Scheduler Plugin"),
 *  description = @Translation("Provides support for scheduling paragraphs entities"),
 *  entityType = "paragraph",
 *  dependency = "paragraphs",
 * )
 */
class ParagraphScheduler extends SchedulerPluginBase implements ContainerFactoryPluginInterface {}
+88 −0
Changes for src/SchedulerManager.php: 88 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1248,4 +1248,92 @@ class SchedulerManager {
    return $updated;
  }

  /**
   * Reverts entity types that are no longer supported by Scheduler plugins.
   *
   * In normal situations this function is not required. However in the case
   * when a plugin (either provided by Scheduler or another modules) is removed
   * after being used, the db fields and third-party-settings remain and have to
   * be deleted. This function was added to clean up the Paragraphs entity type
   * but has been made generic for future use. It is called from a hook_update()
   * and can also be run via drush command scheduler:entity-revert.
   * See https://www.drupal.org/project/scheduler/issues/3259200
   *
   * @param array $only_these_types
   *   Optional list of entity type ids to restrict the updates. If none given
   *   then reverts all applicable entity types that have schema changes showing
   *   that the db fields need to be removed.
   *
   * @return array
   *   Messages about the entity types reverted.
   */
  public function entityRevert(array $only_these_types = []) {
    // Find all changed entity definitions.
    $entityUpdateManager = \Drupal::entityDefinitionUpdateManager();
    $changeList = $entityUpdateManager->getChangeList();

    $output = [];
    if ($only_these_types) {
      // First remove any non-existent entity types requested.
      $all_entity_types = array_keys($this->entityTypeManager->getDefinitions());
      if ($unknown = array_diff($only_these_types, $all_entity_types)) {
        $output['unknown'] = $this->t('Unknown entity types (@unknown)', ['@unknown' => implode(' ', $unknown)]);
      }
      $entity_type_ids = array_intersect($only_these_types, $all_entity_types);
    }
    else {
      // Nothing given. Get the list of changed entity types.
      $entity_type_ids = array_keys($changeList);
    }
    // Remove any requested entity types that do have enabled plugins, as these
    // must not be reverted.
    $supported_types = $this->getPluginEntityTypes();
    $entity_type_ids = array_diff($entity_type_ids, $supported_types);

    foreach ($entity_type_ids as $entity_type_id) {
      $entityType = $this->entityTypeManager->getDefinition($entity_type_id);
      $bundleType = $entityType->getBundleEntityType();

      // Remove the Scheduler fields from the entity type if they are shown in
      // the changeList as 'deleted'.
      if (isset($changeList[$entity_type_id]['field_storage_definitions'])) {
        foreach (['publish_on', 'unpublish_on'] as $field_name) {
          $change = ($changeList[$entity_type_id]['field_storage_definitions'][$field_name] ?? NULL);
          // If the field is marked as deleted then remove it.
          if ($change == $entityUpdateManager::DEFINITION_DELETED && $field = $entityUpdateManager->getFieldStorageDefinition($field_name, $entity_type_id)) {
            $entityUpdateManager->uninstallFieldStorageDefinition($field);
            $output["$entity_type_id fields"] = $this->t('Scheduler fields removed from @entityType', [
              '@entityType' => $entityType->getLabel(),
            ]);
            $this->logger->notice('%field field removed from %entityType entity type', [
              '%field' => $field->getLabel(),
              '%entityType' => $entityType->getLabel(),
            ]);
          }
        }
      }

      // Remove Scheduler third-party-settings from each bundle.
      foreach ($this->entityTypeManager->getStorage($bundleType)->loadMultiple() as $bundle) {
        // Remove each third_party_setting. The last one to be removed will also
        // cause the 'scheduler' top-level array to be deleted.
        $third_party_settings = $bundle->getThirdPartySettings('scheduler');
        if ($third_party_settings) {
          foreach (array_keys($third_party_settings) as $setting) {
            $bundle->unsetThirdPartySetting('scheduler', $setting)->save();
          }
          $this->logger->notice('Scheduler settings removed from %entity %bundle', [
            '%entity' => $bundle->getEntityType()->getLabel(),
            '%bundle' => $bundle->label(),
          ]);
          $output["{$bundle->id()} settings"] = $this->t('@bundle settings removed', [
            '@bundle' => $bundle->label(),
          ]);
        }
      }
    }

    return $output;
  }

}