You are here

class EntityPublisher in Content Synchronizer 8

Same name and namespace in other branches
  1. 8.2 src/Service/EntityPublisher.php \Drupal\content_synchronizer\Service\EntityPublisher
  2. 3.x src/Service/EntityPublisher.php \Drupal\content_synchronizer\Service\EntityPublisher

THe entity publsher service.

Hierarchy

Expanded class hierarchy of EntityPublisher

1 file declares its use of EntityPublisher
EntityProcessorBase.php in src/Processors/Entity/EntityProcessorBase.php
1 string reference to 'EntityPublisher'
content_synchronizer.services.yml in ./content_synchronizer.services.yml
content_synchronizer.services.yml
1 service uses EntityPublisher
content_synchronizer.entity_publisher in ./content_synchronizer.services.yml
\Drupal\content_synchronizer\Service\EntityPublisher

File

src/Service/EntityPublisher.php, line 12

Namespace

Drupal\content_synchronizer\Service
View source
class EntityPublisher {
  const SERVICE_NAME = 'content_synchronizer.entity_publisher';

  /**
   * Save the entity after import.
   *
   * If the entity is revisionable, it creates a new revision.
   * If the entity is new and is a root entity, then it is unpublished.
   */
  public function saveEntity(Entity $entity, $gid = NULL, $existingEntity = NULL, array $dataToImport = []) {

    // Alter entity before import.
    $entityDataToImport = array_key_exists('translations', $dataToImport) ? $dataToImport['translations'][$entity
      ->language()
      ->getId()] : $dataToImport;
    \Drupal::moduleHandler()
      ->alter(EntityProcessorBase::IMPORT_HOOK, $entity, $existingEntity, $entityDataToImport);

    // Try to create a new revision of the current entity.
    $this
      ->saveEntityWithRevision($entity, $gid, $existingEntity);
  }

  /**
   * Try to create a revision of the entity.
   *
   * @param \Drupal\Core\Entity\Entity $entity
   *   The entity to save.
   * @param string $gid
   *   THe gid.
   * @param \Drupal\Core\Entity\Entity $existingEntity
   *   The existing entity before changes.
   */
  protected function saveEntityWithRevision(Entity $entity, $gid, Entity $existingEntity = NULL) {
    if ($entity
      ->id()) {
      $entityMethods = get_class_methods($entity);
      if (in_array('setRevisionCreationTime', $entityMethods) && in_array('setNewRevision', $entityMethods)) {
        try {
          $revision = clone $entity;
          $revision
            ->setNewRevision(TRUE);
          $revision->revision_log = '[Content Synchronizer] ' . ImportProcessor::getCurrentImportProcessor()
            ->getImport()
            ->label();
          $revision
            ->setRevisionCreationTime(time());
          if ($this
            ->haveToSave($revision, $existingEntity)) {
            $revision
              ->save();
          }
        } catch (\Exception $e) {
          $this
            ->saveEntityWithUnpublishedStatus($entity, $gid, $existingEntity);
        }
      }
      else {
        $this
          ->saveEntityWithUnpublishedStatus($entity, $gid, $existingEntity);
      }
    }
    else {
      $this
        ->saveEntityWithUnpublishedStatus($entity, $gid, $existingEntity);
    }
  }

  /**
   * Try to unpublish entity if it needs to be created. Either default save.
   *
   * @param \Drupal\Core\Entity\Entity $entity
   *   The entity to save.
   * @param string $gid
   *   THe gid.
   * @param \Drupal\Core\Entity\Entity $existingEntity
   *   The existing entity before changes.
   */
  protected function saveEntityWithUnpublishedStatus(Entity $entity, $gid, Entity $existingEntity = NULL) {
    if ($this
      ->isPublishable($entity)) {
      if (!$entity
        ->id()) {
        if (ImportProcessor::getCurrentImportProcessor()
          ->getCreationType() == ImportProcessor::PUBLICATION_UNPUBLISH) {
          $entity
            ->setPublished(FALSE);
        }
        else {
          $entity
            ->setPublished(TRUE);
        }
      }
    }
    $this
      ->defaultSave($entity, $existingEntity);
  }

  /**
   * Save without publish status care.
   *
   * @param \Drupal\Core\Entity\Entity $entity
   *   The entity to save.
   * @param \Drupal\Core\Entity\Entity $existingEntity
   *   The existing entity before changes.
   */
  protected function defaultSave(Entity $entity, Entity $existingEntity = NULL) {
    if ($this
      ->haveToSave($entity, $existingEntity)) {
      if (is_null($entity
        ->uuid())) {
        $entity->uuid = \Drupal::service('uuid')
          ->generate();
      }
      $entity
        ->save();
    }
  }

  /**
   * Check if the entity is publishable.
   *
   * @param \Drupal\Core\Entity\Entity $entity
   *   The entity to check.
   *
   * @return bool
   *   The publishable state.
   */
  protected function isPublishable(Entity $entity) {
    return method_exists($entity, 'setPublished');
  }

  /**
   * Return TRUE if the entity has to be updated.
   *
   * @param \Drupal\Core\Entity\Entity $entity
   *   The imported entity.
   * @param \Drupal\Core\Entity\Entity $existingEntity
   *   The entity before update.
   *
   * @return bool
   *   The state of update.
   */
  protected function haveToSave(Entity $entity, Entity $existingEntity = NULL) {
    $haveToSave = TRUE;

    // Update :
    if ($entity
      ->id()) {
      switch (ImportProcessor::getCurrentImportProcessor()
        ->getUpdateType()) {
        case ImportProcessor::UPDATE_IF_RECENT:
          if ($existingEntity) {
            if (method_exists($entity, 'getChangedTime')) {
              $haveToSave = $entity
                ->getChangedTime() > $existingEntity
                ->getChangedTime();
            }
            else {
              $haveToSave = TRUE;
            }
          }
          else {
            $haveToSave = TRUE;
          }
          break;
        case ImportProcessor::UPDATE_NO_UPDATE:
          $haveToSave = FALSE;
          break;
        case ImportProcessor::UPDATE_SYSTEMATIC:
        default:
          $haveToSave = TRUE;
      }
    }
    return $haveToSave;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityPublisher::defaultSave protected function Save without publish status care.
EntityPublisher::haveToSave protected function Return TRUE if the entity has to be updated.
EntityPublisher::isPublishable protected function Check if the entity is publishable.
EntityPublisher::saveEntity public function Save the entity after import.
EntityPublisher::saveEntityWithRevision protected function Try to create a revision of the entity.
EntityPublisher::saveEntityWithUnpublishedStatus protected function Try to unpublish entity if it needs to be created. Either default save.
EntityPublisher::SERVICE_NAME constant