View source
<?php
namespace Drupal\layout_paragraphs\Element;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Layout\LayoutPluginManager;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Access\AccessResultForbidden;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Template\Attribute;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\layout_paragraphs\LayoutParagraphsSection;
use Drupal\layout_paragraphs\LayoutParagraphsComponent;
use Drupal\layout_paragraphs\LayoutParagraphsLayoutTempstoreRepository;
use Drupal\layout_paragraphs\DialogHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LayoutParagraphsBuilder extends RenderElement implements ContainerFactoryPluginInterface {
use DialogHelperTrait;
protected $tempstore;
protected $entityTypeManager;
protected $layoutPluginManager;
protected $renderer;
protected $entityTypeBundleInfo;
protected $isTranslating;
protected $sourceLangcode;
protected $layoutParagraphsLayout;
protected $entityRepository;
protected $langcode;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LayoutParagraphsLayoutTempstoreRepository $tempstore_repository, EntityTypeManagerInterface $entity_type_manager, LayoutPluginManager $layout_plugin_manager, Renderer $renderer, EntityTypeBundleInfo $entity_type_bundle_info, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->tempstore = $tempstore_repository;
$this->entityTypeManager = $entity_type_manager;
$this->layoutPluginManager = $layout_plugin_manager;
$this->renderer = $renderer;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityRepository = $entity_repository;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('layout_paragraphs.tempstore_repository'), $container
->get('entity_type.manager'), $container
->get('plugin.manager.core.layout'), $container
->get('renderer'), $container
->get('entity_type.bundle.info'), $container
->get('entity.repository'));
}
public function getInfo() {
return [
'#layout_paragraphs_layout' => NULL,
'#uuid' => NULL,
'#source_langcode' => NULL,
'#theme' => 'layout_paragraphs_builder',
'#pre_render' => [
[
$this,
'preRender',
],
],
];
}
public function preRender($element) {
$this->layoutParagraphsLayout = $this->tempstore
->get($element['#layout_paragraphs_layout']);
$this->langcode = $this->entityRepository
->getTranslationFromContext($this->layoutParagraphsLayout
->getEntity())
->language()
->getId();
$this->sourceLangcode = $element['#source_langcode'];
$element_uuid = $element['#uuid'];
$preview_view_mode = $this->layoutParagraphsLayout
->getSetting('preview_view_mode', 'default');
$element['#components'] = [];
if ($this
->isTranslating()) {
$this
->initTranslations();
$element['#translation_warning'] = $this
->translationWarning();
}
foreach ($this->layoutParagraphsLayout
->getComponents() as $component) {
$element['#components'][$component
->getEntity()
->uuid()] = $this
->buildComponent($component, $preview_view_mode);
}
foreach ($this->layoutParagraphsLayout
->getComponents() as $component) {
$uuid = $component
->getEntity()
->uuid();
if ($component
->isLayout()) {
$section = $this->layoutParagraphsLayout
->getLayoutSection($component
->getEntity());
$layout_plugin_instance = $this
->layoutPluginInstance($section);
foreach (array_keys($element['#components'][$uuid]['regions']) as $region_name) {
foreach ($section
->getComponentsForRegion($region_name) as $child_component) {
$child_uuid = $child_component
->getEntity()
->uuid();
$element['#components'][$uuid]['regions'][$region_name][$child_uuid] =& $element['#components'][$child_uuid];
}
}
$element['#components'][$uuid]['regions'] = $layout_plugin_instance
->build($element['#components'][$uuid]['regions']);
$element['#components'][$uuid]['regions']['#weight'] = 1000;
}
}
if ($element_uuid) {
if (isset($element['#components'][$element_uuid])) {
return [
'build' => $element['#components'][$element_uuid],
];
}
}
$element['#attributes'] = [
'class' => [
'lp-builder',
'lp-builder-' . $this->layoutParagraphsLayout
->id(),
],
'data-lpb-id' => $this->layoutParagraphsLayout
->id(),
];
$element['#attached']['library'] = [
'layout_paragraphs/builder',
];
$element['#attached']['drupalSettings']['lpBuilder'][$this->layoutParagraphsLayout
->id()] = $this->layoutParagraphsLayout
->getSettings();
$element['#is_empty'] = $this->layoutParagraphsLayout
->isEmpty();
$element['#empty_message'] = $this->layoutParagraphsLayout
->getSetting('empty_message', $this
->t('Start adding content.'));
if ($this->layoutParagraphsLayout
->getSetting('require_layouts', FALSE)) {
$element['#insert_button'] = $this
->insertSectionButton([
'layout_paragraphs_layout' => $this->layoutParagraphsLayout
->id(),
]);
}
else {
$element['#insert_button'] = $this
->insertComponentButton([
'layout_paragraphs_layout' => $this->layoutParagraphsLayout
->id(),
]);
}
$element['#root_components'] = [];
foreach ($this->layoutParagraphsLayout
->getRootComponents() as $component) {
$uuid = $component
->getEntity()
->uuid();
$element['#root_components'][$uuid] =& $element['#components'][$uuid];
}
if (count($element['#root_components'])) {
$element['#attributes']['class'][] = 'has-components';
}
return $element;
}
protected function buildComponent(LayoutParagraphsComponent $component, $preview_view_mode = 'default') {
$entity = $component
->getEntity();
$view_builder = $this->entityTypeManager
->getViewBuilder($entity
->getEntityTypeId());
$build = $view_builder
->view($entity, $preview_view_mode, $entity
->language()
->getId());
$build['#post_render'] = [
[
$this,
'postRenderComponent',
],
];
$build['#attributes']['data-uuid'] = $entity
->uuid();
$build['#attributes']['data-type'] = $entity
->bundle();
$build['#attributes']['data-id'] = $entity
->id();
$build['#attributes']['class'][] = 'js-lpb-component';
if ($entity
->isNew()) {
$build['#attributes']['class'][] = 'is_new';
}
$url_params = [
'layout_paragraphs_layout' => $this->layoutParagraphsLayout
->id(),
];
$query_params = [
'sibling_uuid' => $entity
->uuid(),
];
if ($parent_uuid = $component
->getParentUuid()) {
$query_params['parent_uuid'] = $parent_uuid;
}
if ($region = $component
->getRegion()) {
$query_params['region'] = $region;
}
if ($this
->editAccess($entity)) {
$edit_attributes = new Attribute([
'href' => Url::fromRoute('layout_paragraphs.builder.edit_item', [
'layout_paragraphs_layout' => $this->layoutParagraphsLayout
->id(),
'component_uuid' => $entity
->uuid(),
])
->toString(),
'class' => [
'lpb-edit',
'use-ajax',
],
'data-dialog-type' => 'dialog',
'data-dialog-options' => Json::encode($this
->dialogSettings($this->layoutParagraphsLayout)),
]);
}
else {
$edit_attributes = [];
}
if ($this
->deleteAccess($entity)) {
$delete_attributes = new Attribute([
'href' => Url::fromRoute('layout_paragraphs.builder.delete_item', [
'layout_paragraphs_layout' => $this->layoutParagraphsLayout
->id(),
'component_uuid' => $entity
->uuid(),
])
->toString(),
'class' => [
'lpb-delete',
'use-ajax',
],
'data-dialog-type' => 'dialog',
'data-dialog-options' => Json::encode([
'modal' => TRUE,
'target' => $this
->dialogId($this->layoutParagraphsLayout),
]),
]);
}
else {
$delete_attributes = [];
}
$build['controls'] = [
'#theme' => 'layout_paragraphs_builder_controls',
'#attributes' => [
'class' => [
'lpb-controls',
],
],
'#label' => $entity
->getParagraphType()->label,
'#edit_attributes' => $edit_attributes,
'#delete_attributes' => $delete_attributes,
'#weight' => -10001,
];
if ($component
->isLayout()) {
$build['controls']['#attributes']['class'][] = 'is-layout';
}
if ($this
->createAccess()) {
if (!$component
->getParentUuid() && $this->layoutParagraphsLayout
->getSetting('require_layouts')) {
$build['insert_before'] = $this
->insertSectionButton($url_params, $query_params + [
'placement' => 'before',
], -10000, [
'before',
]);
$build['insert_after'] = $this
->insertSectionButton($url_params, $query_params + [
'placement' => 'after',
], 10000, [
'after',
]);
}
else {
$build['insert_before'] = $this
->insertComponentButton($url_params, $query_params + [
'placement' => 'before',
], -10000, [
'before',
]);
$build['insert_after'] = $this
->insertComponentButton($url_params, $query_params + [
'placement' => 'after',
], 10000, [
'after',
]);
}
}
if ($component
->isLayout()) {
$section = $this->layoutParagraphsLayout
->getLayoutSection($entity);
$layout_instance = $this
->layoutPluginInstance($section);
$region_names = $layout_instance
->getPluginDefinition()
->getRegionNames();
$build['#attributes']['class'][] = 'lpb-layout';
$build['#attributes']['data-layout'] = $section
->getLayoutId();
$build['#layout_plugin_instance'] = $layout_instance;
$build['regions'] = [];
foreach ($region_names as $region_name) {
$url_params = [
'layout_paragraphs_layout' => $this->layoutParagraphsLayout
->id(),
];
$query_params = [
'parent_uuid' => $entity
->uuid(),
'region' => $region_name,
];
$build['regions'][$region_name] = [
'#attributes' => [
'class' => [
'js-lpb-region',
],
'data-region' => $region_name,
'data-region-uuid' => $entity
->uuid() . '-' . $region_name,
],
];
if ($this
->createAccess()) {
$build['regions'][$region_name]['insert_button'] = $this
->insertComponentButton($url_params, $query_params, 10000, [
'center',
]);
}
}
}
return $build;
}
public function postRenderComponent($content, array $element) {
if (strpos($content, '<form') !== FALSE) {
$search = [
'<form',
'</form>',
'required="required"',
'name="form_token"',
'name="form_id"',
];
$replace = [
'<div',
'</div>',
'',
'',
'',
];
$content = str_replace($search, $replace, $content);
}
return $content;
}
protected function insertComponentButton(array $route_params = [], array $query_params = [], int $weight = 0, array $classes = []) {
return [
'#type' => 'link',
'#title' => Markup::create('<span class="visually-hidden">' . $this
->t('Choose component') . '</span>'),
'#weight' => $weight,
'#attributes' => [
'class' => array_merge([
'lpb-btn--add',
'use-ajax',
], $classes),
'data-dialog-type' => 'dialog',
'data-dialog-options' => Json::encode([
'target' => $this
->dialogId($this->layoutParagraphsLayout),
'modal' => TRUE,
]),
],
'#url' => Url::fromRoute('layout_paragraphs.builder.choose_component', $route_params, [
'query' => $query_params,
]),
];
}
protected function insertSectionButton(array $route_params = [], array $query_params = [], int $weight = 0, array $classes = []) {
return [
'#type' => 'link',
'#title' => Markup::create($this
->t('Add section')),
'#attributes' => [
'class' => array_merge([
'lpb-btn',
'use-ajax',
], $classes),
'data-dialog-type' => 'dialog',
'data-dialog-options' => Json::encode($this
->dialogSettings($this->layoutParagraphsLayout)),
],
'#url' => Url::fromRoute('layout_paragraphs.builder.choose_component', $route_params, [
'query' => $query_params,
]),
];
}
protected function dialogOptions() {
return [
'modal' => TRUE,
'target' => $this
->dialogId($this->layoutParagraphsLayout),
];
}
protected function translationWarning() {
if ($this
->isTranslating()) {
if ($this
->supportsAsymmetricTranslations()) {
return $this
->t('You are in translation mode. Changes will only affect the current language.');
}
else {
return $this
->t('You are in translation mode. You cannot add or remove items while translating. Reordering items will affect all languages.');
}
}
}
protected function layoutPluginInstance(LayoutParagraphsSection $section) {
$layout_id = $section
->getLayoutId();
$layout_config = $section
->getLayoutConfiguration();
$layout_instance = $this->layoutPluginManager
->createInstance($layout_id, $layout_config);
return $layout_instance;
}
protected function editAccess(ParagraphInterface $paragraph) {
return $paragraph
->access('update');
}
protected function deleteAccess(ParagraphInterface $paragraph) {
if ($this
->isTranslating() && !$this
->supportsAsymmetricTranslations()) {
$access = new AccessResultForbidden('Cannot delete paragraphs while in translation mode.');
return $access
->isAllowed();
}
return $paragraph
->access('delete');
}
protected function createAccess() {
$access = new AccessResultAllowed();
if ($this
->isTranslating() && !$this
->supportsAsymmetricTranslations()) {
$access = new AccessResultForbidden('Cannot add paragraphs while in translation mode.');
}
return $access
->isAllowed();
}
protected function isTranslating() {
if (is_null($this->isTranslating)) {
$this->isTranslating = FALSE;
$host = $this->layoutParagraphsLayout
->getEntity();
$default_langcode_key = $host
->getEntityType()
->getKey('default_langcode');
if ($host
->hasTranslation($this->langcode) && $host
->getTranslation($this->langcode)
->get($default_langcode_key)->value == 0) {
$this->isTranslating = TRUE;
}
}
return $this->isTranslating;
}
protected function supportsAsymmetricTranslations() {
return $this->layoutParagraphsLayout
->getParagraphsReferenceField()
->getFieldDefinition()
->isTranslatable();
}
protected function initTranslations() {
$items = $this->layoutParagraphsLayout
->getParagraphsReferenceField();
foreach ($items as $delta => $item) {
if (!empty($item->entity) && $item->entity instanceof ParagraphInterface) {
if (!$this
->isTranslating()) {
$langcode_key = $item->entity
->getEntityType()
->getKey('langcode');
if ($item->entity
->get($langcode_key)->value != $this->langcode) {
if ($item->entity
->hasTranslation($this->langcode)) {
$item->entity = $item->entity
->getTranslation($this->langcode);
}
else {
$item->entity
->set($langcode_key, $this->langcode);
}
}
}
else {
if (!$item->entity
->hasTranslation($this->langcode)) {
$entity_langcode = $item->entity
->language()
->getId();
$source_langcode = $this->sourceLangcode ?? $entity_langcode;
if ($item->entity
->hasTranslation($source_langcode)) {
$entity = $item->entity
->getTranslation($source_langcode);
}
if ($item->entity
->hasField('content_translation_source')) {
$item->entity
->addTranslation($this->langcode, $entity
->toArray());
$translation = $item->entity
->getTranslation($this->langcode);
$manager = \Drupal::service('content_translation.manager');
$manager
->getTranslationMetadata($translation)
->setSource($item->entity
->language()
->getId());
}
}
if ($item->entity
->hasField('content_translation_source')) {
$item->entity = $item->entity
->getTranslation($this->langcode);
}
}
$items[$delta]->entity = $item->entity;
}
}
$this->tempstore
->set($this->layoutParagraphsLayout);
}
}