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

Adds compatibility with Inline Entity Form.

parent 674b2865
No related branches found
No related tags found
No related merge requests found
name: 'Layout Paragraphs IEF Support'
type: module
description: 'Provides support for using Inline Entity Forms within Layout Paragraphs.'
core: 8.x
core_version_requirement: ^8 || ^9
package: 'Paragraphs'
dependencies:
- drupal:layout_paragraphs
- drupal:inline_entity_form
<?php
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\inline_entity_form\ElementSubmit;
use Drupal\inline_entity_form\WidgetSubmit;
use Drupal\inline_entity_form\Form\EntityInlineForm;
use Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex;
/**
* Implements hook_form_alter().
*/
function layout_paragraphs_ief_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Attach the IEF handlers only if the current form has an IEF widget.
$widget_state = $form_state->get('inline_entity_form');
if (!is_null($widget_state)) {
$layout_paragraphs_widgets = $form_state->get('layout_paragraph_widgets');
if (!empty($layout_paragraphs_widgets)) {
foreach ($layout_paragraphs_widgets as $layout_paragraphs_widget) {
$entity_form_path = array_merge($layout_paragraphs_widget, ['entity_form']);
$entity_form = NestedArray::getValue($form, $entity_form_path);
if (!empty($entity_form)) {
ElementSubmit::attach($entity_form, $form_state);
WidgetSubmit::attach($entity_form, $form_state);
NestedArray::setValue($form, $entity_form_path, $entity_form);
}
}
}
}
}
function _layout_paragraphs_ief_traverse_form($form) {
foreach ($form as $item) {
$test = '';
}
}
...@@ -771,6 +771,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -771,6 +771,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
]; ];
// Add layout_paragraphs_widget library. // Add layout_paragraphs_widget library.
$elements['#attached']['library'][] = 'layout_paragraphs/layout_paragraphs_widget'; $elements['#attached']['library'][] = 'layout_paragraphs/layout_paragraphs_widget';
$elements['#process'] = [[$this, 'registerLayoutParagraphsWidget']];
return $elements; return $elements;
} }
...@@ -787,6 +788,26 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -787,6 +788,26 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
return $elements; return $elements;
} }
/**
* Stores mapping to this widget in form_state.
*
* This allows other modules to more easily find, interact with or enhance
* layout paragraph widgets.
*
* @param array $elements
* The widget form elements.
* @param Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function registerLayoutParagraphsWidget(array $elements, FormStateInterface $form_state) {
$widgets = $form_state->get('layout_paragraph_widgets') ?? [];
$widgets[] = $elements['#array_parents'];
$form_state->set('layout_paragraph_widgets', $widgets);
return $elements;
}
/** /**
* Restructures $elements array into layout. * Restructures $elements array into layout.
* *
...@@ -1160,7 +1181,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -1160,7 +1181,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
'#weight' => 1000, '#weight' => 1000,
'#type' => 'actions', '#type' => 'actions',
'#attributes' => ['class' => ['layout-paragraphs-item-form-actions']], '#attributes' => ['class' => ['layout-paragraphs-item-form-actions']],
'save_item' => [ 'submit' => [
'#type' => 'submit', '#type' => 'submit',
'#name' => 'save', '#name' => 'save',
'#value' => $this->t('Save'), '#value' => $this->t('Save'),
......
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