Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Drupal 8: Forms
1. Drupal 8: FormsDrupal 8: Forms
Working with forms in Drupal 8 modulesWorking with forms in Drupal 8 modules
Drupal Meetup StuttgartDrupal Meetup Stuttgart
05/07/2015
5. Examples for base formsExamples for base forms
FormBase (generic)
ConfigFormBase (settings forms)
ConfirmFormBase (confirmation forms)
XYZFormBase (roll your own)
6. <?php
/**
* @file
* Contains DrupalconfigFormConfigExportForm.
*/
namespace DrupalconfigForm;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
class ConfigExportForm extends FormBase {
public function getFormId() {
return 'config_export_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = array(
'#markup' => '<p>' . $this->t('Use the export button below to download your site configuration.') . '</p>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Export'),
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setRedirect('config.export_download');
}
}
Complete class: DrupalconfigFormConfigExportForm
9. ...
public function translatePage() {
return array(
'filter' => $this->formBuilder()->getForm('DrupallocaleFormTranslateFilterForm'),
'form' => $this->formBuilder()->getForm('DrupallocaleFormTranslateEditForm'),
);
}
...
DrupallocaleControllerlocaleContoller
<?php
namespace DrupallocaleForm;
use DrupalCoreFormFormStateInterface;
class TranslateFilterForm extends TranslateFormBase {
...
}
Embedding a form in other markup
DrupallocaleFormTranslateFilterForm
12. public function validateForm(array &$form, FormStateInterface $form_state) {
$this->file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://', 0);
// Ensure we have the file uploaded.
if (!$this->file) {
$form_state->setErrorByName('file', $this->t('File to import not found.'))
}
}
Example: DrupallocaleFormImportForm
13. public function validateForm(array &$form, FormStateInterface $form_state) {
$name = trim($form_state->getValue('name'));
// Try to load by email.
$users = $this->userStorage->loadByProperties(array('mail' => $name, 'status' => '1'));
if (empty($users)) {
// No success, try to load by name.
$users = $this->userStorage->loadByProperties(array('name' => $name, 'status' => '1'));
}
$account = reset($users);
if ($account && $account->id()) {
$form_state->setValueForElement(array('#parents' => array('account')), $account);
}
else {
$form_state->setErrorByName('name', $this->t('Sorry, %name is not recognized as a
username or an email address.', array('%name' => $name)));
}
}
Example: DrupaluserFormUserPasswordForm
17. public function submitForm(array &$form, FormStateInterface $form_state) {
$allowed_types = array_filter($form_state->getValue('book_allowed_types'));
// We need to save the allowed types in an array ordered by machine_name so
// that we can save them in the correct order if node type changes.
// @see book_node_type_update().
sort($allowed_types);
$this->config('book.settings')
// Remove unchecked types.
->set('allowed_types', $allowed_types)
->set('child_type', $form_state->getValue('book_child_type'))
->save();
parent::submitForm($form, $form_state);
}
Example: DrupalbookFormBookSettingsForm
20. <?php
/**
* @file
* Contains DrupalblockFormBlockDeleteForm.
*/
namespace DrupalblockForm;
use DrupalCoreEntityEntityDeleteForm;
use DrupalCoreUrl;
/**
* Provides a deletion confirmation form for the block instance deletion form.
*/
class BlockDeleteForm extends EntityDeleteForm {
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('block.admin_display');
}
}
Example: DrupalblockFormBlockDeleteForm
22. Some old friends:Some old friends:
hook_form_alter()
hook_form_FORM_ID_alter()
hook_form_BASE_FORM_ID_alter()
23. ...
use DrupalCoreFormFormStateInterface;
...
function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Add a checkbox to the node form about agreeing to terms of use.
$form['terms_of_use'] = array(
'#type' => 'checkbox',
'#title' => t("I agree with the website's terms and conditions."),
'#required' => TRUE,
);
}
Example: mymodule.module