You are here

class BootstrapLibrarySettingsForm in Bootstrap Library 8

Configure bootstrap_library settings for this site.

Hierarchy

Expanded class hierarchy of BootstrapLibrarySettingsForm

1 string reference to 'BootstrapLibrarySettingsForm'
bootstrap_library.routing.yml in ./bootstrap_library.routing.yml
bootstrap_library.routing.yml

File

src/BootstrapLibrarySettingsForm.php, line 17
Contains \Drupal\bootstrap_library\BootstrapLibrarySettingsForm

Namespace

Drupal\bootstrap_library
View source
class BootstrapLibrarySettingsForm extends ConfigFormBase {
  public function getFormId() {
    return 'bootstrap_library_admin_settings';
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('bootstrap_library.settings');
    $themes = \Drupal::service('theme_handler')
      ->listInfo();
    $active_themes = array();
    foreach ($themes as $key => $theme) {
      if ($theme->status) {
        $active_themes[$key] = $theme->info['name'];
      }
    }

    // Load from CDN
    $form['cdn'] = array(
      '#type' => 'fieldset',
      '#title' => t('Load Boostrap from CDN'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $data = _bootstrap_library_data();
    $cdn_options = json_decode($data);
    $versions = array_keys(_bootstrap_library_object_to_array($cdn_options->bootstrap));
    $options = array_combine($versions, $versions);
    array_unshift($options, 'Load locally');
    $form['cdn']['bootstrap'] = array(
      '#type' => 'select',
      '#title' => t('Select Bootstrap version to load via CDN, non for local library'),
      '#options' => $options,
      '#default_value' => $config
        ->get('cdn.bootstrap'),
    );
    $form['cdn']['cdn_options'] = array(
      '#type' => 'hidden',
      '#value' => $data,
    );

    // Production or minimized version
    $form['minimized'] = array(
      '#type' => 'fieldset',
      '#title' => t('Minimized, Non-minimized, or Composer version'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['minimized']['minimized_options'] = array(
      '#type' => 'radios',
      '#title' => t('Choose minimized, non-minimized, or composer version.'),
      '#options' => array(
        0 => t('Use non minimized libraries (Development)'),
        1 => t('Use minimized libraries (Production)'),
        2 => t('Use composer installed libraries'),
      ),
      '#default_value' => $config
        ->get('minimized.options'),
    );

    // Per-theme visibility.
    $form['theme'] = array(
      '#type' => 'fieldset',
      '#title' => t('Themes Visibility'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['theme']['theme_visibility'] = array(
      '#type' => 'radios',
      '#title' => t('Activate on specific themes'),
      '#options' => array(
        0 => t('All themes except those listed'),
        1 => t('Only the listed themes'),
      ),
      '#default_value' => $config
        ->get('theme.visibility'),
    );
    $form['theme']['theme_themes'] = array(
      '#type' => 'select',
      '#title' => 'List of themes where library will be loaded.',
      '#options' => $active_themes,
      '#multiple' => TRUE,
      '#default_value' => $config
        ->get('theme.themes'),
      '#description' => t("Specify in which themes you wish the library to load."),
    );

    // Per-path visibility.
    $form['url'] = array(
      '#type' => 'fieldset',
      '#title' => t('Activate on specific URLs'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['url']['url_visibility'] = array(
      '#type' => 'radios',
      '#title' => t('Load bootstrap on specific pages'),
      '#options' => array(
        0 => t('All pages except those listed'),
        1 => t('Only the listed pages'),
      ),
      '#default_value' => $config
        ->get('url.visibility'),
    );
    $form['url']['url_pages'] = array(
      '#type' => 'textarea',
      '#title' => '<span class="element-invisible">' . t('Pages') . '</span>',
      '#default_value' => _bootstrap_library_array_to_string($config
        ->get('url.pages')),
      '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
        '%blog' => 'blog',
        '%blog-wildcard' => 'blog/*',
        '%front' => '<front>',
      )),
    );

    // Files settings.
    $form['files'] = array(
      '#type' => 'fieldset',
      '#title' => t('Files Settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['files']['types'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Select which files to load from the library. By default you should check both, but in some cases you will need to load only CSS or JS Bootstrap files.'),
      '#options' => array(
        'css' => t('CSS files'),
        'js' => t('Javascript files'),
      ),
      '#default_value' => $config
        ->get('files.types'),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('bootstrap_library.settings')
      ->set('theme.visibility', $form_state
      ->getValue('theme_visibility'))
      ->set('theme.themes', $form_state
      ->getValue('theme_themes'))
      ->set('url.visibility', $form_state
      ->getValue('url_visibility'))
      ->set('url.pages', _bootstrap_library_string_to_array($form_state
      ->getValue('url_pages')))
      ->set('minimized.options', $form_state
      ->getValue('minimized_options'))
      ->set('cdn.bootstrap', $form_state
      ->getValue('bootstrap'))
      ->set('cdn.options', $form_state
      ->getValue('cdn_options'))
      ->set('files.types', $form_state
      ->getValue('types'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BootstrapLibrarySettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
BootstrapLibrarySettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
BootstrapLibrarySettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
BootstrapLibrarySettingsForm::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.