class BlockEntitySettingTrayForm in Drupal 8
Same name and namespace in other branches
- 9 core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php \Drupal\settings_tray\Block\BlockEntitySettingTrayForm
Provides form for block instance forms when used in the off-canvas dialog.
This form removes advanced sections of regular block form such as the visibility settings, machine ID and region.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\block\BlockForm uses DeprecatedServicePropertyTrait
- class \Drupal\settings_tray\Block\BlockEntitySettingTrayForm uses AjaxFormHelperTrait
- class \Drupal\block\BlockForm uses DeprecatedServicePropertyTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of BlockEntitySettingTrayForm
1 file declares its use of BlockEntitySettingTrayForm
- settings_tray.module in core/
modules/ settings_tray/ settings_tray.module - Allows configuring blocks and other configuration from the site front-end.
File
- core/
modules/ settings_tray/ src/ Block/ BlockEntitySettingTrayForm.php, line 24
Namespace
Drupal\settings_tray\BlockView source
class BlockEntitySettingTrayForm extends BlockForm {
use AjaxFormHelperTrait;
/**
* Provides a title callback to get the block's admin label.
*
* @param \Drupal\block\BlockInterface $block
* The block entity.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The title.
*/
public function title(BlockInterface $block) {
// @todo Wrap "Configure " in <span class="visually-hidden"></span> once
// https://www.drupal.org/node/2359901 is fixed.
return $this
->t('Configure @block', [
'@block' => $block
->getPlugin()
->getPluginDefinition()['admin_label'],
]);
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
// Create link to full block form.
$query = [];
if ($destination = $this
->getRequest()->query
->get('destination')) {
$query['destination'] = $destination;
}
$form['advanced_link'] = [
'#type' => 'link',
'#title' => $this
->t('Advanced block options'),
'#url' => $this->entity
->toUrl('edit-form', [
'query' => $query,
]),
'#weight' => 1000,
];
// Remove the ID and region elements.
unset($form['id'], $form['region'], $form['settings']['admin_label']);
if (isset($form['settings']['label_display']) && isset($form['settings']['label'])) {
// Only show the label input if the label will be shown on the page.
$form['settings']['label_display']['#weight'] = -100;
$form['settings']['label']['#states']['visible'] = [
':input[name="settings[label_display]"]' => [
'checked' => TRUE,
],
];
// Relabel to "Block title" because on the front-end this may be confused
// with page title.
$form['settings']['label']['#title'] = $this
->t("Block title");
$form['settings']['label_display']['#title'] = $this
->t("Display block title");
}
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Save @block', [
'@block' => $this->entity
->getPlugin()
->getPluginDefinition()['admin_label'],
]);
$actions['delete']['#access'] = FALSE;
return $actions;
}
/**
* {@inheritdoc}
*/
protected function buildVisibilityInterface(array $form, FormStateInterface $form_state) {
// Do not display the visibility.
return [];
}
/**
* {@inheritdoc}
*/
protected function validateVisibility(array $form, FormStateInterface $form_state) {
// Intentionally empty.
}
/**
* {@inheritdoc}
*/
protected function submitVisibility(array $form, FormStateInterface $form_state) {
// Intentionally empty.
}
/**
* {@inheritdoc}
*/
protected function getPluginForm(BlockPluginInterface $block) {
if ($block instanceof PluginWithFormsInterface) {
return $this->pluginFormFactory
->createInstance($block, 'settings_tray', 'configure');
}
return $block;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['actions']['submit']['#ajax'] = [
'callback' => '::ajaxSubmit',
];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
// static::ajaxSubmit() requires data-drupal-selector to be the same between
// the various Ajax requests. A bug in \Drupal\Core\Form\FormBuilder
// prevents that from happening unless $form['#id'] is also the same.
// Normally, #id is set to a unique HTML ID via Html::getUniqueId(), but
// here we bypass that in order to work around the data-drupal-selector bug.
// This is okay so long as we assume that this form only ever occurs once on
// a page.
// @todo Remove this workaround once https://www.drupal.org/node/2897377 is
// fixed.
$form['#id'] = Html::getId($form_state
->getBuildInfo()['form_id']);
return $form;
}
/**
* {@inheritdoc}
*/
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
if ($redirect_url = $this
->getRedirectUrl()) {
$command = new RedirectCommand($redirect_url
->setAbsolute()
->toString());
}
else {
// Settings Tray always provides a destination.
throw new \Exception("No destination provided by Settings Tray form");
}
$response = new AjaxResponse();
return $response
->addCommand($command);
}
/**
* Gets the form's redirect URL from 'destination' provide in the request.
*
* @return \Drupal\Core\Url|null
* The redirect URL or NULL if dialog should just be closed.
*/
protected function getRedirectUrl() {
// \Drupal\Core\Routing\RedirectDestination::get() cannot be used directly
// because it will use <current> if 'destination' is not in the query
// string.
if ($this
->getRequest()->query
->has('destination') && ($destination = $this
->getRedirectDestination()
->get())) {
return Url::fromUserInput('/' . $destination);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxFormHelperTrait:: |
public | function | Submit form dialog #ajax callback. | |
AjaxHelperTrait:: |
protected | function | Gets the wrapper format of the current request. | |
AjaxHelperTrait:: |
protected | function | Determines if the current request is via AJAX. | |
BlockEntitySettingTrayForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides BlockForm:: |
|
BlockEntitySettingTrayForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
BlockEntitySettingTrayForm:: |
protected | function |
Helper function for building the visibility UI form. Overrides BlockForm:: |
|
BlockEntitySettingTrayForm:: |
public | function |
Gets the actual form array to be built. Overrides BlockForm:: |
|
BlockEntitySettingTrayForm:: |
protected | function |
Retrieves the plugin form for a given block and operation. Overrides BlockForm:: |
|
BlockEntitySettingTrayForm:: |
protected | function | Gets the form's redirect URL from 'destination' provide in the request. | |
BlockEntitySettingTrayForm:: |
protected | function |
Helper function to independently submit the visibility UI. Overrides BlockForm:: |
|
BlockEntitySettingTrayForm:: |
protected | function |
Allows the form to respond to a successful AJAX submission. Overrides AjaxFormHelperTrait:: |
|
BlockEntitySettingTrayForm:: |
public | function | Provides a title callback to get the block's admin label. | |
BlockEntitySettingTrayForm:: |
protected | function |
Helper function to independently validate the visibility UI. Overrides BlockForm:: |
|
BlockForm:: |
protected | property | The context repository service. | |
BlockForm:: |
protected | property | ||
BlockForm:: |
protected | property |
The block entity. Overrides EntityForm:: |
|
BlockForm:: |
protected | property | The language manager service. | |
BlockForm:: |
protected | property | The condition plugin manager. | |
BlockForm:: |
protected | property | The plugin form manager. | |
BlockForm:: |
protected | property | The block storage. | |
BlockForm:: |
protected | property | The theme handler. | |
BlockForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
BlockForm:: |
public | function | Generates a unique machine name for a block. | |
BlockForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
BlockForm:: |
public | function | Handles switching the available regions based on the selected theme. | |
BlockForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
BlockForm:: |
public | function | Constructs a BlockForm 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 | |
DeprecatedServicePropertyTrait:: |
public | function | Allows to access deprecated/removed properties. | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityFormInterface:: |
41 |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
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. | |
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. |