You are here

class LayoutParagraphsComponent in Layout Paragraphs 2.0.x

Provides a domain object for a single Layout Paragraphs Component.

A Layout Paragraphs Component wraps a paragraph entity and provides APIs for working with the paragraph in the context of layouts.

See also:

Hierarchy

Expanded class hierarchy of LayoutParagraphsComponent

2 files declare their use of LayoutParagraphsComponent
LayoutParagraphsBuilder.php in src/Element/LayoutParagraphsBuilder.php
LayoutParagraphsFormatter.php in src/Plugin/Field/FieldFormatter/LayoutParagraphsFormatter.php

File

src/LayoutParagraphsComponent.php, line 17

Namespace

Drupal\layout_paragraphs
View source
class LayoutParagraphsComponent {

  /**
   * The paragraph entity.
   *
   * @var Drupal\paragraphs\Entity\Paragraph
   */
  protected $paragraph;

  /**
   * Class constructor.
   *
   * @param Drupal\paragraphs\Entity\Paragraph $paragraph
   *   The paragraph entity.
   */
  public function __construct(Paragraph $paragraph) {
    $this->paragraph = $paragraph;
  }

  /**
   * Gets the region for the component.
   *
   * @return string
   *   The region.
   */
  public function getRegion() {
    return $this
      ->getSetting('region');
  }

  /**
   * Static wrapper for isLayout().
   *
   * @param Drupal\paragraphs\Entity\Paragraph $paragraph
   *   The paragraph to check if is layout.
   *
   * @return bool
   *   True if paragraph is a layout.
   */
  public static function isLayoutComponent(Paragraph $paragraph) {
    $instance = new static($paragraph);
    return $instance
      ->isLayout();
  }

  /**
   * Returns true if this component has a layout applied.
   *
   * @return bool
   *   True if is layout.
   */
  public function isLayout() {
    return !empty($this
      ->getSetting('layout'));
  }

  /**
   * Returns true if this component has a layout applied.
   *
   * @return bool
   *   True if is layout.
   */
  public function hasLayout() {
    return !empty($this
      ->getSetting('layout'));
  }

  /**
   * Returns true if disabled.
   *
   * @return bool
   *   True if disabled.
   */
  public function isDisabled() {
    return $this
      ->getSetting('region') == '_disabled';
  }

  /**
   * Static wrapper for isRoot().
   *
   * @param \Drupal\paragraphs\Entity\Paragraph $paragraph
   *   The paragraph entity.
   *
   * @return bool
   *   True if this item has no parent.
   */
  public static function isRootComponent(Paragraph $paragraph) {
    $component = new static($paragraph);
    return $component
      ->isRoot();
  }

  /**
   * A "root" component is rendered at the top level.
   *
   * @return bool
   *   True if component is a root element.
   */
  public function isRoot() {
    return !$this
      ->getParentUuid() && !$this
      ->isDisabled();
  }

  /**
   * Returns the parent component if one exists.
   *
   * @return Drupal\paragraphs\Entity\Paragraph|false
   *   The parent paragraph or false if doesn't exist.
   */
  public function getParentUuid() {
    return $this
      ->getSetting('parent_uuid');
  }

  /**
   * Returns the wrapped paragraph entity.
   *
   * @return \Drupal\paragraphs\Entity\Paragraph
   *   The paragraph entity.
   */
  public function getEntity() {
    return $this->paragraph;
  }

  /**
   * Returns a single layout paragraph setting.
   *
   * @param string $key
   *   The setting to return.
   *
   * @return mixed
   *   The setting value.
   */
  public function getSetting($key) {
    $settings = $this
      ->getSettings();
    return $settings[$key] ?? NULL;
  }

  /**
   * Returns the layout paragraph's behavior settings.
   *
   * @return array
   *   The settings array.
   */
  public function getSettings() {
    $behaviors_settings = $this->paragraph
      ->getAllBehaviorSettings();
    $layout_behavior_settings = $behaviors_settings['layout_paragraphs'] ?? [];
    $defaults = $this
      ->defaultSettings();
    return $layout_behavior_settings + $defaults;
  }

  /**
   * Sets the layout paragraph's behavior settings.
   *
   * @param array $settings
   *   The layout settings.
   */
  public function setSettings(array $settings) {
    $behaviors_settings = $this->paragraph
      ->getAllBehaviorSettings();
    $layout_behavior_settings = $behaviors_settings['layout_paragraphs'] ?? [];
    $layout_behavior_settings = array_merge($layout_behavior_settings, $settings);
    $this->paragraph
      ->setBehaviorSettings('layout_paragraphs', $layout_behavior_settings);
    $this->paragraph
      ->setNeedsSave(TRUE);
  }

  /**
   * Returns an array of default settings.
   *
   * @return array
   *   The default settings.
   */
  protected function defaultSettings() {
    return [
      'region' => '',
      'parent_uuid' => '',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayoutParagraphsComponent::$paragraph protected property The paragraph entity.
LayoutParagraphsComponent::defaultSettings protected function Returns an array of default settings. 1
LayoutParagraphsComponent::getEntity public function Returns the wrapped paragraph entity.
LayoutParagraphsComponent::getParentUuid public function Returns the parent component if one exists.
LayoutParagraphsComponent::getRegion public function Gets the region for the component.
LayoutParagraphsComponent::getSetting public function Returns a single layout paragraph setting.
LayoutParagraphsComponent::getSettings public function Returns the layout paragraph's behavior settings.
LayoutParagraphsComponent::hasLayout public function Returns true if this component has a layout applied.
LayoutParagraphsComponent::isDisabled public function Returns true if disabled.
LayoutParagraphsComponent::isLayout public function Returns true if this component has a layout applied.
LayoutParagraphsComponent::isLayoutComponent public static function Static wrapper for isLayout().
LayoutParagraphsComponent::isRoot public function A "root" component is rendered at the top level.
LayoutParagraphsComponent::isRootComponent public static function Static wrapper for isRoot().
LayoutParagraphsComponent::setSettings public function Sets the layout paragraph's behavior settings.
LayoutParagraphsComponent::__construct public function Class constructor. 1