You are here

class Simplelogin in SimpleLogin 8.6

Defines a form that configure settings.

Hierarchy

Expanded class hierarchy of Simplelogin

1 string reference to 'Simplelogin'
simplelogin.routing.yml in ./simplelogin.routing.yml
simplelogin.routing.yml

File

src/Form/Simplelogin.php, line 22
Contains \Drupal\simplelogin\Form\SettingsForm. Simplelogin settings form.

Namespace

Drupal\simplelogin\Form
View source
class Simplelogin extends ConfigFormBase {

  /**
   * Image factory.
   *
   * @var \Drupal\Core\Image\ImageFactory
   *   Image factory.
   */
  protected $imageFactory;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, ImageFactory $image_factory) {
    parent::__construct($config_factory);
    $this->imageFactory = $image_factory;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
    $form_state
      ->disableCache();
    $simplelogin_config = $this
      ->config('simplelogin.settings');
    $imageid = $simplelogin_config
      ->get('background_image');
    if ($imageid) {
      $file = File::load($imageid[0]);

      // File Load
      $fileUrl = $file
        ->getFileUri();

      // Check if image is valid.
      $image = $this->imageFactory
        ->get($fileUrl);
      if ($image
        ->isValid()) {
        $image_render = array(
          '#theme' => 'image_style',
          '#width' => $image
            ->getWidth(),
          '#height' => $image
            ->getHeight(),
          '#style_name' => 'medium',
          '#uri' => $fileUrl,
        );
      }
    }
    $form['simplelogin'] = array(
      '#type' => 'details',
      '#title' => $this
        ->t('Configuration'),
      '#open' => TRUE,
      'background_active' => array(
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Background Image'),
        '#default_value' => $simplelogin_config
          ->get('background_active'),
        '#description' => $this
          ->t('If enabled, the background image will be added on the simple login pages.'),
      ),
      'settings' => array(
        '#type' => 'details',
        '#title' => $this
          ->t('Background Image Settings'),
        '#open' => TRUE,
        'background_image' => array(
          '#type' => 'managed_file',
          '#name' => 'Background Image',
          '#title' => $this
            ->t('Image'),
          '#default_value' => $simplelogin_config
            ->get('background_image') ? $simplelogin_config
            ->get('background_image') : '',
          '#description' => $this
            ->t('Upload an image file for the Simplelogin pages'),
          '#upload_location' => 'public://simplelogin/',
          '#multiple' => FALSE,
          '#upload_validators' => array(
            'file_validate_is_image' => array(),
            'file_validate_extensions' => array(
              'gif png jpg jpeg',
            ),
            'file_validate_size' => array(
              25600000,
            ),
          ),
        ),
        'background_url' => array(
          '#type' => 'item',
          '#markup' => render($image_render),
        ),
        'background_opacity' => array(
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Opacity'),
          '#default_value' => $simplelogin_config
            ->get('background_opacity'),
          '#description' => $this
            ->t('If enabled, the opacity will be added to the SimpleLogin pages. so the background image will take up with opacity.'),
        ),
      ),
      'background_color' => array(
        '#type' => 'color',
        '#title' => $this
          ->t('Color'),
        '#required' => TRUE,
        '#default_value' => $simplelogin_config
          ->get('background_color') ? $simplelogin_config
          ->get('background_color') : '#00bfff',
        //#76b852',
        '#description' => $this
          ->t('If you want the background color you need to remove the background image. (example: [Red: 0, Green:191, Blue:255] , [red:118, Green:184, Blue:82])'),
      ),
      'wrapper_width' => array(
        '#type' => 'textfield',
        '#title' => $this
          ->t('Wrapper width'),
        '#default_value' => $simplelogin_config
          ->get('wrapper_width') ? $simplelogin_config
          ->get('wrapper_width') : '360',
        '#description' => $this
          ->t('Simplelogin wrapper width in pixels.(example:360)'),
        '#field_suffix' => 'PX',
        '#size' => 5,
        '#required' => TRUE,
      ),
      'advanced' => array(
        '#type' => 'details',
        '#title' => $this
          ->t('Advanced Settings'),
        '#open' => FALSE,
        'unset_active_css' => array(
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Unset Active theme CSS files'),
          '#default_value' => $simplelogin_config
            ->get('unset_active_css'),
          '#description' => $this
            ->t('If enabled, active theme CSS files are removed from the simple login pages.'),
        ),
        'unset_css' => array(
          '#type' => 'textarea',
          '#title' => $this
            ->t('Unset CSS file path'),
          '#default_value' => $simplelogin_config
            ->get('unset_css') ? $simplelogin_config
            ->get('unset_css') : '',
          '#description' => $this
            ->t('Remove unwanted CSS files each path as a separate line(example: core/themes/classy/css/components/button.css)'),
        ),
      ),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $background_image = $form_state
      ->getValue([
      'background_image',
    ]);
    $opacity = $form_state
      ->getValue([
      'background_opacity',
    ]);
    if (empty($background_image) && !empty($opacity)) {
      $form_state
        ->setErrorByName('background_image', "Opacity is applicable only for images. if image empty means we won't need Opacity. Please uncheck");
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $image_id = isset($values['background_image']) ? $values['background_image'] : '';
    if (!empty($image_id)) {
      $file = File::load($image_id[0]);
      if ($file instanceof File) {
        $file
          ->setPermanent();

        // FILE_STATUS_PERMANENT;
        $file
          ->save();
      }
    }
    $this
      ->config('simplelogin.settings')
      ->set('background_active', $values['background_active'])
      ->set('background_image', $values['background_image'])
      ->set('background_color', $values['background_color'])
      ->set('background_opacity', $values['background_opacity'])
      ->set('wrapper_width', $values['wrapper_width'])
      ->set('unset_active_css', $values['unset_active_css'])
      ->set('unset_css', $values['unset_css'])
      ->save();
    drupal_flush_all_caches();
  }

}

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.
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.
Simplelogin::$imageFactory protected property Image factory.
Simplelogin::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
Simplelogin::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
Simplelogin::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
Simplelogin::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
Simplelogin::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
Simplelogin::validateForm public function Form validation handler. Overrides FormBase::validateForm
Simplelogin::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 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.