You are here

class ToolbarThemesSettingsForm in Toolbar Themes 8

Toobar themes settings form.

Hierarchy

Expanded class hierarchy of ToolbarThemesSettingsForm

1 string reference to 'ToolbarThemesSettingsForm'
toolbar_themes.routing.yml in ./toolbar_themes.routing.yml
toolbar_themes.routing.yml

File

src/Form/ToolbarThemesSettingsForm.php, line 11

Namespace

Drupal\toolbar_themes\Form
View source
class ToolbarThemesSettingsForm extends ConfigFormBase {

  /**
   * {@inheritDoc}
   */
  public function getFormId() {
    return 'toolbar_themes_form';
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('toolbar_themes.settings');
    $admin_theme = $this
      ->config('system.theme')
      ->get('admin');
    $form['settings'] = array(
      '#type' => 'container',
      '#tree' => TRUE,
      '#title' => t('Toolbar Themes'),
    );
    $form['settings']['info'] = array(
      '#type' => 'markup',
      '#prefix' => '<p class="toolbar-themes form-info">',
      '#suffix' => '</p>',
      '#markup' => t('Select options to style the Toolbar.'),
    );

    // Table
    $header = array(
      'theme' => t('Theme'),
      'screen_shot' => t('Screen Shots'),
      'meta' => t('Meta'),
    );
    $definitions = toolbar_themes_get_theme_definitions();
    $theme_handler = \Drupal::service('theme_handler');
    $options = array();
    foreach ($definitions as $provider => $values) {
      if ($theme_handler
        ->themeExists($provider)) {
        $provider_path = drupal_get_path('theme', $provider);
      }
      else {
        $provider_path = drupal_get_path('module', $provider);
      }
      foreach ($values as $theme => $data) {
        if (!isset($data['hidden']) || $data['hidden'] !== TRUE) {

          // Label
          $label[$theme] = ucfirst(t($data['label']));

          // Description
          $description[$theme] = $data['description'] ? t($data['description']) : t('No description provided');

          // Dependencies
          $dependency[$theme] = isset($data['libraries']['dependencies']) ? $data['libraries']['dependencies'][0] : NULL;
          $screen_shot = [];
          if (isset($data['path']) && !empty($data['path']) && (isset($data['screen_shot']) && !empty($data['screen_shot']))) {
            $screen_shot[$theme] = array(
              '#theme' => 'image',
              '#uri' => $provider_path . '/' . $data['path'] . '/' . $data['screen_shot'],
              '#alt' => $description[$theme],
              '#attributes' => array(
                'class' => array(
                  'toolbar-theme-screen-shot',
                ),
              ),
            );
          }
          else {
            $screen_shot[$theme] = array(
              '#markup' => '<em>' . t('No screen shot provided') . '</em>',
            );
          }

          // Meta
          $meta[$theme]['version'] = $data['version'] ? $data['version'] : NULL;
          $meta[$theme]['meta'] = array(
            '#markup' => '<dl class="toolbar-themes-meta"><dt>' . t('Version') . '</dt><dd>' . $meta[$theme]['version'] . '</dd><dt>' . t('Provider') . '</dt><dd>' . $provider . '</dd></dl>',
          );

          // Table options
          $options[$theme] = array(
            'theme' => $label[$theme],
            'screen_shot' => \Drupal::service('renderer')
              ->render($screen_shot[$theme]),
            'meta' => \Drupal::service('renderer')
              ->render($meta[$theme]['meta']),
          );

          // Radios default value is a key, not an array.
          if ($config
            ->get('default_theme') === $theme) {
            $default = $theme;
          }
        }
      }
    }
    if (!empty($options)) {
      $form['settings']['default_theme'] = array(
        '#type' => 'tableselect',
        '#title' => t('Select toolbar theme'),
        '#header' => $header,
        '#options' => $options,
        '#multiple' => FALSE,
        '#js_select' => FALSE,
        '#default_value' => $default,
        '#empty' => t('No toolbar themes found'),
      );
    }

    //    $form['settings']['font_size'] = array(
    //      '#type' => 'number',
    //      '#title' => t('Set font size'),
    //      '#max-length' => 2,
    //      '#step' => 1,
    //      '#default_value' => $config->get('font_size'),
    //      '#attributes' => array(
    //        'min' => 10,
    //        'max' => 99,
    //        'step' => 1,
    //        'class' => array('font-size'),
    //      ),
    //      '#field_suffix' => 'px',
    //    );
    // Adminimal warnings etc.
    if ($admin_theme == 'adminimal_theme') {
      $form['settings']['font_disabled'] = array(
        '#type' => 'markup',
        '#prefix' => '<div class="messages messages--warning">',
        '#suffix' => '</div>',
        '#markup' => t('For Adminmal the font size will only apply to the front end theme.'),
      );
    }
    $form['settings']['tabs'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show toolbar tabs'),
      '#default_value' => $config
        ->get('tabs'),
      '#description' => t('Unchecked this will remove tabs except for the Manage tab which is replaced by an icon.'),
    );
    $form['settings']['icons'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show icons'),
      '#default_value' => $config
        ->get('icons'),
      '#description' => t('Unchecked this will remove icons from tab and menu items, while orientation toggle icons and sub-menu arrows remain.'),
    );
    $form['settings']['actions']['#type'] = 'actions';
    $form['settings']['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
      '#button_type' => 'primary',
    );
    return $form;
  }

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $config = \Drupal::service('config.factory')
      ->getEditable('toolbar_themes.settings');
    foreach ($values['settings'] as $key => $setting) {
      $config
        ->set($key, $setting);
    }
    $config
      ->save();
    parent::submitForm($form, $form_state);
    drupal_flush_all_caches();
  }

}

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.
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.
ToolbarThemesSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ToolbarThemesSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ToolbarThemesSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ToolbarThemesSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ToolbarThemesSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
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.