class SiteVerifyAdminForm in Site verification 8
Configure cron settings for this site.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\site_verify\Form\SiteVerifyAdminForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SiteVerifyAdminForm
1 string reference to 'SiteVerifyAdminForm'
File
- src/
Form/ SiteVerifyAdminForm.php, line 13
Namespace
Drupal\site_verify\FormView source
class SiteVerifyAdminForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'site_verify.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'site_verify_admin';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $record = [], $engine = NULL, $site_verify = NULL) {
if (!empty($site_verify)) {
$record = \Drupal::service('site_verify_service')
->siteVerifyLoad($site_verify);
}
$storage = $form_state
->getStorage();
if (!isset($storage['step'])) {
$record += [
'svid' => NULL,
'file' => '',
'file_contents' => $this
->t('This is a verification page.'),
'meta' => '',
'engine' => $engine,
];
!empty($record['engine']) ? $form_state
->setStorage([
'step' => 2,
'record' => $record,
]) : $form_state
->setStorage([
'step' => 1,
'record' => $record,
]);
}
else {
$record = $storage['record'];
}
$form['actions'] = [
'#type' => 'actions',
];
$storage = $form_state
->getStorage();
switch ($storage['step']) {
case 1:
$engines = \Drupal::service('site_verify_service')
->siteVerifyGetEngines();
$options = [];
foreach ($engines as $key => $engine) {
$options[$key] = $engine['name'];
}
asort($options);
$form['engine'] = [
'#type' => 'select',
'#title' => $this
->t('Search engine'),
'#options' => $options,
];
break;
case 2:
$form['svid'] = [
'#type' => 'value',
'#value' => $record['svid'],
];
$form['engine'] = [
'#type' => 'value',
'#value' => $record['engine']['key'],
];
$form['engine_name'] = [
'#type' => 'item',
'#title' => $this
->t('Search engine'),
'#markup' => $record['engine']['name'],
];
$form['#engine'] = $record['engine'];
$form['meta'] = [
'#type' => 'textfield',
'#title' => $this
->t('Verification META tag'),
'#default_value' => $record['meta'],
'#description' => $this
->t('This is the full meta tag provided for verification. Note that this meta tag will only be visible in the source code of your <a href="@frontpage">front page</a>.', [
'@frontpage' => Url::fromRoute('<front>')
->toString(),
]),
'#element_validate' => $record['engine']['meta_validate'],
'#access' => $record['engine']['meta'],
'#maxlength' => NULL,
'#attributes' => [
'placeholder' => $record['engine']['meta_example'],
],
];
$form['file_upload'] = [
'#type' => 'file',
'#title' => $this
->t('Upload an existing verification file'),
'#description' => $this
->t('If you have been provided with an actual file, you can simply upload the file.'),
'#access' => $record['engine']['file'],
];
$form['file'] = [
'#type' => 'textfield',
'#title' => $this
->t('Verification file'),
'#default_value' => $record['file'],
'#description' => $this
->t('The name of the HTML verification file you were asked to upload.'),
'#element_validate' => $record['engine']['file_validate'],
'#access' => $record['engine']['file'],
'#attributes' => [
'placeholder' => $record['engine']['file_example'],
],
];
$form['file_contents'] = [
'#type' => 'textarea',
'#title' => $this
->t('Verification file contents'),
'#default_value' => $record['file_contents'],
'#element_validate' => $record['engine']['file_contents_validate'],
'#wysiwyg' => FALSE,
'#access' => $record['file_contents'],
];
if ($record['engine']['file']) {
$form['#attributes'] = [
'enctype' => 'multipart/form-data',
];
}
break;
}
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#url' => isset($_GET['destination']) ? $_GET['destination'] : Url::fromRoute('site_verify.verifications_list'),
'#weight' => 15,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$storage = $form_state
->getStorage();
$values =& $form_state
->getValues();
// Check META tag.
$form_state
->setValue('meta', trim($values['meta']));
if ($values['meta'] != '' && !preg_match('/<meta (.*)>/', $values['meta'])) {
$form_state
->setErrorByName('meta', $this
->t('Only META tags are supported at this moment'));
}
// Check verification file.
if ($storage['record']['engine']['file']) {
// Import the uploaded verification file.
$validators = [
'file_validate_extensions' => [],
];
if ($file = file_save_upload('file_upload', $validators, FALSE, 0, FileSystemInterface::EXISTS_REPLACE)) {
$contents = @file_get_contents($file
->getFileUri());
$file
->delete();
if ($contents === FALSE) {
$this
->messenger()
->addError(t('The verification file import failed, because the file %filename could not be read.', [
'%filename' => $file
->getFilename(),
]));
}
else {
$values['file'] = $file
->getFilename();
$values['file_contents'] = $contents;
}
}
if ($values['file']) {
$existing_file = \Drupal::database()
->query("SELECT svid FROM {site_verify} WHERE LOWER(file) = LOWER(:file)", [
':file' => $values['file'],
])
->fetchField();
if ($existing_file && $values['svid'] !== $existing_file) {
$form_state
->setErrorByName('file', $this
->t('The file %filename is already being used in another verification.', [
'%filename' => $values['file'],
]));
}
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$storage = $form_state
->getStorage();
if ($storage['step'] == 1) {
// Send the form to step 2 (verification details).
$form_state
->setStorage([
'record' => [
'engine' => \Drupal::service('site_verify_service')
->siteVerifyEngineLoad($form_state
->getValue('engine')),
],
'step' => 2,
]);
$form_state
->setRebuild(TRUE);
}
else {
// Save the verification to the database.
\Drupal::database()
->merge('site_verify')
->key('svid', $form_state
->getValue('svid'))
->fields([
'engine' => $form_state
->getValue('engine'),
'file' => $form_state
->getValue('file'),
'file_contents' => $form_state
->getValue('file_contents'),
'meta' => $form_state
->getValue('meta'),
])
->execute();
$this
->messenger()
->addStatus(t('Verification saved.'));
$form_state
->setStorage([]);
$form_state
->setRebuild(NULL);
$form_state
->setRedirect('site_verify.verifications_list');
// Set the menu to be rebuilt.
\Drupal::service('router.builder')
->setRebuildNeeded();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SiteVerifyAdminForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SiteVerifyAdminForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SiteVerifyAdminForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SiteVerifyAdminForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SiteVerifyAdminForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |