View source
<?php
namespace Drupal\google_qr_code\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class GoogleQRCodeAdminForm extends ConfigFormBase {
public function getFormID() {
return 'googleqrcode_admin_settings';
}
public function getEditableConfigNames() {
return [
'google_qr_code.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$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);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
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);
}
$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);
}
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);
}
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'));
}
}
}
}