/** * Creates an alias for testing. * * @param Drupal\domain\Entity\Domain $domain * A domain entity. * @param string $pattern * An optional alias pattern. * @param int $redirect * An optional redirect (301 or 302). * * @return Drupal\domain_alias\Entity\DomainAlias * A domain alias entity. */ public function domainAliasCreateTestAlias(DomainInterface $domain, $pattern = NULL, $redirect = 0) { if (empty($pattern)) { $pattern = '*.' . $domain->getHostname(); } $values = array('domain_id' => $domain->id(), 'pattern' => $pattern, 'redirect' => $redirect); // Replicate the logic for creating machine_name patterns. $values['id'] = str_replace(array('*', '.'), '_', $values['pattern']); $alias = \Drupal::entityManager()->getStorage('domain_alias')->create($values); $alias->save(); return $alias; }
/** * @inheritdoc * * @TODO: Divide this into separate methods. Do not return Drupal-specfic * responses. */ public function validate(DomainInterface $domain) { $hostname = $domain->getHostname(); $error_list = array(); // Check for at least one dot or the use of 'localhost'. // Note that localhost can specify a port. $localhost_check = explode(':', $hostname); if (substr_count($hostname, '.') == 0 && $localhost_check[0] != 'localhost') { $error_list[] = $this->t('At least one dot (.) is required, except when using <em>localhost</em>.'); } // Check for one colon only. if (substr_count($hostname, ':') > 1) { $error_list[] = $this->t('Only one colon (:) is allowed.'); } elseif (substr_count($hostname, ':') == 1) { $parts = explode(':', $hostname); $port = (int) $parts[1]; if (strcmp($port, $parts[1])) { $error_list[] = $this->t('The port protocol must be an integer.'); } } // The domain cannot begin or end with a period. if (substr($hostname, 0, 1) == '.') { $error_list[] = $this->t('The domain must not begin with a dot (.)'); } // The domain cannot begin or end with a period. if (substr($hostname, -1) == '.') { $error_list[] = $this->t('The domain must not end with a dot (.)'); } // Check for valid characters, unless using non-ASCII domains. $non_ascii = \Drupal::config('domain.settings')->get('allow_non_ascii'); if (!$non_ascii) { $pattern = '/^[a-z0-9\\.\\-:]*$/i'; if (!preg_match($pattern, $hostname)) { $error_list[] = $this->t('Only alphanumeric characters, dashes, and a colon are allowed.'); } } // Check for lower case. if ($hostname != Unicode::strtolower($hostname)) { $error_list[] = $this->t('Only lower-case characters are allowed.'); } // Check for 'www' prefix if redirection / handling is enabled under global domain settings. // Note that www prefix handling must be set explicitly in the UI. // See http://drupal.org/node/1529316 and http://drupal.org/node/1783042 if (\Drupal::config('domain.settings')->get('www_prefix') && substr($hostname, 0, strpos($hostname, '.')) == 'www') { $error_list[] = $this->t('WWW prefix handling: Domains must be registered without the www. prefix.'); } // Check existing domains. $domains = \Drupal::entityManager()->getStorage('domain')->loadByProperties(array('hostname' => $hostname)); foreach ($domains as $domain) { if ($domain->id() != $domain->id()) { $error_list[] = $this->t('The hostname is already registered.'); } } // Allow modules to alter this behavior. \Drupal::moduleHandler()->invokeAll('domain_validate', $error_list, $hostname); // Return the errors, if any. if (!empty($error_list)) { return $this->t('The domain string is invalid for %subdomain: @errors', array('%subdomain' => $hostname, '@errors' => array('#theme' => 'item_list', '#items' => $error_list))); } return array(); }