You are here

class SettingsForm in Bootstrap Styles 1.0.x

Configure Bootstrap Styles settings.

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
bootstrap_styles.routing.yml in ./bootstrap_styles.routing.yml
bootstrap_styles.routing.yml

File

src/Form/SettingsForm.php, line 14

Namespace

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

  /**
   * The styles group plugin manager.
   *
   * @var \Drupal\bootstrap_styles\StylesGroup\StylesGroupManager
   */
  protected $stylesGroupManager;

  /**
   * The styles plugin manager.
   *
   * @var \Drupal\bootstrap_styles\Style\StyleManager
   */
  protected $styleManager;

  /**
   * Constructs a SettingsForm object.
   *
   * @param \Drupal\bootstrap_styles\StylesGroup\StylesGroupManager $styles_group_manager
   *   The styles group plugin manager.
   * @param \Drupal\bootstrap_styles\Style\StyleManager $style_manager
   *   The styles plugin manager.
   */
  public function __construct(StylesGroupManager $styles_group_manager, StyleManager $style_manager) {
    $this->stylesGroupManager = $styles_group_manager;
    $this->styleManager = $style_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.bootstrap_styles_group'), $container
      ->get('plugin.manager.bootstrap_styles'));
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $this
      ->messenger()
      ->addWarning('Important: the following classes are only used as an example to explain how to use this module. You should use your own theme classes.');

    // Layout builder theme toggler.
    $options = [
      'dark' => $this
        ->t('Dark'),
      'light' => $this
        ->t('Light'),
    ];
    $form['layout_builder_theme'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Layout Builder Theme'),
      '#options' => $options,
      '#default_value' => $this
        ->config(static::SETTINGS)
        ->get('layout_builder_theme') ?? 'dark',
    ];

    // Loop through styles groups plugins.
    foreach ($this->stylesGroupManager
      ->getStylesGroups() as $group_plugin_id => $styles_group) {

      // Style group form.
      $group_instance = $this->stylesGroupManager
        ->createInstance($group_plugin_id);
      $form = $group_instance
        ->buildConfigurationForm($form, $form_state);
      if (isset($styles_group['styles'])) {

        // Loop through styles plugins.
        foreach (array_keys($styles_group['styles']) as $style_plugin_id) {

          // Style plugin form.
          $style_instance = $this->styleManager
            ->createInstance($style_plugin_id);
          $form = $style_instance
            ->buildConfigurationForm($form, $form_state);
        }
      }
    }

    // Attach admin form style.
    $form['#attached']['library'][] = 'bootstrap_styles/layout_builder_admin_form_style';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);

    // Save layout builder theme.
    $this
      ->config(static::SETTINGS)
      ->set('layout_builder_theme', $form_state
      ->getValue('layout_builder_theme'))
      ->save();
    foreach ($this->stylesGroupManager
      ->getStylesGroups() as $group_plugin_id => $style_group) {

      // Submit group form.
      $group_instance = $this->stylesGroupManager
        ->createInstance($group_plugin_id);
      $group_instance
        ->submitConfigurationForm($form, $form_state);
      if (isset($style_group['styles'])) {
        foreach ($style_group['styles'] as $style_plugin_id => $style) {

          // Submit style form.
          $style_instance = $this->styleManager
            ->createInstance($style_plugin_id);
          $style_instance
            ->submitConfigurationForm($form, $form_state);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
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. 3
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.
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 72
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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::$styleManager protected property The styles plugin manager.
SettingsForm::$stylesGroupManager protected property The styles group plugin manager.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
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::SETTINGS constant Config settings.
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::__construct public function Constructs a SettingsForm object. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.