View source
<?php
namespace Drupal\layout_paragraphs\Plugin\Field\FieldWidget;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\layout_paragraphs\LayoutParagraphsLayout;
use Drupal\layout_paragraphs\LayoutParagraphsLayoutTempstoreRepository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Layout\LayoutPluginManager;
use Drupal\Core\Form\FormBuilder;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPluginInterface {
protected $tempstore;
protected $entityTypeManager;
protected $layoutPluginManager;
protected $layoutParagraphsLayout;
protected $storageKey;
protected $formBuilder;
protected $entityDisplayRepository;
protected $config;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, LayoutParagraphsLayoutTempstoreRepository $tempstore, EntityTypeManagerInterface $entity_type_manager, LayoutPluginManager $layout_plugin_manager, FormBuilder $form_builder, EntityDisplayRepositoryInterface $entity_display_repository, ConfigFactoryInterface $config_factory) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->tempstore = $tempstore;
$this->entityTypeManager = $entity_type_manager;
$this->layoutPluginManager = $layout_plugin_manager;
$this->formBuilder = $form_builder;
$this->entityDisplayRepository = $entity_display_repository;
$this->config = $config_factory
->get('layout_paragraphs.settings');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('layout_paragraphs.tempstore_repository'), $container
->get('entity_type.manager'), $container
->get('plugin.manager.core.layout'), $container
->get('form_builder'), $container
->get('entity_display.repository'), $container
->get('config.factory'));
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$this->layoutParagraphsLayout = new LayoutParagraphsLayout($items, $this
->getSettings());
if (!$form_state
->getUserInput()) {
$this->tempstore
->set($this->layoutParagraphsLayout);
}
else {
$this->layoutParagraphsLayout = $this->tempstore
->get($this->layoutParagraphsLayout);
}
$element += [
'#type' => 'fieldset',
'#title' => $this->fieldDefinition
->getLabel(),
'layout_paragraphs_builder' => [
'#type' => 'layout_paragraphs_builder',
'#layout_paragraphs_layout' => $this->layoutParagraphsLayout,
],
];
if ($source = $form_state
->get([
'content_translation',
'source',
])) {
$element['layout_paragraphs_builder']['#source_langcode'] = $source
->getId();
}
return $element;
}
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition
->getName();
$path = array_merge($form['#parents'], [
$field_name,
]);
$layout_paragraphs_layout = $this->tempstore
->get(new LayoutParagraphsLayout($items));
$values = [];
foreach ($layout_paragraphs_layout
->getParagraphsReferenceField() as $item) {
if ($item->entity) {
$entity = $item->entity;
$entity
->setNeedsSave(TRUE);
$values[] = [
'entity' => $entity,
'target_id' => $entity
->id(),
'target_revision_id' => $entity
->getRevisionId(),
];
}
}
$form_state
->setValue($path, $values);
return parent::extractFormValues($items, $form, $form_state);
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$entity_type_id = $this
->getFieldSetting('target_type');
$element = parent::settingsForm($form, $form_state);
$element['preview_view_mode'] = [
'#type' => 'select',
'#title' => $this
->t('Preview view mode'),
'#default_value' => $this
->getSetting('preview_view_mode'),
'#options' => $this->entityDisplayRepository
->getViewModeOptions($entity_type_id),
'#description' => $this
->t('View mode for the referenced entity preview on the edit form. Automatically falls back to "default", if it is not enabled in the referenced entity type displays.'),
];
$element['nesting_depth'] = [
'#type' => 'select',
'#title' => $this
->t('Maximum nesting depth'),
'#options' => range(0, 10),
'#default_value' => $this
->getSetting('nesting_depth'),
'#description' => $this
->t('Choosing 0 will prevent nesting layouts within other layouts.'),
];
$element['require_layouts'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Require paragraphs to be added inside a layout'),
'#default_value' => $this
->getSetting('require_layouts'),
];
$element['empty_message'] = [
'#type' => 'textfield',
'#title' => $this
->t('Placeholder message to display when field is empty'),
'#default_value' => $this
->getSetting('empty_message'),
];
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = $this
->t('Preview view mode: @preview_view_mode', [
'@preview_view_mode' => $this
->getSetting('preview_view_mode'),
]);
$summary[] = $this
->t('Maximum nesting depth: @max_depth', [
'@max_depth' => $this
->getSetting('nesting_depth'),
]);
if ($this
->getSetting('require_layouts')) {
$summary[] = $this
->t('Paragraphs <b>must be</b> added within layouts.');
}
else {
$summary[] = $this
->t('Layouts are optional.');
}
$summary[] = $this
->t('Maximum nesting depth: @max_depth', [
'@max_depth' => $this
->getSetting('nesting_depth'),
]);
return $summary;
}
public static function defaultSettings() {
$defaults = parent::defaultSettings();
$defaults += [
'empty_message' => '',
'preview_view_mode' => 'default',
'nesting_depth' => 0,
'require_layouts' => 0,
];
return $defaults;
}
}