You are here

protected static function MissingDependencyManager::saveResolvedDependencies in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/MissingDependencyManager.php \Drupal\cms_content_sync\MissingDependencyManager::saveResolvedDependencies()
  2. 2.0.x src/MissingDependencyManager.php \Drupal\cms_content_sync\MissingDependencyManager::saveResolvedDependencies()

Parameters

$entity:

$missing:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to MissingDependencyManager::saveResolvedDependencies()
MissingDependencyManager::resolveDependencies in src/MissingDependencyManager.php
Resolve any dependencies that were missing before for the given entity that is now available.

File

src/MissingDependencyManager.php, line 138

Class

MissingDependencyManager
Class MissingDependencyManagement.

Namespace

Drupal\cms_content_sync

Code

protected static function saveResolvedDependencies($entity, $missing) {
  foreach ($missing as $sync) {
    $infos = EntityStatus::getInfosForEntity($sync[self::INDEX_ENTITY_TYPE], $sync[self::INDEX_ENTITY_ID]);
    foreach ($infos as $info) {
      if ($info
        ->isDeleted()) {
        break;
      }
      if (!$info
        ->getPool() || !$info
        ->getFlow()) {
        continue;
      }
      $referenced_entity = $info
        ->getEntity();
      $flow = $info
        ->getFlow();
      if (!$flow
        ->canPullEntity($referenced_entity
        ->getEntityTypeId(), $referenced_entity
        ->bundle(), PullIntent::PULL_FORCED)) {
        continue;
      }
      if (!empty($sync[self::INDEX_SET_FIELD])) {

        /**
         * @var \Drupal\Core\Entity\FieldableEntityInterface $referenced_entity
         */
        if ('link' === $sync[self::INDEX_SET_FIELD] && 'menu_link_content' === $referenced_entity
          ->getEntityTypeId()) {
          if (isset($sync[self::INDEX_DATA]['enabled']) && $sync[self::INDEX_DATA]['enabled']) {
            $referenced_entity
              ->set('enabled', [
              [
                'value' => 1,
              ],
            ]);
          }
          $data = 'entity:' . $entity
            ->getEntityTypeId() . '/' . $entity
            ->id();
        }
        elseif ('crop' === $referenced_entity
          ->getEntityTypeId() && 'entity_id' === $sync[self::INDEX_SET_FIELD]) {
          $data = [
            'value' => $entity
              ->id(),
          ];
        }
        else {
          $data = [
            'target_id' => $entity
              ->id(),
          ];
        }
        $referenced_entity
          ->set($sync[self::INDEX_SET_FIELD], $data);
        $referenced_entity
          ->save();
        break;
      }
      try {
        $info
          ->getPool()
          ->getClient()
          ->getSyndicationService()
          ->pullSingle($flow->id, $referenced_entity
          ->getEntityTypeId(), $referenced_entity
          ->bundle(), $referenced_entity
          ->uuid())
          ->fromPool($info
          ->getPool()->id)
          ->asDependency(PullIntent::PULL_AS_DEPENDENCY === $sync[self::INDEX_PULL_REASON])
          ->manually(PullIntent::PULL_MANUALLY === $sync[self::INDEX_PULL_REASON])
          ->execute();
      } catch (SyncCoreException $e) {
        \Drupal::logger('cms_content_sync')
          ->warning('Failed to pull %type.%bundle %entity_id through the missing dependency manager: @message<br>Flow: @flow_id | Pool: @pool_id', [
          '%type' => $referenced_entity
            ->getEntityTypeId(),
          '%bundle' => $referenced_entity
            ->bundle(),
          '%entity_id' => $referenced_entity
            ->uuid(),
          '@message' => $e
            ->getMessage(),
          '@flow_id' => $flow->id,
          '@pool_id' => $info
            ->getPool()->id,
        ]);
      }
    }
  }
}