You are here

class GoogleQRCodeAdminForm in Google QR Code Generator 8

Hierarchy

Expanded class hierarchy of GoogleQRCodeAdminForm

1 string reference to 'GoogleQRCodeAdminForm'
google_qr_code.routing.yml in ./google_qr_code.routing.yml
google_qr_code.routing.yml

File

src/Form/GoogleQRCodeAdminForm.php, line 16

Namespace

Drupal\google_qr_code\Form
View source
class GoogleQRCodeAdminForm extends ConfigFormBase {

  // Establish form ID.
  public function getFormID() {
    return 'googleqrcode_admin_settings';
  }

  // Establish what values are editable via the form.
  public function getEditableConfigNames() {
    return [
      'google_qr_code.settings',
    ];
  }

  // Build out the form.
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Load configuration values
    $config = $this
      ->config('google_qr_code.settings');
    $form['google_qr_code_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Google QR Code Configuration'),
    );
    $form['google_qr_code_settings']['google_qr_code_when_show'] = array(
      '#type' => 'select',
      '#title' => t('When to render QR COde'),
      '#options' => array(
        "on_pageload" => t('On Page Load'),
        "on_click" => t('On Click'),
      ),
      '#default_value' => $config
        ->get('whenshow') ? $config
        ->get('whenshow') : 'on_pageload',
      '#required' => FALSE,
      '#description' => t('Choose whether you want the QR code to load everytime the page loads (jQuery code) or only get the QR code when generate text in block is clicked.'),
    );
    $form['google_qr_code_image_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Google QR Code Image Settings'),
      '#description' => t('Configure QR code width and height here. There is a
maximum size of 1000 pixels for any single dimension, and a total size
of 300,000 pixels'),
    );
    $form['google_qr_code_image_settings']['google_qr_code_height'] = array(
      '#type' => 'textfield',
      '#title' => t('QR Code Height'),
      '#default_value' => $config
        ->get('height') ? $config
        ->get('height') : 250,
      '#size' => 40,
      '#maxlength' => 255,
      '#required' => TRUE,
      '#description' => t('Enter the QR Code Height'),
    );
    $form['google_qr_code_image_settings']['google_qr_code_width'] = array(
      '#type' => 'textfield',
      '#title' => t('QR Code Width'),
      '#default_value' => $config
        ->get('width') ? $config
        ->get('width') : 250,
      '#size' => 40,
      '#maxlength' => 255,
      '#required' => TRUE,
      '#description' => t('Enter the QR Code Width'),
    );
    return parent::buildForm($form, $form_state);
  }

  // Form validation.
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // Check that entered values are numeric.
    if (!is_numeric($form_state
      ->getValue('google_qr_code_height')) || !is_numeric($form_state
      ->getValue('google_qr_code_width'))) {
      $error_text = $this
        ->t('Values entered for height and width must be numeric!');
      $form_state
        ->setErrorByName('google_qr_code_image_settings', $error_text);
    }

    // Check entered pixels VS maximum amount of pixels.
    $total_pixels = $form_state
      ->getValue('google_qr_code_height') * $form_state
      ->getValue('google_qr_code_width');
    if ($total_pixels > 300000) {
      $error_text = $this
        ->t('Total dimensions cannot exceed 300,000px. Currently at @total px', array(
        '@total' => $total_pixels,
      ));
      $form_state
        ->setErrorByName('google_qr_code_image_settings', $error_text);
    }
    parent::validateForm($form, $form_state);

    // TODO: Change the autogenerated stub
  }

  // Submit form processing function.
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('google_qr_code.settings')
      ->set('whenshow', $form_state
      ->getValue('google_qr_code_when_show'))
      ->set('height', $form_state
      ->getValue('google_qr_code_height'))
      ->set('width', $form_state
      ->getValue('google_qr_code_width'))
      ->save();
    parent::submitForm($form, $form_state);
  }

  // Helper function to determine a fields maximum singular dimension.
  private function _google_qr_code_max_single_dimension($element) {
    if (!empty($element['#value']) && !is_numeric($element['#value'])) {
      form_error($element, t('Has to be a number. Do not include "px"'));
    }
    else {
      if (!empty($element['#value']) && $element['#value'] > 1000) {
        form_error($element, t('Google does not allow single dimensions over 1000px'));
      }
    }
  }

}

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