You are here

protected function DomainCommands::reassignEntities in Domain Access 8

Reassign old_domain entities, of the supplied type, to the new_domain.

Parameters

string $entity_type: The entity type name, e.g. 'node'.

string $field: The field to manipulate in the entity, e.g. DomainInterface::DOMAIN_ADMIN_FIELD.

\Drupal\domain\DomainInterface $old_domain: The domain the entities currently belong to. It is not an error for entity ids to be passed in that are not in this domain, though of course not very useful.

\Drupal\domain\DomainInterface $new_domain: The domain the entities should now belong to: When an entity belongs to the old_domain, this domain replaces it.

array $ids: List of entity IDs for the selected domain and all of type $entity_type.

Return value

int A count of the number of entities changed.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Core\Entity\EntityStorageException

1 call to DomainCommands::reassignEntities()
DomainCommands::reassignLinkedEntities in domain/src/Commands/DomainCommands.php
Reassign entities of the supplied type to the $policy domain.

File

domain/src/Commands/DomainCommands.php, line 1292

Class

DomainCommands
Drush commands for the domain module.

Namespace

Drupal\domain\Commands

Code

protected function reassignEntities($entity_type, $field, DomainInterface $old_domain, DomainInterface $new_domain, array $ids) {
  $entity_storage = \Drupal::entityTypeManager()
    ->getStorage($entity_type);
  $entities = $entity_storage
    ->loadMultiple($ids);
  foreach ($entities as $entity) {
    $changed = FALSE;
    if (!$entity
      ->hasField($field)) {
      continue;
    }

    // Multivalue fields are used, so check each one.
    foreach ($entity
      ->get($field) as $k => $item) {
      if ($item->target_id == $old_domain
        ->id()) {
        if ($this->isDryRun) {
          $this
            ->logger()
            ->info(dt('DRYRUN: Update domain membership for entity @id to @new.', [
            '@id' => $entity
              ->id(),
            '@new' => $new_domain
              ->id(),
          ]));

          // Don't set changed, so don't save either.
          continue;
        }
        $changed = TRUE;
        $item->target_id = $new_domain
          ->id();
      }
    }
    if ($changed) {
      $entity
        ->save();
    }
  }
  return count($entities);
}