You are here

public static function EntityStatus::saveSelectedPoolsToPushTo in CMS Content Sync 8

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

Parameters

\Drupal\Core\Entity\EntityInterface $reference:

string $flow_id:

string[] $pool_ids:

null|EntityInterface $parent_entity:

null|string $parent_field_name:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

2 calls to EntityStatus::saveSelectedPoolsToPushTo()
EntityStatus::saveSelectedPushToPoolForField in src/Entity/EntityStatus.php
_cms_content_sync_set_entity_push_pools in ./cms_content_sync.module
Entity status update.

File

src/Entity/EntityStatus.php, line 389

Class

EntityStatus
Defines the "Content Sync - Entity Status" entity type.

Namespace

Drupal\cms_content_sync\Entity

Code

public static function saveSelectedPoolsToPushTo($reference, $flow_id, $pool_ids, $parent_entity = null, $parent_field_name = null) {
  $entity_type = $reference
    ->getEntityTypeId();
  $bundle = $reference
    ->bundle();
  $uuid = $reference
    ->uuid();
  $flow = Flow::getAll()[$flow_id];
  $pools = Pool::getAll();
  $entity_type_pools = Pool::getSelectablePools($entity_type, $bundle, $parent_entity, $parent_field_name)[$flow_id]['pools'];
  foreach ($entity_type_pools as $entity_type_pool_id => $config) {
    $pool = $pools[$entity_type_pool_id];
    $entity_status = EntityStatus::getInfoForEntity($entity_type, $uuid, $flow, $pool);
    if (in_array($entity_type_pool_id, $pool_ids)) {
      if (!$entity_status) {
        $entity_status = EntityStatus::create([
          'flow' => $flow->id,
          'pool' => $pool->id,
          'entity_type' => $entity_type,
          'entity_uuid' => $uuid,
          'entity_type_version' => Flow::getEntityTypeVersion($entity_type, $bundle),
          'flags' => 0,
          'source_url' => null,
        ]);
      }
      $entity_status
        ->isPushEnabled(true);
      $entity_status
        ->save();
      continue;
    }
    if ($entity_status) {
      $entity_status
        ->isPushEnabled(false);
      $entity_status
        ->save();
    }
  }

  // Also check if the entity is going to be force pushed into another pool.
  $force_push_pools = $flow
    ->getPoolsToPushTo($reference, PushIntent::PUSH_FORCED, SyncIntent::ACTION_CREATE);
  if (count($entity_type_pools) && !count($pool_ids) && !count($force_push_pools)) {
    \Drupal::messenger()
      ->addWarning(\Drupal::translation()
      ->translate("You didn't assign a pool to @entity_type %entity_label so it won't be pushed along with the content.", [
      '@entity_type' => $entity_type,
      '%entity_label' => $reference
        ->label(),
    ]));
  }
  elseif (count($entity_type_pools) && !count($pool_ids) && count($force_push_pools)) {
    $pools = '';
    $numItems = count($force_push_pools);
    $i = 0;
    if (count($force_push_pools) > 1) {
      foreach ($force_push_pools as $force_push_pool) {
        if (++$i === $numItems) {
          $pools .= $force_push_pool
            ->label();
        }
        else {
          $pools .= $force_push_pool
            ->label() . ', ';
        }
      }
    }
    else {
      foreach ($force_push_pools as $force_push_pool) {
        $pools = $force_push_pool
          ->label();
      }
    }
    \Drupal::messenger()
      ->addWarning(\Drupal::translation()
      ->translate("You didn't assign a pool to @entity_type %entity_label, but it is going to be force pushed to the following pools based on the content sync configuration: %pools.", [
      '%pools' => $pools,
      '@entity_type' => $entity_type,
      '%entity_label' => $reference
        ->label(),
    ]));
  }
}