You are here

class SettingsForm in Private files download permission 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/SettingsForm.php \Drupal\pfdp\Form\SettingsForm

Settings form for private_files_download_permission.

Hierarchy

Expanded class hierarchy of SettingsForm

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

File

src/Form/SettingsForm.php, line 11

Namespace

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $settings = \Drupal::config('pfdp.settings');

    // Prepare the fields.
    $form['by_user_checks'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable by-user checks'),
      '#default_value' => $settings
        ->get('by_user_checks'),
      '#description' => $this
        ->t('You may wish to disable this feature if there are plenty of users, as it may slow down the entire site.'),
    ];
    $form['cache_users'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Cache user list'),
      '#default_value' => $settings
        ->get('cache_users'),
      '#description' => $this
        ->t('For sites with lots of users, this will save on load time when editing directory settings.'),
      '#states' => [
        'visible' => [
          ':input[name="by_user_checks"]' => [
            'checked' => TRUE,
          ],
        ],
        'enabled' => [
          ':input[name="by_user_checks"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['attachment_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable attachment mode'),
      '#default_value' => $settings
        ->get('attachment_mode'),
      '#description' => $this
        ->t('Have files downloaded as attachments instead of displayed inline in the browser.'),
    ];
    $form['override_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable override mode'),
      '#default_value' => $settings
        ->get('override_mode'),
      '#description' => $this
        ->t('Skip the system-wide validation chain and download files immediately.'),
    ];
    $form['debug_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable debug mode'),
      '#default_value' => $settings
        ->get('debug_mode'),
      '#description' => $this
        ->t('Turn on logging to debug issues.'),
    ];

    // Prepare the submit button.
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save settings'),
    ];

    // Return the form.
    return $form;
  }

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $settings = \Drupal::configFactory()
      ->getEditable('pfdp.settings');

    // Save all settings.
    $settings
      ->set('by_user_checks', $form_state
      ->getValue('by_user_checks'))
      ->save();
    if (!$form_state
      ->getValue('by_user_checks')) {
      $settings
        ->set('cache_users', FALSE)
        ->save();
    }
    else {
      $settings
        ->set('cache_users', $form_state
        ->getValue('cache_users'))
        ->save();
    }
    $settings
      ->set('attachment_mode', $form_state
      ->getValue('attachment_mode'))
      ->save();
    $settings
      ->set('override_mode', $form_state
      ->getValue('override_mode'))
      ->save();
    $settings
      ->set('debug_mode', $form_state
      ->getValue('debug_mode'))
      ->save();

    // Display the status message.
    \Drupal::messenger()
      ->addStatus($this
      ->t('Your settings were saved successfully.'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 18
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 16
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.
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::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
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::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
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.