You are here

class SettingsForm in Layout Builder Base 8

Class SettingsForm.

Hierarchy

Expanded class hierarchy of SettingsForm

1 file declares its use of SettingsForm
ConfigManagerService.php in src/ConfigManagerService.php
2 string references to 'SettingsForm'
layout_builder_base.routing.yml in ./layout_builder_base.routing.yml
layout_builder_base.routing.yml
layout_builder_base.routing.yml in ./layout_builder_base.routing.yml
layout_builder_base.routing.yml

File

src/Form/SettingsForm.php, line 11

Namespace

Drupal\layout_builder_base\Form
View source
class SettingsForm extends ConfigFormBase {

  /**
   * Config settings.
   *
   * @var string
   */
  const SETTINGS = 'layout_builder_base.settings';

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      static::SETTINGS,
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config(static::SETTINGS);
    $form['lbb_settings'] = [
      '#type' => 'vertical_tabs',
      '#title' => $this
        ->t('Layout Builder Base Settings'),
    ];
    $form['default_values'] = [
      '#type' => 'details',
      '#title' => 'Default values',
      '#group' => 'lbb_settings',
      '#tree' => true,
    ];
    $form['override_options'] = [
      '#type' => 'details',
      '#title' => 'Override options',
      '#group' => 'lbb_settings',
      '#tree' => true,
    ];
    $form['default_values']['help'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('Configuring the default settings here will apply to all the layouts. If you need custom logic per layout, you will have to create your own. Since the layouts can have any value for their options, you need to enter yourself the value of the selects.'),
    ];
    $form['default_values'] = array_merge($form['default_values'], $this
      ->getDetails());
    $form['override_options']['help'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('Those fields allow you to override the options that the layouts are proposing. The options should be written with the following format: class|Label with one option per line. You can also use the value <none> in case you want to completely remove this option from the interface. If you want to keep some options from the module, just add them to the list in addition of yours. Example: layout--background--white|White'),
    ];
    $form['override_options'] = array_merge($form['override_options'], $this
      ->getDetails());
    $properties = $this
      ->getProperties();
    foreach ($properties as $property => $infos) {
      $form['default_values'][$infos['group']][$property] = [
        '#type' => 'textfield',
        '#title' => $infos['label'],
        '#size' => 64,
        '#default_value' => $config
          ->get($property),
      ];
    }
    foreach ($properties as $property => $infos) {
      $form['override_options'][$infos['group']][$property] = [
        '#type' => 'textarea',
        '#title' => $infos['label'],
        '#size' => 64,
        '#default_value' => $config
          ->get(self::getOverrideConfigName($property)),
      ];
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * Function to get the name of the override config for a property.
   *
   * @param string $property
   *   The property key.
   *
   * @return string
   *   The override config name for a property.
   */
  public static function getOverrideConfigName($property) {
    return 'override_' . $property;
  }

  /**
   * Function to get the details used for the form.
   *
   * @return array
   *   The different details.
   */
  public function getDetails() {
    $details = [];
    $details['grid'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Grid properties'),
    ];
    $details['background_detail'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Background'),
    ];
    $details['margin'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Margins'),
    ];
    $details['padding'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Paddings'),
    ];
    $details['container'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Container'),
    ];
    $details['styles'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Styles'),
    ];
    return $details;
  }

  /**
   * Function to get the properties list.
   *
   * @return array
   *   The list of the properties.
   */
  public function getProperties() {
    return [
      'column_gap' => [
        'label' => $this
          ->t('Column gap'),
        'group' => 'grid',
      ],
      'row_gap' => [
        'label' => $this
          ->t('Row gap'),
        'group' => 'grid',
      ],
      'column_breakpoint' => [
        'label' => $this
          ->t('Column breakpoint'),
        'group' => 'grid',
      ],
      'column_width' => [
        'label' => $this
          ->t('Columns width'),
        'group' => 'grid',
      ],
      'two_column_width' => [
        'label' => $this
          ->t('Two Columns width'),
        'group' => 'grid',
      ],
      'three_column_width' => [
        'label' => $this
          ->t('Three Columns width'),
        'group' => 'grid',
      ],
      'align_items' => [
        'label' => $this
          ->t('Align items'),
        'group' => 'grid',
      ],
      'customizable_columns' => [
        'label' => $this
          ->t('Customizable columns'),
        'group' => 'grid',
      ],
      'background' => [
        'label' => $this
          ->t('Background'),
        'group' => 'background_detail',
      ],
      'background_position' => [
        'label' => $this
          ->t('Background Position'),
        'group' => 'background_detail',
      ],
      'background_attachment' => [
        'label' => $this
          ->t('Background Attachment'),
        'group' => 'background_detail',
      ],
      'background_size' => [
        'label' => $this
          ->t('Background Size'),
        'group' => 'background_detail',
      ],
      'background_overlay' => [
        'label' => $this
          ->t('Background Overlay'),
        'group' => 'background_detail',
      ],
      'top_margin' => [
        'label' => $this
          ->t('Top Margin'),
        'group' => 'margin',
      ],
      'bottom_margin' => [
        'label' => $this
          ->t('Bottom Margin'),
        'group' => 'margin',
      ],
      'left_margin' => [
        'label' => $this
          ->t('Left Margin'),
        'group' => 'margin',
      ],
      'right_margin' => [
        'label' => $this
          ->t('Right Margin'),
        'group' => 'margin',
      ],
      'equal_top_bottom_margins' => [
        'label' => $this
          ->t('Top Bottom Margins'),
        'group' => 'margin',
      ],
      'equal_left_right_margins' => [
        'label' => $this
          ->t('Left Right Margins'),
        'group' => 'margin',
      ],
      'top_padding' => [
        'label' => $this
          ->t('Top Padding'),
        'group' => 'padding',
      ],
      'bottom_padding' => [
        'label' => $this
          ->t('Bottom Padding'),
        'group' => 'padding',
      ],
      'left_padding' => [
        'label' => $this
          ->t('Left Padding'),
        'group' => 'padding',
      ],
      'right_padding' => [
        'label' => $this
          ->t('Right Padding'),
        'group' => 'padding',
      ],
      'equal_top_bottom_paddings' => [
        'label' => $this
          ->t('Top Bottom Paddings'),
        'group' => 'padding',
      ],
      'equal_left_right_paddings' => [
        'label' => $this
          ->t('Left Right Paddings'),
        'group' => 'padding',
      ],
      'height' => [
        'label' => $this
          ->t('Height'),
        'group' => 'container',
      ],
      'container' => [
        'label' => $this
          ->t('Container'),
        'group' => 'container',
      ],
      'content_container' => [
        'label' => $this
          ->t('Content Container'),
        'group' => 'container',
      ],
      'color' => [
        'label' => $this
          ->t('Default color'),
        'group' => 'styles',
      ],
      'alignment' => [
        'label' => $this
          ->t('Default alignment'),
        'group' => 'styles',
      ],
      'modifiers' => [
        'label' => $this
          ->t('Modifiers'),
        'group' => 'styles',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $newconfig = $this->configFactory
      ->getEditable(static::SETTINGS);
    $values = $form_state
      ->getValues();
    $tabs = [
      'default_values',
      'override_options',
    ];
    $properties = $this
      ->getProperties();
    foreach ($tabs as $tab) {
      foreach ($properties as $property => $infos) {
        $value = $values[$tab][$infos['group']][$property];
        $config_name = $tab === 'default_values' ? $property : self::getOverrideConfigName($property);
        $newconfig
          ->set($config_name, $value);
      }
    }
    $newconfig
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::getDetails public function Function to get the details used for the form.
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::getOverrideConfigName public static function Function to get the name of the override config for a property.
SettingsForm::getProperties public function Function to get the properties list.
SettingsForm::SETTINGS constant Config settings.
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
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.