View source
<?php
namespace Drupal\panopoly_magic\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\layout_builder\Form\UpdateBlockForm;
use Drupal\layout_builder\SectionStorageInterface;
class LayoutBuilderUpdateBlockForm extends UpdateBlockForm {
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);
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',
],
];
$form['#attached']['library'][] = 'panopoly_magic/preview';
}
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$submit_button_name = end($form_state
->getTriggeringElement()['#parents']);
if ($submit_button_name == 'preview' || $submit_button_name == 'cancel') {
$form_state
->setLimitValidationErrors([]);
$form_state
->clearErrors();
}
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('op') == $form['actions']['preview']['#value']) {
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
$this
->getPluginForm($this->block)
->submitConfigurationForm($form, $subform_state);
$configuration = array_merge($this->block
->getConfiguration(), $form_state
->getValue('settings'));
$section = $this->sectionStorage
->getSection($this->delta);
$section
->getComponent($this->uuid)
->setConfiguration($configuration);
return $this
->rebuildLayout($this->sectionStorage);
}
elseif ($form_state
->getValue('op') == $form['actions']['cancel']['#value']) {
$this->sectionStorage = $this->layoutTempstoreRepository
->get($this->sectionStorage);
return $this
->rebuildLayout($this->sectionStorage);
}
parent::submitForm($form, $form_state);
}
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
if ($form_state
->getValue('op') == $form['actions']['preview']['#value']) {
return $this
->rebuildLayout($this->sectionStorage);
}
return parent::successfulAjaxSubmit($form, $form_state);
}
protected function submitLabel() {
return $this
->t('Save');
}
}