You are here

class GinLoginConfigurationForm in Gin Login 8

Class SettingsForm.

Hierarchy

Expanded class hierarchy of GinLoginConfigurationForm

1 string reference to 'GinLoginConfigurationForm'
gin_login.routing.yml in ./gin_login.routing.yml
gin_login.routing.yml

File

src/Form/GinLoginConfigurationForm.php, line 12

Namespace

Drupal\gin_login\Form
View source
class GinLoginConfigurationForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('gin_login.settings');
    $default_scheme = $this
      ->config('system.file')
      ->get('default_scheme');
    $form['logo'] = [
      '#type' => 'details',
      '#title' => t('Logo image'),
      '#open' => TRUE,
    ];
    $form['logo']['default_logo'] = [
      '#type' => 'checkbox',
      '#title' => t('Use default logo'),
      '#default_value' => $config
        ->get('logo.use_default'),
      '#tree' => FALSE,
    ];
    $form['logo']['settings'] = [
      '#type' => 'container',
      '#states' => [
        // Hide the logo settings when using the default logo.
        'invisible' => [
          'input[name="default_logo"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['logo']['settings']['logo_path'] = [
      '#type' => 'textfield',
      '#title' => t('Path to custom logo'),
      '#default_value' => $config
        ->get('logo.path') ? str_replace($default_scheme . '://', "", $config
        ->get('logo.path')) : '',
    ];
    $form['logo']['settings']['logo_upload'] = [
      '#type' => 'file',
      '#title' => t('Upload logo image'),
      '#maxlength' => 40,
      '#description' => t("If you don't have direct file access to the server, use this field to upload your logo."),
      '#upload_validators' => [
        'file_validate_is_image' => [],
      ],
    ];
    $form['brand_image'] = [
      '#type' => 'details',
      '#title' => t('Brand image'),
      '#open' => TRUE,
    ];
    $form['brand_image']['default_brand_image'] = [
      '#type' => 'checkbox',
      '#title' => t('Use random image'),
      '#default_value' => $config
        ->get('brand_image.use_default'),
      '#tree' => FALSE,
    ];
    $form['brand_image']['settings'] = [
      '#type' => 'container',
      '#states' => [
        // Hide the logo settings when using the default logo.
        'invisible' => [
          'input[name="default_brand_image"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['brand_image']['settings']['brand_image_path'] = [
      '#type' => 'textfield',
      '#title' => t('Path to custom brand image'),
      '#default_value' => $config
        ->get('brand_image.path') ? str_replace($default_scheme . '://', "", $config
        ->get('brand_image.path')) : '',
    ];
    $form['brand_image']['settings']['brand_image_upload'] = [
      '#type' => 'file',
      '#title' => t('Upload Brand image'),
      '#maxlength' => 40,
      '#description' => t("If you don't have direct file access to the server, use this field to upload your brand image."),
      '#upload_validators' => [
        'file_validate_is_image' => [],
      ],
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    $moduleHandler = \Drupal::service('module_handler');
    if ($moduleHandler
      ->moduleExists('file')) {

      // Check for a new uploaded logo.
      if (isset($form['logo'])) {
        $file = _file_save_upload_from_form($form['logo']['settings']['logo_upload'], $form_state, 0);
        if ($file) {

          // Put the temporary file in form_values so we can save it on submit.
          $form_state
            ->setValue('logo_upload', $file);
        }
      }

      // When intending to use the default logo, unset the logo_path.
      if ($form_state
        ->getValue('default_logo')) {
        $form_state
          ->unsetValue('logo_path');
      }

      // If the user provided a path for a logo or favicon file, make sure a file
      // exists at that path.
      if ($form_state
        ->getValue('logo_path')) {
        $path = $this
          ->validatePath($form_state
          ->getValue('logo_path'));
        if (!$path) {
          $form_state
            ->setErrorByName('logo_path', $this
            ->t('The custom logo path is invalid.'));
        }
      }

      // Check for a new uploaded Brand Image.
      if (isset($form['brand_image'])) {
        $file = _file_save_upload_from_form($form['brand_image']['settings']['brand_image_upload'], $form_state, 0);
        if ($file) {

          // Put the temporary file in form_values so we can save it on submit.
          $form_state
            ->setValue('brand_image_upload', $file);
        }
      }

      // When intending to use the default brand image, unset the brand_image_path.
      if ($form_state
        ->getValue('default_brand_image')) {
        $form_state
          ->unsetValue('brand_image_path');
      }

      // If the user provided a path for a brand image make sure a file
      // exists at that path.
      if ($form_state
        ->getValue('brand_image_path')) {
        $path = $this
          ->validatePath($form_state
          ->getValue('brand_image_path'));
        if (!$path) {
          $form_state
            ->setErrorByName('brand_image_path', $this
            ->t('The custom brand image path is invalid.'));
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $values = $form_state
      ->getValues();
    $file_system = \Drupal::service('file_system');
    $default_scheme = $this
      ->config('system.file')
      ->get('default_scheme');
    $config = $this
      ->config('gin_login.settings');
    try {
      if (!empty($values['logo_upload'])) {
        $filename = $file_system
          ->copy($values['logo_upload']
          ->getFileUri(), $default_scheme . '://');
        $values['default_logo'] = 0;
        $values['logo_path'] = $filename;
      }
    } catch (FileException $e) {

      // Ignore.
    }
    try {
      if (!empty($values['brand_image_upload'])) {
        $filename = $file_system
          ->copy($values['brand_image_upload']
          ->getFileUri(), $default_scheme . '://');
        $values['default_brand_image'] = 0;
        $values['brand_image_path'] = $filename;
      }
    } catch (FileException $e) {

      // Ignore.
    }
    unset($values['logo_upload']);
    unset($values['favicon_upload']);

    // If the user entered a path relative to the system files directory for
    // a logo store a public:// URI so the theme system can handle it.
    if (!empty($values['logo_path'])) {
      $values['logo_path'] = $this
        ->validatePath($values['logo_path']);
    }

    // If the user entered a path relative to the system files directory for
    // a brand images, store a public:// URI so the theme system can handle it.
    if (!empty($values['brand_image_path'])) {
      $values['brand_image_path'] = $this
        ->validatePath($values['brand_image_path']);
    }
    foreach ($values as $key => $value) {
      if ($key == 'default_logo') {
        $config
          ->set('logo.use_default', $value);
      }
      elseif ($key == 'logo_path') {
        $config
          ->set('logo.path', $value);
      }
      elseif ($key == 'default_brand_image') {
        $config
          ->set('brand_image.use_default', $value);
      }
      elseif ($key == 'brand_image_path') {
        $config
          ->set('brand_image.path', $value);
      }
    }
    $config
      ->save();

    // Rebuild the router.
    \Drupal::service('router.builder')
      ->rebuild();
  }

  /**
   * Helper function for the system_theme_settings form.
   *
   * Attempt to validate normal system paths, paths relative to the public files
   * directory, or stream wrapper URIs. If the given path is any of the above,
   * returns a valid path or URI that the theme system can display.
   *
   * @param string $path
   *   A path relative to the Drupal root or to the public files directory, or
   *   a stream wrapper URI.
   * @return mixed
   *   A valid path that can be displayed through the theme system, or FALSE if
   *   the path could not be validated.
   */
  protected function validatePath($path) {
    $file_system = \Drupal::service('file_system');

    // Absolute local file paths are invalid.
    if ($file_system
      ->realpath($path) == $path) {
      return FALSE;
    }

    // A path relative to the Drupal root or a fully qualified URI is valid.
    if (is_file($path)) {
      return $path;
    }

    // Prepend 'public://' for relative file paths within public filesystem.
    if (StreamWrapperManager::getScheme($path) === FALSE) {
      $path = 'public://' . $path;
    }
    if (is_file($path)) {
      return $path;
    }
    return FALSE;
  }

}

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