You are here

class BakerySettingsForm in Bakery Single Sign-On System 8.2

Configure bakery settings for this site.

Hierarchy

Expanded class hierarchy of BakerySettingsForm

1 string reference to 'BakerySettingsForm'
bakery.routing.yml in ./bakery.routing.yml
bakery.routing.yml

File

src/Forms/BakerySettingsForm.php, line 11

Namespace

Drupal\bakery\Forms
View source
class BakerySettingsForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('bakery.settings');
    $form['bakery_is_master'] = [
      '#type' => 'checkbox',
      '#title' => 'Is this the master site?',
      '#default_value' => $config
        ->get('bakery_is_master'),
      '#description' => $this
        ->t('On the master site, accounts need to be created by traditional processes, i.e by a user registering or an admin creating them.'),
    ];
    $form['subsite_login'] = [
      '#type' => 'radios',
      '#title' => 'Subsite log in & registration',
      '#options' => [
        0 => $this
          ->t('Only log in & register on master site'),
        1 => $this
          ->t('Allow log in & register on any site (deprecated)'),
      ],
      '#default_value' => (int) $config
        ->get('subsite_login'),
      '#description' => $this
        ->t('Limiting log ins and registration to the master site gives users a consistent experience and reduces the surface area available to attackers.'),
    ];
    $form['bakery_master'] = [
      '#type' => 'textfield',
      '#title' => 'Master site',
      '#default_value' => $config
        ->get('bakery_master'),
      '#description' => $this
        ->t('Specify the master site for your bakery network.'),
    ];
    $form['bakery_slaves'] = [
      '#type' => 'textarea',
      '#title' => 'Slave sites',
      '#default_value' => implode("\n", $config
        ->get('bakery_slaves') ?: []),
      '#description' => $this
        ->t('Specify any slave sites in your bakery network that you want to update if a user changes email or username on the master. Enter one site per line, in the form "http://sub.example.com/".'),
    ];
    $form['bakery_help_text'] = [
      '#type' => 'textarea',
      '#title' => 'Help text for users with synch problems.',
      '#default_value' => $config
        ->get('bakery_help_text'),
      '#description' => $this
        ->t('This message will be shown to users if/when they have problems synching their accounts. It is an alternative to the "self repair" option and can be blank.'),
    ];
    $form['bakery_freshness'] = [
      '#type' => 'textfield',
      '#title' => 'Seconds of age before a cookie is old',
      '#default_value' => $config
        ->get('bakery_freshness'),
    ];
    $form['bakery_key'] = [
      '#type' => 'textfield',
      '#title' => 'Private key for cookie validation',
      '#default_value' => $config
        ->get('bakery_key'),
    ];
    $form['bakery_domain'] = [
      '#type' => 'textfield',
      '#title' => 'Cookie domain',
      '#default_value' => $config
        ->get('bakery_domain'),
    ];
    $default = $config
      ->get('bakery_supported_fields');
    $default['mail'] = 'mail';
    $default['name'] = 'name';
    $options = [
      'name' => $this
        ->t('username'),
      'mail' => $this
        ->t('e-mail'),
      'status' => $this
        ->t('status'),
      'picture' => $this
        ->t('user picture'),
      'language' => $this
        ->t('language'),
      'signature' => $this
        ->t('signature'),
    ];

    // TODO: need to add profile fileds

    /*
        if (module_exists('profile')) {
        $result = db_query('SELECT name, title FROM {profile_field}
        ORDER BY category, weight');
        foreach ($result as $field) {
        $options[$field->name] = check_plain($field->title);
        }
        }
    */
    $form['bakery_supported_fields'] = [
      '#type' => 'checkboxes',
      '#title' => 'Supported profile fields',
      '#default_value' => $default,
      '#options' => $options,
      '#description' => $this
        ->t('Choose the profile fields that should be exported by the master and imported on the slaves. Username and E-mail are always exported. The correct export of individual fields may depend on the appropriate settings for other modules on both master and slaves. You need to configure this setting on both the master and the slaves.'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('bakery.settings');
    $config
      ->set('bakery_is_master', $form_state
      ->getValue('bakery_is_master'))
      ->set('bakery_master', trim($form_state
      ->getValue('bakery_master'), '/') . '/')
      ->set('subsite_login', (bool) $form_state
      ->getValue('subsite_login'))
      ->set('bakery_help_text', $form_state
      ->getValue('bakery_help_text'))
      ->set('bakery_freshness', $form_state
      ->getValue('bakery_freshness'))
      ->set('bakery_key', $form_state
      ->getValue('bakery_key'))
      ->set('bakery_domain', $form_state
      ->getValue('bakery_domain'))
      ->set('bakery_supported_fields', $form_state
      ->getValue('bakery_supported_fields'));
    if ($form_state
      ->getValue('bakery_slaves') && !empty($form_state
      ->getValue('bakery_slaves'))) {

      // Transform the text string into an array.
      $slaves = explode("\n", trim(str_replace("\r", '', $form_state
        ->getValue('bakery_slaves'))));

      // For each entry, remove the trailing slash
      // (if present) and concatenate with a new trailing slash.
      foreach ($slaves as &$slave) {
        $slave = trim($slave, '/') . '/';
      }
      $config
        ->set('bakery_slaves', $slaves);
    }
    else {
      $config
        ->set('bakery_slaves', []);
    }
    $config
      ->save();

    // Trigger some rebuilds that might be required by main/child changes.
    \Drupal::service('router.builder')
      ->setRebuildNeeded();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BakerySettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
BakerySettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
BakerySettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
BakerySettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
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
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.