You are here

class UpdateUtils in Scheduled Updates 8

Service to determine information about Scheduled Update Types.

Hierarchy

Expanded class hierarchy of UpdateUtils

1 file declares its use of UpdateUtils
BaseUpdateRunner.php in src/Plugin/BaseUpdateRunner.php
Contains \Drupal\scheduled_updates\Plugin\BaseUpdateRunner.
1 string reference to 'UpdateUtils'
scheduled_updates.services.yml in ./scheduled_updates.services.yml
scheduled_updates.services.yml
1 service uses UpdateUtils
scheduled_updates.update_utils in ./scheduled_updates.services.yml
\Drupal\scheduled_updates\UpdateUtils

File

src/UpdateUtils.php, line 14

Namespace

Drupal\scheduled_updates
View source
class UpdateUtils implements UpdateUtilsInterface {
  use ClassUtilsTrait;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $bundleInfo;

  /**
   * UpdateUtils constructor.
   *
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInfo
   */
  public function __construct(EntityFieldManagerInterface $entityFieldManager, EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $bundleInfo) {
    $this->entityFieldManager = $entityFieldManager;
    $this->entityTypeManager = $entityTypeManager;
    $this->bundleInfo = $bundleInfo;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsRevisionUpdates(ScheduledUpdateTypeInterface $scheduledUpdateType) {
    $type_definition = $this
      ->getUpdateTypeDefinition($scheduledUpdateType);
    if ($type_definition && $type_definition
      ->isRevisionable()) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsRevisionBundleDefault(ScheduledUpdateTypeInterface $scheduledUpdateType) {
    if ($type_definition = $this
      ->getUpdateTypeDefinition($scheduledUpdateType)) {
      $bundle_type_id = $type_definition
        ->getBundleEntityType();
      $bundle_class = $this->entityTypeManager
        ->getDefinition($bundle_type_id)
        ->getClass();

      // Core doesn't have a standard method for determining if entities of bundle should have new revisions.
      // This should be updated if an interface is create for this.
      // @todo Check Entity API has a solution for this and also check it's interface.
      if ($this
        ->implementsInterface($bundle_class, [
        'Drupal\\node\\NodeTypeInterface',
      ])) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function isRevisionableUpdate(ScheduledUpdateInterface $update) {
    return $this
      ->supportsRevisionUpdates($this
      ->getUpdateType($update));
  }

  /**
   * {@inheritdoc}
   */
  public function getUpdateType(ScheduledUpdateInterface $update) {
    return $this->entityTypeManager
      ->getStorage('scheduled_update_type')
      ->load($update
      ->bundle());
  }

  /**
   * {@inheritdoc}
   */
  public function getUpdateTypeLabel(ScheduledUpdateInterface $update) {
    return $this
      ->getUpdateType($update)
      ->label();
  }

  /**
   * {@inheritdoc}
   */
  public function getRevisionDefault(ContentEntityInterface $entity_to_update) {
    $bundle_type_id = $entity_to_update
      ->getEntityType()
      ->getBundleEntityType();
    $bundle = $this->entityTypeManager
      ->getStorage($bundle_type_id)
      ->load($entity_to_update
      ->bundle());

    // This should exist because of previous check in
    // supportsRevisionBundleDefault but just in case.
    if ($bundle instanceof NodeTypeInterface) {
      return $bundle
        ->isNewRevision();
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getLatestRevision($entity_type_id, $entity_id) {
    if ($latest_revision_id = $this
      ->getLatestRevisionId($entity_type_id, $entity_id)) {
      return $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->loadRevision($latest_revision_id);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getLatestRevisionId($entity_type_id, $entity_id) {
    if ($storage = $this->entityTypeManager
      ->getStorage($entity_type_id)) {
      $revision_ids = $storage
        ->getQuery()
        ->allRevisions()
        ->condition($this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getKey('id'), $entity_id)
        ->sort($this->entityTypeManager
        ->getDefinition($entity_type_id)
        ->getKey('revision'), 'DESC')
        ->pager(1)
        ->execute();
      if ($revision_ids) {
        $revision_id = array_keys($revision_ids)[0];
        return $revision_id;
      }
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setRevisionCreationTime(ContentEntityInterface $entity) {
    $revision_timestamp_interfaces = [
      'Drupal\\entity\\Revision\\EntityRevisionLogInterface',
      'Drupal\\node\\NodeInterface',
    ];
    if ($this
      ->implementsInterface($entity, $revision_timestamp_interfaces)) {

      /** @var \Drupal\entity\Revision\EntityRevisionLogInterface|\Drupal\node\NodeInterface $entity */
      $entity
        ->setRevisionCreationTime(\Drupal::time()
        ->getRequestTime());
    }
  }

  /**
   * {@inheritdoc}
   */
  public function supportsRevisionOwner(ScheduledUpdateTypeInterface $scheduledUpdateType) {
    if ($definition = $this
      ->getUpdateTypeDefinition($scheduledUpdateType)) {
      return $this
        ->definitionClassImplementsInterface($definition, $this
        ->revisionOwnerInterfaces());
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsOwner(ScheduledUpdateTypeInterface $scheduledUpdateType) {
    if ($type = $this
      ->getUpdateTypeDefinition($scheduledUpdateType)) {
      return $this
        ->definitionClassImplementsInterface($type, [
        'Drupal\\user\\EntityOwnerInterface',
      ]);
    }
    return FALSE;
  }

  /**
   * Get the entity definition for the entity to be updated.
   *
   * @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $scheduledUpdateType
   *
   * @return array|\Drupal\Core\Entity\EntityTypeInterface|null
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function getUpdateTypeDefinition(ScheduledUpdateTypeInterface $scheduledUpdateType) {
    if ($update_entity_type = $scheduledUpdateType
      ->getUpdateEntityType()) {
      return $this->entityTypeManager
        ->getDefinition($update_entity_type);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getPreviousRevision(ContentEntityInterface $entity) {
    $storage = $this->entityTypeManager
      ->getStorage($entity
      ->getEntityTypeId());
    $query = $storage
      ->getQuery();
    $type = $entity
      ->getEntityType();
    $query
      ->allRevisions()
      ->condition($type
      ->getKey('id'), $entity
      ->id())
      ->condition($type
      ->getKey('revision'), $entity
      ->getRevisionId(), '<')
      ->sort($type
      ->getKey('revision'), 'DESC')
      ->pager(1);
    $revision_ids = $query
      ->execute();
    if ($revision_ids) {
      $revision_id = array_keys($revision_ids)[0];
      return $storage
        ->loadRevision($revision_id);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function bundleOptions($entity_type) {
    $info = $this->bundleInfo
      ->getBundleInfo($entity_type);
    $options = [];
    foreach ($info as $bundle => $bundle_info) {
      $options[$bundle] = $bundle_info['label'];
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClassUtilsTrait::bundleLabel protected function
ClassUtilsTrait::definitionClassImplementsInterface protected function Determines if the class for an entity type definition implements and interface.
ClassUtilsTrait::entityLabel protected function
ClassUtilsTrait::entityTypeManager protected function
ClassUtilsTrait::getEntityOwner protected function Get the entity owner if applicable.
ClassUtilsTrait::getRevisionOwner protected function Get the revision owner for an ContentEntity.
ClassUtilsTrait::implementsInterface protected function Determines if an object or class name implements any interfaces in a list.
ClassUtilsTrait::revisionOwnerInterfaces protected function Get class names of interfaces that support revision ownership.
ClassUtilsTrait::targetSupportBundles protected function
ClassUtilsTrait::targetTypeBundleLabel protected function
ClassUtilsTrait::targetTypeLabel public function
ClassUtilsTrait::typeSupportsBundles protected function
UpdateUtils::$bundleInfo protected property The bundle info service.
UpdateUtils::$entityFieldManager protected property The entity field manager.
UpdateUtils::$entityTypeManager protected property The entity type manager.
UpdateUtils::bundleOptions public function Create select element bundle options for entity type. Overrides UpdateUtilsInterface::bundleOptions
UpdateUtils::getLatestRevision public function Loads the latest revision of a specific entity. Overrides UpdateUtilsInterface::getLatestRevision
UpdateUtils::getLatestRevisionId public function Returns the revision ID of the latest revision of the given entity. Overrides UpdateUtilsInterface::getLatestRevisionId
UpdateUtils::getPreviousRevision public function Get the directly previous revision. Overrides UpdateUtilsInterface::getPreviousRevision
UpdateUtils::getRevisionDefault public function Get whether a new revision should be created by default for this entity. Overrides UpdateUtilsInterface::getRevisionDefault
UpdateUtils::getUpdateType public function Get the update type for an update. Overrides UpdateUtilsInterface::getUpdateType
UpdateUtils::getUpdateTypeDefinition protected function Get the entity definition for the entity to be updated.
UpdateUtils::getUpdateTypeLabel public function Overrides UpdateUtilsInterface::getUpdateTypeLabel
UpdateUtils::isRevisionableUpdate public function Determines if an update supports revisions Overrides UpdateUtilsInterface::isRevisionableUpdate
UpdateUtils::setRevisionCreationTime public function Set revision creation time for entities that support it. Overrides UpdateUtilsInterface::setRevisionCreationTime
UpdateUtils::supportsOwner public function Determines if the entity type being updated supports ownership. Overrides UpdateUtilsInterface::supportsOwner
UpdateUtils::supportsRevisionBundleDefault public function Determine if the entity type being update support default revision setting. Overrides UpdateUtilsInterface::supportsRevisionBundleDefault
UpdateUtils::supportsRevisionOwner public function Determines if entity type being updated supports Revision Ownership. Overrides UpdateUtilsInterface::supportsRevisionOwner
UpdateUtils::supportsRevisionUpdates public function Determine a scheduled update type supports creating new revisions on update. Overrides UpdateUtilsInterface::supportsRevisionUpdates
UpdateUtils::__construct public function UpdateUtils constructor.