Skip to content
Snippets Groups Projects
Commit 41e892b9 authored by Justin Toupin's avatar Justin Toupin Committed by Justin Toupin
Browse files

Issue #3229555 by pookmish, justin2pin, John Pitcairn, Anybody: Add Paragraph...

Issue #3229555 by pookmish, justin2pin, John Pitcairn, Anybody: Add Paragraph Behaviors Form Support
parent 685d5b91
No related branches found
No related tags found
3 merge requests!103Issue #3295875: Add a new dedicated permission for Layout paragraphs configurations,!27Add default width and height values for modals.,!25Provide layout info in hook_preprocess_paragraph
show_paragraph_labels: 0
show_layout_labels: 0
\ No newline at end of file
show_layout_labels: 0
paragraph_behaviors_label: 'Behaviors'
paragraph_behaviors_position: -99
\ No newline at end of file
......@@ -10,6 +10,14 @@ layout_paragraphs.settings:
type: integer
label: 'Show Layout Labels'
description: 'This option allows to show the Paragraphs Layout Label of each Item added in LP widget Sections/Layouts'
paragraph_behaviors_label:
type: string
label: 'Paragraph behaviors label'
descripton: 'The paragraph behaviors form fieldset label'
paragraph_behaviors_position:
type: integer
label: 'Paragraph behaviors fieldset position'
description: 'Wheter to render the paragraph behaviors at the top or bottom of paragraph edit forms'
layout_paragraphs.modal_settings:
type: config_object
......@@ -27,7 +35,7 @@ layout_paragraphs.modal_settings:
type: boolean
label: 'Modal autoresize'
description: 'If checked modal forms will automatically resize.'
field.formatter.settings.layout_paragraphs:
type: mapping
label: 'Layout Paragraphs format settings'
......
......@@ -138,6 +138,7 @@ abstract class ComponentFormBase extends FormBase {
$display = EntityFormDisplay::collectRenderDisplay($this->paragraph, 'default');
$display->buildForm($this->paragraph, $form, $form_state);
$this->paragraphType = $this->paragraph->getParagraphType();
$lp_config = $this->config('layout_paragraphs.settings');
$form += [
'#title' => $this->formTitle(),
......@@ -148,7 +149,7 @@ abstract class ComponentFormBase extends FormBase {
[$this, 'afterBuild'],
],
'actions' => [
'#weight' => 20,
'#weight' => 100,
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
......@@ -187,6 +188,17 @@ abstract class ComponentFormBase extends FormBase {
];
}
if (count($this->getEnabledBehaviorPlugins())) {
$form['behavior_plugins'] = [
'#weight' => $lp_config->get('paragraph_behaviors_position') ?? -99,
'#type' => 'details',
'#title' => $lp_config->get('paragraph_behaviors_label') ?? $this->t('Behaviors'),
'#process' => [
[$this, 'behaviorPluginsForm'],
],
];
}
// Support for Field Group module based on Paragraphs module.
// @todo Remove as part of https://www.drupal.org/node/2640056
if ($this->moduleHandler->moduleExists('field_group')) {
......@@ -222,6 +234,16 @@ abstract class ComponentFormBase extends FormBase {
* The form state object.
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// First validate paragraph behavior forms.
foreach ($this->getEnabledBehaviorPlugins() as $behavior_id => $behavior_plugin) {
if (!empty($form['behavior_plugins'][$behavior_id])) {
$subform_state = SubformState::createForSubform($form['behavior_plugins'][$behavior_id], $form_state->getCompleteForm(), $form_state);
$behavior_plugin->validateBehaviorForm($this->paragraph, $form['behavior_plugins'][$behavior_id], $subform_state);
}
}
// Validate the paragraph with submitted form values.
$paragraph = $this->buildParagraphComponent($form, $form_state);
$violations = $paragraph->validate();
// Remove violations of inaccessible fields.
......@@ -234,6 +256,7 @@ abstract class ComponentFormBase extends FormBase {
$form_state->setErrorByName('', $violation->getMessage());
}
$form['#display']->flagWidgetsErrorsFromViolations($violations, $form, $form_state);
}
/**
......@@ -264,6 +287,7 @@ abstract class ComponentFormBase extends FormBase {
$display = $form['#display'];
$paragraph = clone $this->paragraph;
$paragraph->getAllBehaviorSettings();
$paragraphs_type = $paragraph->getParagraphType();
if ($paragraphs_type->hasEnabledBehaviorPlugin('layout_paragraphs')) {
......@@ -272,6 +296,11 @@ abstract class ComponentFormBase extends FormBase {
$layout_paragraphs_plugin->submitBehaviorForm($paragraph, $form['layout_paragraphs'], $subform_state);
}
foreach ($this->getEnabledBehaviorPlugins() as $behavior_id => $behavior_plugin) {
$subform_state = SubformState::createForSubform($form['behavior_plugins'][$behavior_id], $form, $form_state);
$behavior_plugin->submitBehaviorForm($paragraph, $form['behavior_plugins'][$behavior_id], $subform_state);
}
$paragraph->setNeedsSave(TRUE);
$display->extractFormValues($paragraph, $form, $form_state);
return $paragraph;
......@@ -339,6 +368,42 @@ abstract class ComponentFormBase extends FormBase {
return $element;
}
/**
* Form #process callback.
*
* Attaches the behavior plugin forms.
*
* @param array $element
* The form element.
* @param Drupal\Core\Form\FormStateInterface $form_state
* The form state.
* @param array $form
* The complete form array.
*
* @return array
* The processed element.
*/
public function behaviorPluginsForm(array $element, FormStateInterface $form_state, array &$form) {
$element['#type'] = 'container';
$element['#attributes']['class'][] = 'lpb-behavior-plugins';
foreach ($this->getEnabledBehaviorPlugins() as $behavior_id => $behavior_plugin) {
$element[$behavior_id] = [
'#parents' => array_merge($element['#parents'], [$behavior_id]),
'#type' => 'container',
'#attributes' => [
'class' => ['lpb-behavior-plugins__' . Html::cleanCssIdentifier($behavior_id)],
],
];
$subform_state = SubformState::createForSubform($element[$behavior_id], $form, $form_state);
if ($behavior_form = $behavior_plugin->buildBehaviorForm($this->paragraph, $element[$behavior_id], $subform_state)) {
$element[$behavior_id] = $behavior_form;
}
}
return $element;
}
/**
* Ajax form callback.
*
......@@ -486,4 +551,25 @@ abstract class ComponentFormBase extends FormBase {
}
}
/**
* Returns an array of enabled behavior plugins excluding Layout Paragraphs.
*
* The Layout Paragraphs behavior plugin form is handled separately.
*
* @return array
* An array of enabled plugins.
*/
protected function getEnabledBehaviorPlugins() {
if ($this->currentUser()->hasPermission('edit behavior plugin settings')) {
return array_filter(
$this->paragraphType->getEnabledBehaviorPlugins(),
function ($key) {
return $key != 'layout_paragraphs';
},
ARRAY_FILTER_USE_KEY
);
}
return [];
}
}
......@@ -84,6 +84,22 @@ class LayoutParagraphsSettingsForm extends ConfigFormBase {
'#default_value' => $lp_config->get('show_layout_labels'),
];
$form['paragraph_behaviors_label'] = [
'#type' => 'textfield',
'#title' => $this->t('Paragraph Behaviors Fieldset Label'),
'#default_value' => $lp_config->get('paragraph_behaviors_label'),
];
$form['paragraph_behaviors_position'] = [
'#type' => 'radios',
'#title' => $this->t('Paragraph Behaviors Fieldset Position'),
'#options' => [
'-99' => $this->t('Top of paragraph edit form'),
'99' => $this->t('Bottom of paragraph edit form'),
],
'#default_value' => $lp_config->get('paragraph_behaviors_position'),
];
return parent::buildForm($form, $form_state);
}
......@@ -94,6 +110,8 @@ class LayoutParagraphsSettingsForm extends ConfigFormBase {
$lp_config = $this->configFactory()->getEditable('layout_paragraphs.settings');
$lp_config->set('show_paragraph_labels', $form_state->getValue('show_paragraph_labels'));
$lp_config->set('show_layout_labels', $form_state->getValue('show_layout_labels'));
$lp_config->set('paragraph_behaviors_label', $form_state->getValue('paragraph_behaviors_label'));
$lp_config->set('paragraph_behaviors_position', $form_state->getValue('paragraph_behaviors_position'));
$lp_config->save();
// Confirmation on form submission.
$this->messenger()->addMessage($this->t('The Layout Paragraphs settings have been saved.'));
......
......@@ -2,22 +2,23 @@
namespace Drupal\layout_paragraphs\Plugin\paragraphs\Behavior;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Layout\LayoutInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Drupal\layout_paragraphs\LayoutParagraphsSection;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\layout_paragraphs\LayoutParagraphsRendererService;
use Drupal\Core\Layout\LayoutInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a way to define grid based layouts.
......@@ -156,6 +157,30 @@ class LayoutParagraphsBehavior extends ParagraphsBehaviorBase {
return $form;
}
/**
* {@inheritdoc}
*/
public function validateBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$plugin_instance = $this->layoutPluginManager->createInstance($form_state->getValue('layout'), $form_state->getValue('config'));
if ($plugin_form = $this->getLayoutPluginForm($plugin_instance)) {
$plugin_form->validateConfigurationForm($form, $form_state);
}
}
/**
* {@inheritdoc}
*/
public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
$filtered_values = $this->filterBehaviorFormSubmitValues($paragraph, $form, $form_state);
$plugin_instance = $this->layoutPluginManager->createInstance($form_state->getValue('layout'), $form_state->getValue('config'));
if ($plugin_form = $this->getLayoutPluginForm($plugin_instance)) {
$subform_state = SubformState::createForSubform($form['config'], $form, $form_state);
$plugin_form->submitConfigurationForm($form['config'], $subform_state);
$filtered_values['config'] = $plugin_form->getConfiguration();
}
$paragraph->setBehaviorSettings($this->getPluginId(), $filtered_values);
}
/**
* Ajax callback - returns the updated layout options form.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment