You are here

trait LayoutParagraphsLayoutRefreshTrait in Layout Paragraphs 2.0.x

Trait for managing refresh state for layouts.

When a layout is changed in the layout builder, an event is dispatched to determine whether or not the entire layout builder ui should be refreshed.

Hierarchy

See also

\Drupal\layout_paragraphs\Event\LayoutParagraphsUpdateLayoutEvent

\Drupal\layout_paragraphs\EventSubscriber\LayoutParagraphsUpdateLayoutSubscriber

3 files declare their use of LayoutParagraphsLayoutRefreshTrait
ChooseComponentController.php in src/Controller/ChooseComponentController.php
ComponentFormBase.php in src/Form/ComponentFormBase.php
DeleteComponentForm.php in src/Form/DeleteComponentForm.php

File

src/LayoutParagraphsLayoutRefreshTrait.php, line 20

Namespace

Drupal\layout_paragraphs
View source
trait LayoutParagraphsLayoutRefreshTrait {

  /**
   * The layout paragraphs layout object.
   *
   * @var \Drupal\layout_paragraphs\LayoutParagraphsLayout
   */
  protected $layoutParagraphsLayout;

  /**
   * The event dispatcher service.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * The original paragraphs layout object.
   *
   * @var \Drupal\layout_paragraphs\LayoutParagraphsLayout
   */
  protected $originalLayoutParagraphsLayout;

  /**
   * Setter for layoutParagraphsLayout property.
   *
   * Also creates an original copy to track changes between original
   * and updated layouts.
   *
   * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout
   *   The layout paragraphs layout object.
   *
   * @return $this
   */
  protected function setLayoutParagraphsLayout(LayoutParagraphsLayout $layout_paragraphs_layout) {
    $this->layoutParagraphsLayout = $layout_paragraphs_layout;
    $reference_field = clone $this->layoutParagraphsLayout
      ->getParagraphsReferenceField();
    $settings = $this->layoutParagraphsLayout
      ->getSettings();
    $this->originalLayoutParagraphsLayout = new LayoutParagraphsLayout($reference_field, $settings);
    return $this;
  }

  /**
   * Decorates an ajax response with a command to refresh an entire layout.
   *
   * @param \Drupal\Core\Ajax\AjaxResponse $response
   *   The ajax response to decorate.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The ajax response.
   */
  protected function refreshLayout(AjaxResponse $response) {
    $layout = $this
      ->renderLayout();
    $dom_selector = '[data-lpb-id="' . $this->layoutParagraphsLayout
      ->id() . '"]';
    $response
      ->addCommand(new ReplaceCommand($dom_selector, $layout));
    return $response;
  }

  /**
   * Renders the layout builder UI render array.
   *
   * @return array
   *   The layout builder render array.
   */
  protected function renderLayout() {
    return [
      '#type' => 'layout_paragraphs_builder',
      '#layout_paragraphs_layout' => $this->layoutParagraphsLayout,
    ];
  }

  /**
   * Returns TRUE if the layout needs to be refreshed.
   *
   * @return bool
   *   Whether or not the layout needs to be refreshed.
   */
  protected function needsRefresh() {
    $event = new LayoutParagraphsUpdateLayoutEvent($this->originalLayoutParagraphsLayout, $this->layoutParagraphsLayout);
    $this
      ->eventDispatcher()
      ->dispatch(LayoutParagraphsUpdateLayoutEvent::EVENT_NAME, $event);
    return $event->needsRefresh;
  }

  /**
   * Returns the event dispatcher service.
   *
   * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
   *   The event dispatcher service.
   */
  protected function eventDispatcher() {
    if (!($this->eventDispatcher && $this->eventDispatcher instanceof EventDispatcherInterface)) {
      $this->eventDispatcher = \Drupal::service('event_dispatcher');
    }
    return $this->eventDispatcher;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayoutParagraphsLayoutRefreshTrait::$eventDispatcher protected property The event dispatcher service. 1
LayoutParagraphsLayoutRefreshTrait::$layoutParagraphsLayout protected property The layout paragraphs layout object.
LayoutParagraphsLayoutRefreshTrait::$originalLayoutParagraphsLayout protected property The original paragraphs layout object.
LayoutParagraphsLayoutRefreshTrait::eventDispatcher protected function Returns the event dispatcher service. 1
LayoutParagraphsLayoutRefreshTrait::needsRefresh protected function Returns TRUE if the layout needs to be refreshed.
LayoutParagraphsLayoutRefreshTrait::refreshLayout protected function Decorates an ajax response with a command to refresh an entire layout.
LayoutParagraphsLayoutRefreshTrait::renderLayout protected function Renders the layout builder UI render array.
LayoutParagraphsLayoutRefreshTrait::setLayoutParagraphsLayout protected function Setter for layoutParagraphsLayout property.