You are here

class DomainConfigSettingsForm in Domain Site Settings 8

Save domain config settings.

@package Drupal\domain_site_settings\Form

Hierarchy

Expanded class hierarchy of DomainConfigSettingsForm

1 string reference to 'DomainConfigSettingsForm'
domain_site_settings.routing.yml in ./domain_site_settings.routing.yml
domain_site_settings.routing.yml

File

src/Form/DomainConfigSettingsForm.php, line 19

Namespace

Drupal\domain_site_settings\Form
View source
class DomainConfigSettingsForm extends ConfigFormBase {

  /**
   * The path alias manager.
   *
   * @var \Drupal\Core\Path\AliasManagerInterface
   */
  protected $aliasManager;

  /**
   * The path validator.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * The request context.
   *
   * @var \Drupal\Core\Routing\RequestContext
   */
  protected $requestContext;

  /**
   * Constructs a SiteInformationForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\path_alias\AliasManagerInterface $alias_manager
   *   The path alias manager.
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The path validator.
   * @param \Drupal\Core\Routing\RequestContext $request_context
   *   The request context.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, PathValidatorInterface $path_validator, RequestContext $request_context) {
    parent::__construct($config_factory);
    $this->aliasManager = $alias_manager;
    $this->pathValidator = $path_validator;
    $this->requestContext = $request_context;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('path_alias.manager'), $container
      ->get('path.validator'), $container
      ->get('router.request_context'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'domain_site_settings.domainconfigsettings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'domain_config_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, DomainInterface $domain = NULL) {
    $config = $this
      ->config('domain_site_settings.domainconfigsettings');
    $domain_id = $domain
      ->id();
    $site_config = $this
      ->config('system.site');
    $site_mail = $site_config
      ->get('mail');
    if (empty($site_mail)) {
      $site_mail = ini_get('sendmail_from');
    }
    if ($config
      ->get($domain_id) != NULL) {
      $site_mail = $config
        ->get($domain_id . '.site_mail');
    }
    $form['site_information'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Site details'),
      '#open' => TRUE,
    ];
    $form['site_information']['site_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Site name'),
      '#default_value' => $config
        ->get($domain_id) != NULL ? $config
        ->get($domain_id . '.site_name') : $site_config
        ->get('name'),
      '#required' => TRUE,
    ];
    $form['site_information']['site_slogan'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Slogan'),
      '#default_value' => $config
        ->get($domain_id) != NULL ? $config
        ->get($domain_id . '.site_slogan') : $site_config
        ->get('slogan'),
      '#description' => $this
        ->t("How this is used depends on your site's theme."),
    ];
    $form['site_information']['site_mail'] = [
      '#type' => 'email',
      '#title' => $this
        ->t('Email address'),
      '#default_value' => $site_mail,
      '#description' => $this
        ->t("The <em>From</em> address in automated emails sent during registration and new password requests, and other notifications. (Use an address ending in your site's domain to help prevent this email being flagged as spam.)"),
      '#required' => TRUE,
    ];
    $form['front_page'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Front page'),
      '#open' => TRUE,
    ];
    $front_page = $site_config
      ->get('page.front') != '/user/login' ? $this->aliasManager
      ->getAliasByPath($site_config
      ->get('page.front')) : '';
    $front_page = $config
      ->get($domain_id) != NULL ? $config
      ->get($domain_id . '.site_frontpage') : $front_page;
    $form['front_page']['site_frontpage'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Default front page'),
      '#default_value' => $front_page,
      '#size' => 40,
      '#description' => $this
        ->t('Optionally, specify a relative URL to display as the front page. Leave blank to display the default front page.'),
      '#field_prefix' => $this->requestContext
        ->getCompleteBaseUrl(),
    ];
    $form['error_page'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Error pages'),
      '#open' => TRUE,
    ];
    $form['error_page']['site_403'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Default 403 (access denied) page'),
      '#default_value' => $config
        ->get($domain_id) !== NULL ? $config
        ->get($domain_id . '.site_403') : $site_config
        ->get('page.403'),
      '#size' => 40,
      '#description' => $this
        ->t('This page is displayed when the requested document is denied to the current user. Leave blank to display a generic "access denied" page.'),
    ];
    $form['error_page']['site_404'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Default 404 (not found) page'),
      '#default_value' => $config
        ->get($domain_id) !== NULL ? $config
        ->get($domain_id . '.site_404') : $site_config
        ->get('page.404'),
      '#size' => 40,
      '#description' => $this
        ->t('This page is displayed when no other content matches the requested document. Leave blank to display a generic "page not found" page.'),
    ];
    $form['domain_id'] = [
      '#type' => 'hidden',
      '#title' => $this
        ->t('Domain ID'),
      '#default_value' => $domain_id,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // Check for empty front page path.
    if ($form_state
      ->isValueEmpty('site_frontpage')) {

      // Set to default "user/login".
      $form_state
        ->setValueForElement($form['front_page']['site_frontpage'], '/user/login');
    }
    else {

      // Get the normal path of the front page.
      $form_state
        ->setValueForElement($form['front_page']['site_frontpage'], $this->aliasManager
        ->getPathByAlias($form_state
        ->getValue('site_frontpage')));
    }

    // Validate front page path.
    if (($value = $form_state
      ->getValue('site_frontpage')) && $value[0] !== '/') {
      $form_state
        ->setErrorByName('site_frontpage', $this
        ->t("The path '%path' has to start with a slash.", [
        '%path' => $form_state
          ->getValue('site_frontpage'),
      ]));
    }
    if (!$this->pathValidator
      ->isValid($form_state
      ->getValue('site_frontpage'))) {
      $form_state
        ->setErrorByName('site_frontpage', $this
        ->t("The path '%path' is either invalid or you do not have access to it.", [
        '%path' => $form_state
          ->getValue('site_frontpage'),
      ]));
    }

    // Get the normal paths of both error pages.
    if (!$form_state
      ->isValueEmpty('site_403')) {
      $form_state
        ->setValueForElement($form['error_page']['site_403'], $this->aliasManager
        ->getPathByAlias($form_state
        ->getValue('site_403')));
    }
    if (!$form_state
      ->isValueEmpty('site_404')) {
      $form_state
        ->setValueForElement($form['error_page']['site_404'], $this->aliasManager
        ->getPathByAlias($form_state
        ->getValue('site_404')));
    }
    if (($value = $form_state
      ->getValue('site_403')) && $value[0] !== '/') {
      $form_state
        ->setErrorByName('site_403', $this
        ->t("The path '%path' has to start with a slash.", [
        '%path' => $form_state
          ->getValue('site_403'),
      ]));
    }
    if (($value = $form_state
      ->getValue('site_404')) && $value[0] !== '/') {
      $form_state
        ->setErrorByName('site_404', $this
        ->t("The path '%path' has to start with a slash.", [
        '%path' => $form_state
          ->getValue('site_404'),
      ]));
    }

    // Validate 403 error path.
    if (!$form_state
      ->isValueEmpty('site_403') && !$this->pathValidator
      ->isValid($form_state
      ->getValue('site_403'))) {
      $form_state
        ->setErrorByName('site_403', $this
        ->t("The path '%path' is either invalid or you do not have access to it.", [
        '%path' => $form_state
          ->getValue('site_403'),
      ]));
    }

    // Validate 404 error path.
    if (!$form_state
      ->isValueEmpty('site_404') && !$this->pathValidator
      ->isValid($form_state
      ->getValue('site_404'))) {
      $form_state
        ->setErrorByName('site_404', $this
        ->t("The path '%path' is either invalid or you do not have access to it.", [
        '%path' => $form_state
          ->getValue('site_404'),
      ]));
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $domain_id = $form_state
      ->getValue('domain_id');
    $site_name = $form_state
      ->getValue('site_name');
    $site_slogan = $form_state
      ->getValue('site_slogan');
    $site_mail = $form_state
      ->getValue('site_mail');
    $site_frontpage = $form_state
      ->getValue('site_frontpage');
    $site_403 = $form_state
      ->getValue('site_403');
    $site_404 = $form_state
      ->getValue('site_404');
    $config = $this
      ->config('domain_site_settings.domainconfigsettings');
    $config
      ->set($domain_id . '.site_name', $site_name);
    $config
      ->set($domain_id . '.site_slogan', $site_slogan);
    $config
      ->set($domain_id . '.site_mail', $site_mail);
    $config
      ->set($domain_id . '.site_frontpage', $site_frontpage);
    $config
      ->set($domain_id . '.site_403', $site_403);
    $config
      ->set($domain_id . '.site_404', $site_404);
    $config
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DomainConfigSettingsForm::$aliasManager protected property The path alias manager.
DomainConfigSettingsForm::$pathValidator protected property The path validator.
DomainConfigSettingsForm::$requestContext protected property The request context.
DomainConfigSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
DomainConfigSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
DomainConfigSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
DomainConfigSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
DomainConfigSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
DomainConfigSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
DomainConfigSettingsForm::__construct public function Constructs a SiteInformationForm object. Overrides ConfigFormBase::__construct
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.