You are here

function cms_content_sync_entity_delete in CMS Content Sync 2.0.x

Same name and namespace in other branches
  1. 8 cms_content_sync.module \cms_content_sync_entity_delete()
  2. 2.1.x cms_content_sync.module \cms_content_sync_entity_delete()

Push the entity deletion automatically if configured to do so.

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

Throws

Exception If this entity has been pulled and local deletion is forbidden, this will throw an error.

File

./cms_content_sync.module, line 1176
Module file for cms_content_sync.

Code

function cms_content_sync_entity_delete(EntityInterface $entity) {
  if (!_cms_content_sync_is_installed()) {
    return;
  }

  // Check if deletion has been called by the developer submodule force_deletion
  // drush command.
  if (Drupal::moduleHandler()
    ->moduleExists('cms_content_sync_developer')) {
    if (CliService::$forceEntityDeletion) {
      return;
    }
  }
  if (!EntityHandlerPluginManager::isSupported($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    return;
  }
  if (!Flow::isLocalDeletionAllowed($entity) && !PullIntent::entityHasBeenPulledFromRemoteSite()) {
    throw new Exception($entity
      ->label() . ' cannot be deleted as it has been pulled.');
  }
  $infos = EntityStatus::getInfosForEntity($entity
    ->getEntityTypeId(), $entity
    ->uuid());

  // Can't propagate deletion if the entity was embedded.
  // TODO: Do we need a mechanism to delete these entities from other sites? They will become invisible when their parent entity doesn't reference them
  //       anymore, but it might create waste in the database.
  foreach ($infos as $info) {
    if ($info
      ->wasPushedEmbedded()) {
      return;
    }
  }
  $pushed = PushIntent::pushEntityFromUi($entity, PushIntent::PUSH_AUTOMATICALLY, SyncIntent::ACTION_DELETE);

  // If the entity has been deleted, there will be no "push changes" button, so this content has to be deleted automatically as well.
  $pushed |= PushIntent::pushEntityFromUi($entity, PushIntent::PUSH_MANUALLY, SyncIntent::ACTION_DELETE);

  // If the entity has been deleted as a dependency, it's deletion also has to be pushed.
  $pushed |= PushIntent::pushEntityFromUi($entity, PushIntent::PUSH_AS_DEPENDENCY, SyncIntent::ACTION_DELETE);
  $not_pushed = PushIntent::getNoPushReason($entity);
  if (!empty($not_pushed) && $not_pushed instanceof SyncException) {

    /**
     * @var \Drupal\cms_content_sync\Exception\SyncException $not_pushed
     */
    if ($not_pushed->errorCode === SyncException::CODE_INTERNAL_ERROR || $not_pushed->errorCode === SyncException::CODE_ENTITY_API_FAILURE || $not_pushed->errorCode === SyncException::CODE_PUSH_REQUEST_FAILED || $not_pushed->errorCode === SyncException::CODE_UNEXPECTED_EXCEPTION) {
      Drupal::logger('cms_content_sync')
        ->error($not_pushed
        ->getMessage());
      throw new Exception($entity
        ->label() . ' cannot be deleted as the deletion could not be propagated to the Sync Core. If you need to delete this item right now, edit the Flow and disable "Export deletions" temporarily.');
    }
  }
  $infos = EntityStatus::getInfosForEntity($entity
    ->getEntityTypeId(), $entity
    ->uuid());

  // Entity was pulled, so we inform the Sync Core that it has been deleted on this site.
  if (!empty($not_pushed) && !$not_pushed instanceof \Exception && !empty($infos)) {
    foreach ($infos as $info) {
      if (!$info
        ->wasPulledEmbedded() && $info
        ->getLastPull()) {
        try {
          $info
            ->getPool()
            ->getClient()
            ->getSyndicationService()
            ->deletedLocally($info
            ->getFlow()->id, $entity
            ->getEntityTypeId(), $entity
            ->bundle(), $entity
            ->language()
            ->getId(), $entity
            ->uuid(), EntityHandlerPluginManager::isEntityTypeConfiguration($entity
            ->getEntityTypeId()) ? $entity
            ->id() : NULL);
        } catch (\Exception $e) {
          Drupal::logger('cms_content_sync')
            ->error($e
            ->getMessage());
          \Drupal::messenger(t('Could not update your @repository to mark the entity as deleted. The entity status may be displayed incorrectly or the content may be re-pulled again by accident.', [
            '@repository' => _cms_content_sync_get_repository_name(),
          ]));
        }
        break;
      }
    }
  }
  foreach ($infos as $info) {
    $info
      ->isDeleted(TRUE);
    $info
      ->save();
  }
}