You are here

protected function DomainCommands::reassignLinkedEntities in Domain Access 8

Reassign entities of the supplied type to the $policy domain.

Parameters

array $options: Drush options sent to the command. An array such as the following: [ 'entity_filter' => 'node', 'policy' => 'prompt' | 'default' | 'ignore' | {domain_id} 'field' => DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD, ]; The caller is expected to provide this information.

array $domains: Array of domain objects to reassign content away from.

Return value

int The count of updated entities.

Throws

\Drupal\domain\Commands\DomainCommandException

1 call to DomainCommands::reassignLinkedEntities()
DomainCommands::doReassign in domain/src/Commands/DomainCommands.php
Handles reassignment of entities to another domain.

File

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

Class

DomainCommands
Drush commands for the domain module.

Namespace

Drupal\domain\Commands

Code

protected function reassignLinkedEntities(array $domains, array $options) {
  $count = 0;
  $field = $options['field'];
  $entity_typenames = $this
    ->findDomainEnabledEntities($field);
  $new_domain = $this
    ->getDomainInstanceFromPolicy($options['policy']);
  if (empty($new_domain)) {
    throw new DomainCommandException('invalid destination domain');
  }

  // Loop through each entity type.
  $exceptions = FALSE;
  foreach ($entity_typenames as $name) {
    if (empty($options['entity_filter']) || $options['entity_filter'] === $name) {

      // For each domain being reassigned from...
      foreach ($domains as $domain) {
        $ids = $this
          ->enumerateDomainEntities($name, $domain
          ->id(), $field);
        if (!empty($ids)) {
          try {
            if ($options['chatty']) {
              $this
                ->logger()
                ->info('Reassigning @count @entity_name entities to @domain', [
                '@entity_name' => '',
                '@count' => count($ids),
                '@domain' => $new_domain
                  ->id(),
              ]);
            }
            $count = $this
              ->reassignEntities($name, $field, $domain, $new_domain, $ids);
          } catch (PluginException $e) {
            $exceptions = TRUE;
            $this
              ->logger()
              ->error('Unable to reassign content to @new_domain: plugin exception: @ex', [
              '@ex' => $e
                ->getMessage(),
              '@new_domain' => $new_domain
                ->id(),
            ]);
          } catch (EntityStorageException $e) {
            $exceptions = TRUE;
            $this
              ->logger()
              ->error('Unable to reassign content to @new_domain: storage exception: @ex', [
              '@ex' => $e
                ->getMessage(),
              '@new_domain' => $new_domain
                ->id(),
            ]);
          }
        }
      }
    }
  }
  if ($exceptions) {
    throw new DomainCommandException('Errors encountered during reassign.');
  }
  return $count;
}