You are here

class CreateNewRevision in Acquia Content Hub 8.2

Prepares the entity for a new revision if it is configured to do so.

@package Drupal\acquia_contenthub\EventSubscriber\PreEntitySave

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\PreEntitySave\CreateNewRevision implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CreateNewRevision

1 file declares its use of CreateNewRevision
CreateNewRevisionTest.php in tests/src/Kernel/EventSubscriber/PreEntitySave/CreateNewRevisionTest.php
1 string reference to 'CreateNewRevision'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses CreateNewRevision
create_new_revision.pre_entity_save in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\PreEntitySave\CreateNewRevision

File

src/EventSubscriber/PreEntitySave/CreateNewRevision.php, line 17

Namespace

Drupal\acquia_contenthub\EventSubscriber\PreEntitySave
View source
class CreateNewRevision implements EventSubscriberInterface {

  /**
   * The stub tracker.
   *
   * @var \Drupal\acquia_contenthub\StubTracker
   */
  protected $stubTracker;

  /**
   * CreateNewRevision constructor.
   *
   * @param \Drupal\acquia_contenthub\StubTracker $stub_tracker
   *   The stub tracker.
   */
  public function __construct(StubTracker $stub_tracker) {
    $this->stubTracker = $stub_tracker;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::PRE_ENTITY_SAVE][] = [
      'onPreEntitySave',
      100,
    ];
    return $events;
  }

  /**
   * Creates a new revision for revisionable entities being imported.
   *
   * @param \Drupal\acquia_contenthub\Event\PreEntitySaveEvent $event
   *   The pre entity save event.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function onPreEntitySave(PreEntitySaveEvent $event) {
    $entity = $event
      ->getEntity();

    // Check whether the entity is configured to create a new revision
    // every time it is saved or if we're saving an entity that
    // has been stubbed.
    $bundle_entity_type = $entity
      ->getEntityType()
      ->getBundleEntityType();
    if (!$bundle_entity_type || $this->stubTracker
      ->hasStub($entity
      ->getEntityTypeId(), $entity
      ->id())) {
      return;
    }
    $bundle = $this
      ->getEntityTypeManager()
      ->getStorage($bundle_entity_type)
      ->load($entity
      ->bundle());
    $should_create_new_revision = $bundle instanceof RevisionableEntityBundleInterface && $bundle
      ->shouldCreateNewRevision();
    if ($entity
      ->getEntityType()
      ->isRevisionable() && $should_create_new_revision) {
      $entity
        ->setNewRevision(TRUE);
    }
  }

  /**
   * Returns uncached entity type manager.
   *
   * Using \Drupal::entityTypeManager() do to caching of the instance in
   * some services. Looks like a core bug.
   *
   * @return \Drupal\Core\Entity\EntityTypeManagerInterface
   *   The entity type manager.
   */
  public function getEntityTypeManager() : EntityTypeManagerInterface {
    return \Drupal::entityTypeManager();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CreateNewRevision::$stubTracker protected property The stub tracker.
CreateNewRevision::getEntityTypeManager public function Returns uncached entity type manager.
CreateNewRevision::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CreateNewRevision::onPreEntitySave public function Creates a new revision for revisionable entities being imported.
CreateNewRevision::__construct public function CreateNewRevision constructor.