public function DomainCommands::add in Domain Access 8
Add a new domain to the site.
@option inactive Set the domain to inactive status if set. @option scheme Use indicated protocol for this domain, defaults to 'https'. Options:
- http: normal http (no SSL).
- https: secure https (with SSL).
- variable: match the scheme used by the request.
@option weight Set the order (weight) of the domain. @option is_default Set this domain as the default domain. @option validate Force a check of the URL response before allowing registration.
@usage drush domain-add example.com 'My Test Site' @usage drush domain-add example.com 'My Test Site' --scheme=https --inactive @usage drush domain-add example.com 'My Test Site' --weight=10 @usage drush domain-add example.com 'My Test Site' --validate
@command domain:add @aliases domain-add
Parameters
string $hostname: The domain hostname to register (e.g. example.com).
string $name: The name of the site (e.g. Domain Two).
array $options: An associative array of optional values.
Return value
string The entity id of the created domain.
Throws
\Drupal\domain\Commands\DomainCommandException
File
- domain/
src/ Commands/ DomainCommands.php, line 298
Class
- DomainCommands
- Drush commands for the domain module.
Namespace
Drupal\domain\CommandsCode
public function add($hostname, $name, array $options = [
'weight' => NULL,
'scheme' => NULL,
]) {
// Validate the weight arg.
if (!empty($options['weight']) && !is_numeric($options['weight'])) {
throw new DomainCommandException(dt('Domain weight "!weight" must be a number', [
'!weight' => !empty($options['weight']) ? $options['weight'] : '',
]));
}
// Validate the scheme arg.
if (!empty($options['scheme']) && ($options['scheme'] !== 'http' && $options['scheme'] !== 'https' && $options['scheme'] !== 'variable')) {
throw new DomainCommandException(dt('Scheme name "!scheme" not known', [
'!scheme' => !empty($options['scheme']) ? $options['scheme'] : '',
]));
}
$domains = $this
->domainStorage()
->loadMultipleSorted();
$start_weight = count($domains) + 1;
$values = [
'hostname' => $hostname,
'name' => $name,
'status' => empty($options['inactive']),
'scheme' => empty($options['scheme']) ? 'http' : $options['scheme'],
'weight' => empty($options['weight']) ? $start_weight : $options['weight'],
'is_default' => !empty($options['is_default']),
'id' => $this
->domainStorage()
->createMachineName($hostname),
];
/** @var \Drupal\domain\DomainInterface */
$domain = $this
->domainStorage()
->create($values);
// Check for hostname validity. This is required.
$valid = $this
->validateDomain($domain);
if (!empty($valid)) {
throw new DomainCommandException(dt('Hostname is not valid. !errors', [
'!errors' => implode(" ", $valid),
]));
}
// Check for hostname and id uniqueness.
foreach ($domains as $existing) {
if ($hostname == $existing
->getHostname()) {
throw new DomainCommandException(dt('No domain created. Hostname is a duplicate of !hostname.', [
'!hostname' => $hostname,
]));
}
if ($values['id'] == $existing
->id()) {
throw new DomainCommandException(dt('No domain created. Id is a duplicate of !id.', [
'!id' => $existing
->id(),
]));
}
}
$validate_response = (bool) $options['validate'];
if ($this
->createDomain($domain, $validate_response)) {
return dt('Created the !hostname with machine id !id.', [
'!hostname' => $values['hostname'],
'!id' => $values['id'],
]);
}
else {
return dt('No domain created.');
}
}