You are here

function _cms_content_sync_reset_entity in CMS Content Sync 2.1.x

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

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

\Drupal\cms_content_sync\Entity\EntityStatus $status:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to _cms_content_sync_reset_entity()
_cms_content_sync_override_embedded_entity_save_status_entity in ./cms_content_sync.module
_cms_content_sync_override_entity_submit in ./cms_content_sync.module
Entity status update.

File

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

Code

function _cms_content_sync_reset_entity($entity, $status) {
  if ($status
    ->wasPulledEmbedded()) {
    $parent = $status
      ->getParentEntity();
    if (!$parent) {
      Drupal::messenger()
        ->addWarning(t("Overwrite changed but @entity_type_id @bundle %label could not be reset to it's original values as it was embedded and the parent entity doesn't exist.", [
        '@entity_type_id' => $entity
          ->getEntityTypeId(),
        '@bundle' => $entity
          ->bundle(),
        '%label' => $entity
          ->label(),
        '@uuid' => $entity
          ->uuid(),
      ]));
      return;
    }
    $parent_statuses = EntityStatus::getInfosForEntity($parent
      ->getEntityTypeId(), $parent
      ->uuid(), [
      'flow' => $status
        ->getFlow()
        ->id(),
    ]);
    if (empty($parent_statuses)) {
      $parent_statuses = EntityStatus::getInfosForEntity($parent
        ->getEntityTypeId(), $parent
        ->uuid());
    }
    if (empty($parent_statuses)) {
      Drupal::messenger()
        ->addWarning(t("Overwrite changed but @entity_type_id @bundle %label could not be reset to it's original values as it was embedded and the parent entity doesn't have a pull status.", [
        '@entity_type_id' => $entity
          ->getEntityTypeId(),
        '@bundle' => $entity
          ->bundle(),
        '%label' => $entity
          ->label(),
        '@uuid' => $entity
          ->uuid(),
      ]));
      return;
    }

    // We just take the first match as it's the same entity in the Sync Core.
    $parent_status = reset($parent_statuses);
    _cms_content_sync_reset_entity($parent, $parent_status);
    return;
  }
  if ($entity instanceof ConfigEntityInterface) {
    $shared_entity_id = $entity
      ->id();
  }
  else {
    $shared_entity_id = $entity
      ->uuid();
  }
  try {
    $flow = $status
      ->getFlow();
    $manually = $flow
      ->getController()
      ->canPullEntity($entity
      ->getEntityTypeId(), $entity
      ->bundle(), PullIntent::PULL_MANUALLY, SyncIntent::ACTION_CREATE, TRUE);
    $dependency = $flow
      ->getController()
      ->canPullEntity($entity
      ->getEntityTypeId(), $entity
      ->bundle(), PullIntent::PULL_AS_DEPENDENCY, SyncIntent::ACTION_CREATE, TRUE);
    $status
      ->getPool()
      ->getClient()
      ->getSyndicationService()
      ->pullSingle($flow->id, $entity
      ->getEntityTypeId(), $entity
      ->bundle(), $shared_entity_id)
      ->fromPool($status
      ->getPool()->id)
      ->manually($manually)
      ->asDependency($dependency)
      ->execute();
    Drupal::messenger()
      ->addMessage(t('Overwrite changed; @entity_type_id @bundle %label will be pulled again within a few seconds, you may want to <a href="javascript:window.location.href=window.location.href">reload</a> the page to see the changes.', [
      '@entity_type_id' => $entity
        ->getEntityTypeId(),
      '@bundle' => $entity
        ->bundle(),
      '%label' => $entity
        ->label(),
      '@uuid' => $entity
        ->uuid(),
    ]));
  } catch (SyncCoreException $e) {
    Drupal::messenger()
      ->addWarning(t('Overwrite changed, but failed to pull entity @entity_type_id @bundle %label (@uuid): @message', [
      '@entity_type_id' => $entity
        ->getEntityTypeId(),
      '@bundle' => $entity
        ->bundle(),
      '%label' => $entity
        ->label(),
      '@uuid' => $entity
        ->uuid(),
      '@message' => $e
        ->getMessage(),
    ]));
    return;
  }
}