You are here

class SettingsForm in Content Planner 8

Same name in this branch
  1. 8 modules/content_kanban/src/Form/SettingsForm.php \Drupal\content_kanban\Form\SettingsForm
  2. 8 modules/content_calendar/src/Form/SettingsForm.php \Drupal\content_calendar\Form\SettingsForm

Defines a form that configures forms module settings.

Hierarchy

Expanded class hierarchy of SettingsForm

3 files declare their use of SettingsForm
KanbanEntry.php in modules/content_kanban/src/Component/KanbanEntry.php
KanbanFilterForm.php in modules/content_kanban/src/Form/KanbanFilterForm.php
KanbanService.php in modules/content_kanban/src/KanbanService.php
1 string reference to 'SettingsForm'
content_kanban.routing.yml in modules/content_kanban/content_kanban.routing.yml
modules/content_kanban/content_kanban.routing.yml

File

modules/content_kanban/src/Form/SettingsForm.php, line 15

Namespace

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

  /**
   * Config name.
   */
  const CONFIG_NAME = 'content_kanban.settings';

  /**
   *  Default Date Range value.
   */
  const DEFAULT_DATE_RANGE_VALUE = 30;

  /**
   * The stored config for the form.
   *
   * @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The Kanban service.
   *
   * @var \Drupal\content_kanban\KanbanService
   */
  protected $kanbanService;

  /**
   * Constructor for the Settings Form.
   */
  public function __construct(KanbanService $kanbanService, ConfigFactoryInterface $config_factory) {
    parent::__construct($config_factory);
    $this->kanbanService = $kanbanService;

    // Get config.
    $this->config = $this
      ->config(self::CONFIG_NAME);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('content_kanban.kanban_service'), $container
      ->get('config.factory'));
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {

    // If the Content Calendar module is not enabled, set the use color setting
    // to inactive.
    if (!$this->kanbanService
      ->contentCalendarModuleIsEnabled()) {
      $this
        ->saveColorSetting(0);
    }
    $config = $this
      ->config(self::CONFIG_NAME);
    $form['advice'] = [
      '#title' => 'Disclaimer',
      '#type' => 'fieldset',
      '#weight' => 78,
      '#description' => $this
        ->t('Ensure all workflow state are set as a Default Revision in order to allow kanban table to work properly'),
    ];
    $form['options'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Options'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    ];

    // Content Calendar colors.
    $form['options']['use_content_calendar_colors'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use the defined colors from Content Calendar'),
      '#description' => $this
        ->t('This setting is only available if the Content Calendar is enabled and configured properly.'),
      '#default_value' => $config
        ->get('use_content_calendar_colors'),
      '#disabled' => !$this->kanbanService
        ->contentCalendarModuleIsEnabled(),
    ];

    // Show user thumb checkbox.
    $user_picture_field_exists = !$this
      ->config('field.field.user.user.user_picture')
      ->isNew();
    $form['options']['show_user_thumb'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show thumbnail image of User image'),
      '#description' => $this
        ->t('This option is only available, if the User account has the "user_picture" field. See Account configuration.'),
      '#disabled' => !$user_picture_field_exists,
      '#default_value' => $this->config
        ->get('show_user_thumb'),
    ];
    $default_date_range_value = self::DEFAULT_DATE_RANGE_VALUE;
    if ($this->config
      ->get('default_filter_date_range')) {
      $default_date_range_value = $this->config
        ->get('default_filter_date_range');
    }
    $form['options']['default_filter_date_range'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Date range'),
      '#options' => \Drupal\content_kanban\Form\KanbanFilterForm::getDateRangeOptions(),
      '#required' => FALSE,
      '#empty_value' => '',
      '#empty_option' => $this
        ->t('All'),
      '#default_value' => $default_date_range_value,
    ];
    return parent::buildForm($form, $form_state);
  }

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

    // Get values.
    $values = $form_state
      ->getValues();

    // Get value to use Content Calendar colors.
    $use_content_calendar_colors = $values['use_content_calendar_colors'];

    // If Content Calendar module is disabled, then disable usage of colors.
    if (!$this->kanbanService
      ->contentCalendarModuleIsEnabled()) {
      $use_content_calendar_colors = 0;
    }

    // Save settings into configuration.
    $this
      ->saveColorSetting($use_content_calendar_colors);

    // Save show user image thumbnail option.
    $this
      ->config(self::CONFIG_NAME)
      ->set('show_user_thumb', $values['show_user_thumb'])
      ->set('default_filter_date_range', $values['default_filter_date_range'])
      ->save();
  }

  /**
   * Saves the color setting.
   *
   * @param int $value
   *   The "use content calendar colors" setting value.
   */
  protected function saveColorSetting($value) {
    $this
      ->config(self::CONFIG_NAME)
      ->set('use_content_calendar_colors', $value)
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::$config protected property The stored config for the form.
SettingsForm::$kanbanService protected property The Kanban service.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::CONFIG_NAME constant Config name.
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::DEFAULT_DATE_RANGE_VALUE constant Default Date Range value.
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::saveColorSetting protected function Saves the color setting.
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::__construct public function Constructor for the Settings Form. Overrides ConfigFormBase::__construct
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.