class FormBuilder in Secure Pages 8
FormBuilder decorator for the core formbuilder.
Hierarchy
- class \Drupal\Core\Form\FormBuilder implements FormBuilderInterface, FormCacheInterface, FormSubmitterInterface, FormValidatorInterface, TrustedCallbackInterface
- class \Drupal\securepages\FormBuilder
Expanded class hierarchy of FormBuilder
1 string reference to 'FormBuilder'
1 service uses FormBuilder
File
- src/
FormBuilder.php, line 17 - Contains \Drupal\securepages\FormBuilder.
Namespace
Drupal\securepagesView source
class FormBuilder extends CoreFormBuilder {
/**
* {@inheritdoc}
*/
public function prepareForm($form_id, &$form, FormStateInterface &$form_state) {
// Override \Drupal\Core\Form\FormBuilder::renderPlaceholderFormAction(),
// the default form action lazy builder, with Secure Pages' variant. Since
// Secure Pages allows specific forms to opt in, we need to not only
// override the #lazy_builder callback but also the arguments: the form ID
// also needs to be known.
// Only update the action if it is not already set.
$config = \Drupal::config('securepages.settings');
if ($config
->get('enabled') && !isset($form['#action'])) {
// Instead of setting an actual action URL, we set the placeholder, which
// will be replaced at the very last moment. This ensures forms with
// dynamically generated action URLs don't have poor cacheability.
// Use the proper API to generate the placeholder, when we have one. See
// https://www.drupal.org/node/2562341.
$placeholder = 'form_action_' . hash('crc32b', __METHOD__);
// $form['#attached']['placeholders'][$placeholder] = [
// '#lazy_builder' => ['securepages.form_builder:renderPlaceholderFormAction', [$form_id]],
// ];
$form['#action'] = $placeholder;
}
return parent::prepareForm($form_id, $form, $form_state);
}
/**
* {@inheritdoc}
*/
public function renderPlaceholderFormAction($form_id = '') {
// This duplicates the logic of the parent method. We need to duplicate it
// because we need to pass an additional argument to ::buildFormAction().
$form_action = [
'#type' => 'markup',
'#markup' => $this
->buildFormAction($form_id),
'#cache' => [
'contexts' => [
'url.path',
'url.query_args',
],
],
];
// Due to dependency on Request::isSecure().
$form_action['#cache']['contexts'][] = 'url.site';
// Due to dependency on \Drupal\securepages\Securepages::matchCurrentUser().
$form_action['#cache']['contexts'][] = 'user.roles';
// The generated form action depends on the Secure Pages configuration.
$cacheability = new CacheableMetadata();
$cacheability
->addCacheableDependency(\Drupal::config('securepages.settings'));
$cacheability
->applyTo($form_action);
return $form_action;
}
/**
* {@inheritdoc}
*/
protected function buildFormAction($form_id = '') {
$url = parent::buildFormAction();
$request = $this->requestStack
->getCurrentRequest();
$config = \Drupal::config('securepages.settings');
$is_https = $request
->isSecure();
$path = \Drupal::service('path.current')
->getPath($request);
$path_match = Securepages::matchPath($path);
$role_match = Securepages::matchCurrentUser();
$form_match = Securepages::matchFormId($form_id);
if ($role_match || $path_match && !$is_https || !(!$path_match && $is_https && $config
->get('switch')) || $form_match) {
$url = rtrim(Securepages::getUrl('<front>')
->toString(), '/') . $url;
}
return $url;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormBuilder:: |
protected | property | The class resolver. | |
FormBuilder:: |
protected | property | The CSRF token generator to validate the form token. | |
FormBuilder:: |
protected | property | The current user. | |
FormBuilder:: |
protected | property | The element info manager. | |
FormBuilder:: |
protected | property | The event dispatcher. | |
FormBuilder:: |
protected | property | The form cache. | |
FormBuilder:: |
protected | property | The form submitter. | |
FormBuilder:: |
protected | property | The form validator. | |
FormBuilder:: |
protected | property | The module handler. | |
FormBuilder:: |
protected | property | The request stack. | |
FormBuilder:: |
protected | property | Defines element value callables which are safe to run even when the form state has an invalid CSRF token. | |
FormBuilder:: |
protected | property | The theme manager. | |
FormBuilder:: |
public | function |
Builds and processes a form for a given form ID. Overrides FormBuilderInterface:: |
|
FormBuilder:: |
protected | function |
Builds the $form['#action']. Overrides FormBuilder:: |
|
FormBuilder:: |
protected | function | Determines if a given button triggered the form submission. | |
FormBuilder:: |
protected | function | Gets the current active user. | |
FormBuilder:: |
public | function |
Deletes a form in the cache. Overrides FormCacheInterface:: |
|
FormBuilder:: |
public | function |
Builds and processes all elements in the structured form array. Overrides FormBuilderInterface:: |
|
FormBuilder:: |
public | function |
Handles the submitted form, executing callbacks and processing responses. Overrides FormSubmitterInterface:: |
|
FormBuilder:: |
protected | function | Detects if an element triggered the form submission via Ajax. | |
FormBuilder:: |
public | function |
Executes custom submission handlers for a given form. Overrides FormSubmitterInterface:: |
|
FormBuilder:: |
public | function |
Executes custom validation handlers for a given form. Overrides FormValidatorInterface:: |
|
FormBuilder:: |
public | function |
Fetches a form from the cache. Overrides FormCacheInterface:: |
|
FormBuilder:: |
protected | function | Wraps file_upload_max_size(). | |
FormBuilder:: |
public | function |
Gets a renderable form array. Overrides FormBuilderInterface:: |
|
FormBuilder:: |
public | function |
Determines the ID of a form. Overrides FormBuilderInterface:: |
|
FormBuilder:: |
protected | function | Adds the #name and #value properties of an input element before rendering. | |
FormBuilder:: |
public | function |
Prepares a structured form array. Overrides FormBuilder:: |
|
FormBuilder:: |
public | function |
Processes a form submission. Overrides FormBuilderInterface:: |
|
FormBuilder:: |
public | function |
Constructs a new $form from the information in $form_state. Overrides FormBuilderInterface:: |
|
FormBuilder:: |
public | function |
Redirects the user to a URL after a form has been processed. Overrides FormSubmitterInterface:: |
|
FormBuilder:: |
public | function | Renders the form CSRF token. It's a #lazy_builder callback. | |
FormBuilder:: |
public | function |
Renders a form action URL. It's a #lazy_builder callback. Overrides FormBuilder:: |
|
FormBuilder:: |
public | function |
Retrieves the structured array that defines a given form. Overrides FormBuilderInterface:: |
|
FormBuilder:: |
public | function |
Stores a form in the cache. Overrides FormCacheInterface:: |
|
FormBuilder:: |
public | function |
Sets a form_token error on the given form state. Overrides FormValidatorInterface:: |
|
FormBuilder:: |
public | function |
Retrieves, populates, and processes a form. Overrides FormBuilderInterface:: |
|
FormBuilder:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
|
FormBuilder:: |
public | function |
Validates user-submitted form data in the $form_state. Overrides FormValidatorInterface:: |
|
FormBuilder:: |
protected | function | Helper function to normalize the different callable formats. | |
FormBuilder:: |
public | function | Constructs a new FormBuilder. | |
FormBuilderInterface:: |
constant | Request key for AJAX forms that submit to the form's original route. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. |