You are here

class ParagraphLineageRevisioner in Paragraphs Edit 8.2

ParagraphLineageRevisioner class.

Hierarchy

Expanded class hierarchy of ParagraphLineageRevisioner

1 string reference to 'ParagraphLineageRevisioner'
paragraphs_edit.services.yml in ./paragraphs_edit.services.yml
paragraphs_edit.services.yml
1 service uses ParagraphLineageRevisioner
paragraphs_edit.lineage.revisioner in ./paragraphs_edit.services.yml
Drupal\paragraphs_edit\ParagraphLineageRevisioner

File

src/ParagraphLineageRevisioner.php, line 14

Namespace

Drupal\paragraphs_edit
View source
class ParagraphLineageRevisioner {
  protected $lineageInspector;

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

  /**
   * Construct a new ParagraphLineageRevisioner object.
   *
   * @param \Drupal\paragraphs_edit\ParagraphLineageInspector $lineage_inspector
   *   Provides paragraphs_edit.lineage.inspector service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Provides  service.
   */
  public function __construct(ParagraphLineageInspector $lineage_inspector, EntityTypeManagerInterface $entity_type_manager) {
    $this->lineageInspector = $lineage_inspector;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Saves all of given entity's lineage as new revisions.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity whose lineage to save as new revisions.
   *
   * @return int
   *   Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
   */
  public function saveNewRevision(ContentEntityInterface $entity) {
    $result = $this
      ->doSaveNewRevision($entity);
    while ($entity instanceof ParagraphInterface) {
      $parent = $entity
        ->getParentEntity();
      if (!$parent) {
        break;
      }
      $this
        ->doSaveNewRevision($parent);
      $entity = $parent;
    }
    return $result;
  }

  /**
   * Saves an entity as a new revision.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity to save.
   *
   * @return int
   *   Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
   */
  protected function doSaveNewRevision(ContentEntityInterface $entity) {

    // Get the parent field item before saving, as after saving the
    // revision ID will be changed.
    if ($entity instanceof ParagraphInterface) {
      $parent_field_item = $this->lineageInspector
        ->getParentFieldItem($entity);
    }
    try {
      $entity
        ->setNewRevision();
    } catch (\LogicException $e) {

      // A content entity not necessarily supports revisioning.
    }
    $status = $entity
      ->save();
    if (isset($parent_field_item)) {
      $parent_field_item
        ->set('target_revision_id', $entity
        ->getRevisionId());
    }
    return $status;
  }

  /**
   * Checks if a given entity should be saved as a new revision by default.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to check for default new revision.
   *
   * @return bool
   *   TRUE if this entity is set to be saved as a new revision by default,
   *   FALSE otherwise.
   */
  public function shouldCreateNewRevision(EntityInterface $entity) {
    $new_revision_default = FALSE;
    if ($bundle_entity_type = $entity
      ->getEntityType()
      ->getBundleEntityType()) {
      $bundle_entity = $this->entityTypeManager
        ->getStorage($bundle_entity_type)
        ->load($entity
        ->bundle());
      if ($bundle_entity instanceof RevisionableEntityBundleInterface) {
        $new_revision_default = $bundle_entity
          ->shouldCreateNewRevision();
      }
    }
    return $new_revision_default;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParagraphLineageRevisioner::$entityTypeManager protected property The entity type manager.
ParagraphLineageRevisioner::$lineageInspector protected property
ParagraphLineageRevisioner::doSaveNewRevision protected function Saves an entity as a new revision.
ParagraphLineageRevisioner::saveNewRevision public function Saves all of given entity's lineage as new revisions.
ParagraphLineageRevisioner::shouldCreateNewRevision public function Checks if a given entity should be saved as a new revision by default.
ParagraphLineageRevisioner::__construct public function Construct a new ParagraphLineageRevisioner object.