You are here

public function DomainCommands::delete in Domain Access 8

Delete a domain from the site.

Deletes the domain from the Drupal configuration and optionally reassign content and/or profiles associated with the deleted domain to another. The domain marked as default cannot be deleted: to achieve this goal, mark another, possibly newly created, domain as the default domain, then delete the old default.

The usage example descriptions are based on starting with three domains:

  • id:19476, machine: example_com, domain: example.com
  • id:29389, machine: example_org, domain: example.org (default)
  • id:91736, machine: example_net, domain: example.net

@usage drush domain:delete example.com Delete the domain example.com, assigning its content and users to the default domain, example.org.

@usage drush domain:delete --content-assign=ignore example.com Delete the domain example.com, leaving its content untouched but assigning its users to the default domain.

@usage drush domain:delete --content-assign=example_net --users-assign=example_net example.com Delete the domain example.com, assigning its content and users to the example.net domain.

@usage drush domain:delete --dryrun 19476 Show the effects of delete the domain example.com and assigning its content and users to the default domain, example.org, but not doing so.

@usage drush domain:delete --chatty example_net Verbosely Delete the domain example.net and assign its content and users to the default domain, example.org.

@usage drush domain-delete --chatty all Verbosely Delete the domains example.com and example.net and assign their content and users to the default domain, example.org.

@option chatty Document each step as it is performed. @option dryrun Do not do anything, but explain what would be done. Implies --chatty. @option users-assign Values "prompt", "ignore", "default", <name>, Reassign user accounts associated with the the domain being deleted to the default domain, to the domain whose machine name is <name>, or leave the user accounts alone (and so inaccessible in the normal way). The default value is 'prompt': ask which domain to use.

@command domain:delete @aliases domain-delete

Parameters

string $domain_id: The numeric id, machine name, or hostname of the domain to delete. The value "all" is taken to mean delete all except the default domain.

array $options: An associative array of options whose values come from cli, aliases, config, etc.

Throws

\Drupal\domain\Commands\DomainCommandException

See also

https://github.com/consolidation/annotated-command#option-event-hook

File

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

Class

DomainCommands
Drush commands for the domain module.

Namespace

Drupal\domain\Commands

Code

public function delete($domain_id, array $options = [
  'users-assign' => NULL,
  'dryrun' => NULL,
  'chatty' => NULL,
]) {
  if (is_null($options['users-assign'])) {
    $policy_users = 'prompt';
  }
  $this->isDryRun = (bool) $options['dryrun'];

  // Get current domain list and perform validation checks.
  $default_domain = $this
    ->domainStorage()
    ->loadDefaultDomain();
  $all_domains = $this
    ->domainStorage()
    ->loadMultipleSorted(NULL);
  if (empty($all_domains)) {
    throw new DomainCommandException('There are no configured domains.');
  }
  if (empty($domain_id)) {
    throw new DomainCommandException('You must specify a domain to delete.');
  }

  // Determine which domains to be deleted.
  if ($domain_id === 'all') {
    $domains = $all_domains;
    if (empty($domains)) {
      $this
        ->logger()
        ->info(dt('There are no domains to delete.'));
      return;
    }
    $really = $this
      ->io()
      ->confirm(dt('This action cannot be undone. Continue?:'), FALSE);
    if (empty($really)) {
      return;
    }

    // TODO: handle deletion of all domains.
    $policy_users = "ignore";
    $message = dt('All domain records have been deleted.');
  }
  elseif ($domain = $this
    ->getDomainFromArgument($domain_id)) {
    if ($domain
      ->isDefault()) {
      throw new DomainCommandException('The primary domain may not be deleted.
          Use drush domain:default to set a new default domain.');
    }
    $domains = [
      $domain,
    ];
    $message = dt('Domain record !domain deleted.', [
      '!domain' => $domain
        ->id(),
    ]);
  }
  if (!empty($options['users-assign'])) {
    if (in_array($options['users-assign'], $this->reassignmentPolicies, TRUE)) {
      $policy_users = $options['users-assign'];
    }
  }
  $delete_options = [
    'entity_filter' => 'user',
    'policy' => $policy_users,
    'field' => DomainInterface::DOMAIN_ADMIN_FIELD,
  ];
  if ($policy_users !== 'ignore') {
    $messages[] = $this
      ->doReassign($domain, $delete_options);
  }

  // Fire any registered hooks for deletion, passing them current imput.
  $handlers = $this
    ->getCustomEventHandlers('domain-delete');
  $messages = [];
  foreach ($handlers as $handler) {
    $messages[] = $handler($domain, $options);
  }
  $this
    ->deleteDomain($domains, $options);
  if ($messages) {
    $message .= "\n" . implode("\n", $messages);
  }
  $this
    ->logger()
    ->info($message);
  return $message;
}