Commit 5bce2e47 authored by Justin Toupin's avatar Justin Toupin
Browse files

Issue #3262792: Supporting nested reference fields using Layout Paragraphs

parent d47f4870
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -365,11 +365,12 @@ a.lpb-enable-button::before {
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

.lpb-formatter {
  position: relative;
}
.lpb-formatter:hover,
.lpb-formatter:focus-within {
  outline: 1px solid blue;
  position: relative;
}

.lpb-formatter:hover .lpb-enable,
+10 −0
Original line number Diff line number Diff line
@@ -2,6 +2,16 @@
  // Updates the "Close" button label when a layout is changed.
  Drupal.behaviors.layoutParagraphsBuilderForm = {
    attach: function attach(context) {
      console.log(context);
      // Prevent nested frontend editors from being activated at the same time.
      $('.lpb-enable__wrapper').removeClass('hidden');
      $('[data-lpb-form-id]').each((i, e) => {
        const p = $(e).parents('[data-lpb-id]').toArray().pop();
        const parent = p || e;
        $('.lpb-enable__wrapper', parent).addClass('hidden');
      });

      // Update the "Close" button to say "Cancel" when any changes are made.
      const events = [
        'lpb-component:insert.lpb',
        'lpb-component:update.lpb',
+7 −0
Original line number Diff line number Diff line
@@ -8,6 +8,13 @@
(function ($, Drupal) {
  Drupal.behaviors.layoutParagraphsBuilderForm = {
    attach: function attach(context) {
      console.log(context);
      $('.lpb-enable__wrapper').removeClass('hidden');
      $('[data-lpb-form-id]').each(function (i, e) {
        var p = $(e).parents('[data-lpb-id]').toArray().pop();
        var parent = p || e;
        $('.lpb-enable__wrapper', parent).addClass('hidden');
      });
      var events = ['lpb-component:insert.lpb', 'lpb-component:update.lpb', 'lpb-component:move.lpb', 'lpb-component:drop.lpb'].join(' ');
      $('[data-lpb-id]', context).once('lpb-builder-form').on(events, function (e) {
        $(e.currentTarget).closest('[data-lpb-form-id]').find('[data-drupal-selector="edit-close"]').val(Drupal.t('Cancel'));
+4 −4
Original line number Diff line number Diff line
@@ -23,15 +23,15 @@ layout_paragraphs.modal_settings:
    _permission: 'administer site configuration'

layout_paragraphs.builder.formatter:
  path: '/layout-paragraphs-builder/{layout_paragraphs_layout}/formatter'
  path: '/layout-paragraphs-builder/formatter/{entity_type}/{entity}/{field_name}/{view_mode}'
  defaults:
    _controller: '\Drupal\layout_paragraphs\Controller\LayoutParagraphsBuilderController::build'
  options:
    parameters:
      layout_paragraphs_layout:
        layout_paragraphs_layout_tempstore: TRUE
      entity:
        type: entity:{entity_type}
  requirements:
    _layout_paragraphs_builder_access: 'TRUE'
    _custom_access: '\Drupal\layout_paragraphs\Controller\LayoutParagraphsBuilderController::access'

layout_paragraphs.builder.choose_component:
  path: '/layout-paragraphs-builder/{layout_paragraphs_layout}/choose-component'
+96 −4
Original line number Diff line number Diff line
@@ -3,10 +3,16 @@
namespace Drupal\layout_paragraphs\Controller;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\layout_paragraphs\LayoutParagraphsLayout;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\layout_paragraphs\LayoutParagraphsLayoutTempstoreRepository;

/**
 * Class LayoutParagraphsBuilderController.
@@ -17,16 +23,63 @@ class LayoutParagraphsBuilderController extends ControllerBase {

  use AjaxHelperTrait;

  /**
   * The tempstore service.
   *
   * @var \Drupal\layout_paragraphs\LayoutParagraphsLayoutTempstoreRepository
   */
  protected $tempstore;

  /**
   * The entity display repository service.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

  /**
   * The Layout Paragraphs Builder Access service.
   *
   * @var \Drupal\layout_paragraphs\Access\LayoutParagraphsBuilderAccess
   */
  protected $layoutParagraphsBuilderAccess;

  /**
   * {@inheritDoc}
   */
  public function __construct(LayoutParagraphsLayoutTempstoreRepository $tempstore, EntityDisplayRepositoryInterface $entity_display_repository, $layout_paragraphs_builder_access) {
    $this->tempstore = $tempstore;
    $this->entityDisplayRepository = $entity_display_repository;
    $this->layoutParagraphsBuilderAccess = $layout_paragraphs_builder_access;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('layout_paragraphs.tempstore_repository'),
      $container->get('entity_display.repository'),
      $container->get('layout_paragraphs.builder_access')
    );
  }

  /**
   * Builds the layout paragraphs builder form.
   *
   * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout
   *   The layout paragraphs layout object.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The parent entity that contains a layout.
   * @param string $field_name
   *   The matching name of the paragraphs reference field.
   * @param string $view_mode
   *   The view mode being rendered.
   *
   * @return mixed
   *   An ajax response or the form.
   */
  public function build(LayoutParagraphsLayout $layout_paragraphs_layout) {
  public function build(ContentEntityInterface $entity, $field_name, $view_mode) {

    $layout_paragraphs_layout = $this->getLayoutParagraphsLayout($entity, $field_name, $view_mode);
    $form = $this->formBuilder()->getForm(
      '\Drupal\layout_paragraphs\Form\LayoutParagraphsBuilderForm',
      $layout_paragraphs_layout
@@ -39,4 +92,43 @@ class LayoutParagraphsBuilderController extends ControllerBase {
    return $form;
  }

  /**
   * Access check.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The user account.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The parent entity that contains a layout.
   * @param string $field_name
   *   The name of the reference field.
   * @param string $view_mode
   *   The view mode.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   The access result.
   */
  public function access(AccountInterface $account, ContentEntityInterface $entity, $field_name, $view_mode) {
    return $this->layoutParagraphsBuilderAccess->access($account, $this->getLayoutParagraphsLayout($entity, $field_name, $view_mode));
  }

  /**
   * Returns a layout paragraphs layout.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity.
   * @param string $field_name
   *   The reference field name.
   * @param string $view_mode
   *   The view mode.
   *
   * @return \Drupal\layout_paragraphs\LayoutParagraphsLayout
   *   The layout.
   */
  protected function getLayoutParagraphsLayout(ContentEntityInterface $entity, string $field_name, string $view_mode) {
    $render_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
    $renderer = $render_display->getRenderer($field_name);
    $layout_paragraphs_settings = $renderer->getSettings() + ['reference_field_view_mode' => $view_mode];
    return new LayoutParagraphsLayout($entity->{$field_name}, $layout_paragraphs_settings);
  }

}
Loading