class LayoutBuilderUpdateBlockForm in Panopoly 8.2
Enhances the update block form with live preview.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\layout_builder\Form\ConfigureBlockFormBase implements BaseFormIdInterface uses AjaxFormHelperTrait, ContextAwarePluginAssignmentTrait, LayoutBuilderContextTrait, LayoutRebuildTrait
- class \Drupal\layout_builder\Form\UpdateBlockForm uses LayoutBuilderHighlightTrait
- class \Drupal\panopoly_magic\Form\LayoutBuilderUpdateBlockForm
- class \Drupal\layout_builder\Form\UpdateBlockForm uses LayoutBuilderHighlightTrait
- class \Drupal\layout_builder\Form\ConfigureBlockFormBase implements BaseFormIdInterface uses AjaxFormHelperTrait, ContextAwarePluginAssignmentTrait, LayoutBuilderContextTrait, LayoutRebuildTrait
Expanded class hierarchy of LayoutBuilderUpdateBlockForm
1 file declares its use of LayoutBuilderUpdateBlockForm
- LayoutBuilderAddBlockController.php in modules/
panopoly/ panopoly_magic/ src/ Controller/ LayoutBuilderAddBlockController.php
File
- modules/
panopoly/ panopoly_magic/ src/ Form/ LayoutBuilderUpdateBlockForm.php, line 13
Namespace
Drupal\panopoly_magic\FormView source
class LayoutBuilderUpdateBlockForm extends UpdateBlockForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $uuid = NULL) {
$form = parent::buildForm($form, $form_state, $section_storage, $delta, $region, $uuid);
// Add a preview and cancel buttons.
if ($this
->isAjax()) {
$plugin_id = $section_storage
->getSection($delta)
->getComponent($uuid)
->getPluginId();
list($plugin_base_id, ) = explode(':', $plugin_id);
if ($plugin_base_id !== 'block_content') {
$form['actions']['preview'] = [
'#type' => 'submit',
'#value' => $this
->t('Preview'),
'#attributes' => [
'class' => [
'panopoly-magic-live-preview',
],
],
'#ajax' => [
'callback' => '::ajaxSubmit',
'disable-refocus' => TRUE,
],
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('Cancel'),
'#ajax' => [
'callback' => '::ajaxSubmit',
],
];
// Attach preview library.
$form['#attached']['library'][] = 'panopoly_magic/preview';
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Suppress form validation errors when Preview is clicked, allowing partial
// previews and removing error messages when a block has multiple required
// fields.
// Supporess form validation errors when Cancel is clicked, allowing the
// dialog to be dismissed discarding changes if there are validation errors.
$submit_button_name = end($form_state
->getTriggeringElement()['#parents']);
if ($submit_button_name == 'preview' || $submit_button_name == 'cancel') {
// Suppress all future validation errors from parent::validateForm().
$form_state
->setLimitValidationErrors([]);
// Clear any existing validation errors from the Field API.
$form_state
->clearErrors();
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Handle preview mode.
if ($form_state
->getValue('op') == $form['actions']['preview']['#value']) {
// Call the plugin submit handler.
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
$this
->getPluginForm($this->block)
->submitConfigurationForm($form, $subform_state);
// Update the component configuration.
$configuration = array_merge($this->block
->getConfiguration(), $form_state
->getValue('settings'));
$section = $this->sectionStorage
->getSection($this->delta);
$section
->getComponent($this->uuid)
->setConfiguration($configuration);
// We want to preview so rebuild the layout only.
// Do not update the temp layout storage.
// Preview config can hence be discarded and are not saved.
return $this
->rebuildLayout($this->sectionStorage);
}
elseif ($form_state
->getValue('op') == $form['actions']['cancel']['#value']) {
// Pull the last configuration from the temp layout storage and rebuild
// the layout.
$this->sectionStorage = $this->layoutTempstoreRepository
->get($this->sectionStorage);
return $this
->rebuildLayout($this->sectionStorage);
}
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
// When in preview mode return the rebuilt layout.
if ($form_state
->getValue('op') == $form['actions']['preview']['#value']) {
return $this
->rebuildLayout($this->sectionStorage);
}
return parent::successfulAjaxSubmit($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function submitLabel() {
return $this
->t('Save');
}
}
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. | |
ConfigureBlockFormBase:: |
protected | property | The plugin being configured. | |
ConfigureBlockFormBase:: |
protected | property | The block manager. | |
ConfigureBlockFormBase:: |
protected | property | The field delta. | |
ConfigureBlockFormBase:: |
protected | property | The layout tempstore repository. | |
ConfigureBlockFormBase:: |
protected | property | The plugin form manager. | |
ConfigureBlockFormBase:: |
protected | property | The current region. | |
ConfigureBlockFormBase:: |
protected | property | The section storage. | |
ConfigureBlockFormBase:: |
protected | property | The UUID of the component. | |
ConfigureBlockFormBase:: |
protected | property | The UUID generator. | |
ConfigureBlockFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ConfigureBlockFormBase:: |
public | function | Builds the form for the block. | |
ConfigureBlockFormBase:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
|
ConfigureBlockFormBase:: |
public | function | Retrieves the current component being edited by the form. | |
ConfigureBlockFormBase:: |
public | function | Retrieves the current layout section being edited by the form. | |
ConfigureBlockFormBase:: |
protected | function | Retrieves the plugin form for a given block. | |
ConfigureBlockFormBase:: |
public | function | Retrieves the section storage object. | |
ConfigureBlockFormBase:: |
public | function | Constructs a new block form. | |
ContextAwarePluginAssignmentTrait:: |
protected | function | Builds a form element for assigning a context to a given slot. | |
ContextAwarePluginAssignmentTrait:: |
protected | function | Wraps the context handler. | |
ContextAwarePluginAssignmentTrait:: |
abstract protected | function | Ensures the t() method is available. | |
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 | 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. | |
LayoutBuilderContextTrait:: |
protected | property | The context repository. | |
LayoutBuilderContextTrait:: |
protected | function | Gets the context repository service. | |
LayoutBuilderContextTrait:: |
protected | function | Provides all available contexts, both global and section_storage-specific. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderUpdateBlockForm:: |
public | function |
Builds the block form. Overrides UpdateBlockForm:: |
|
LayoutBuilderUpdateBlockForm:: |
public | function |
Form submission handler. Overrides ConfigureBlockFormBase:: |
|
LayoutBuilderUpdateBlockForm:: |
protected | function |
Returns the label for the submit button. Overrides UpdateBlockForm:: |
|
LayoutBuilderUpdateBlockForm:: |
protected | function |
Allows the form to respond to a successful AJAX submission. Overrides ConfigureBlockFormBase:: |
|
LayoutBuilderUpdateBlockForm:: |
public | function |
Form validation handler. Overrides ConfigureBlockFormBase:: |
|
LayoutRebuildTrait:: |
protected | function | Rebuilds the layout. | |
LayoutRebuildTrait:: |
protected | function | Rebuilds the layout. | |
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 |
UpdateBlockForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
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. |