class LayoutParagraphsRendererService in Layout Paragraphs 2.0.x
Class definition for a layout paragraphs service.
Hierarchy
- class \Drupal\layout_paragraphs\LayoutParagraphsRendererService
Expanded class hierarchy of LayoutParagraphsRendererService
1 file declares its use of LayoutParagraphsRendererService
- LayoutParagraphsBehavior.php in src/
Plugin/ paragraphs/ Behavior/ LayoutParagraphsBehavior.php
1 string reference to 'LayoutParagraphsRendererService'
1 service uses LayoutParagraphsRendererService
File
- src/
LayoutParagraphsRendererService.php, line 12
Namespace
Drupal\layout_paragraphsView source
class LayoutParagraphsRendererService {
/**
* The layout plugin manager service.
*
* @var Drupal\Core\Layout\LayoutPluginManagerInterface
*/
protected $layoutPluginManager;
/**
* The entity type manager service.
*
* @var Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* An array of parent entities.
*
* @var \Drupal\Core\Entity\Entity[]
*/
protected static $parentEntities;
/**
* Class constructor.
*
* @param Drupal\Core\Layout\LayoutPluginManagerInterface $layout_plugin_manager
* The layout plugin manager service.
* @param Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(LayoutPluginManagerInterface $layout_plugin_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->layoutPluginManager = $layout_plugin_manager;
$this->entityTypeManager = $entity_type_manager;
}
/**
* Renders a single Layout Paragraph Section for the provided paragraph.
*
* @param Drupal\paragraphs\Entity\Paragraph $paragraph
* The paragraph entity.
* @param string $view_mode
* The view mode.
*
* @return array
* The component render array.
*/
public function renderLayoutSection(array &$build, Paragraph $paragraph, string $view_mode = 'default') {
if (!LayoutParagraphsComponent::isLayoutComponent($paragraph)) {
// @Todo: consider throwing an exception if $paragraph does not have a layout applied.
return [];
}
if ($paragraph->_referringItem) {
$layout = new LayoutParagraphsLayout($paragraph->_referringItem
->getParent());
}
else {
$parent_entity = $this
->getParentEntity($paragraph);
$field_name = $paragraph
->get('parent_field_name')->value;
$layout = new LayoutParagraphsLayout($parent_entity->{$field_name});
}
$section = $layout
->getLayoutSection($paragraph);
return $this
->buildLayoutSection($section, $view_mode);
}
/**
* Build the render array for Layout Paragraph Section.
*
* @param LayoutParagraphsSection $section
* The layout paragraph section.
* @param string $view_mode
* The view mode.
*
* @return array
* The build array.
*/
public function buildLayoutSection(LayoutParagraphsSection $section, $view_mode = '') {
$view_builder = $this->entityTypeManager
->getViewBuilder('paragraph');
$layout = $this->layoutPluginManager
->createInstance($section
->getLayoutId(), $section
->getLayoutConfiguration());
// Map rendered paragraphs into their respective regions.
$regions = $layout
->getPluginDefinition()
->getRegions();
foreach (array_keys($regions) as $region) {
$regions[$region] = array_map(function ($component) use ($view_builder, $view_mode) {
$entity = $component
->getEntity();
$access = $entity
->access('view', NULL, TRUE);
if ($access
->isAllowed()) {
return $view_builder
->view($component
->getEntity(), $view_mode);
}
}, $section
->getComponentsForRegion($region));
}
return $layout
->build($regions);
}
/**
* Returns the parent entity for a given paragraph.
*
* @param \Drupal\paragraphs\Entity\Paragraph $paragraph
* The paragraph.
*
* @return \Drupal\Core\Entity\Entity
* The entity.
*/
protected function getParentEntity(Paragraph $paragraph) {
$type = $paragraph
->get('parent_type')->value;
$id = $paragraph
->get('parent_id')->value;
if (!isset(static::$parentEntities["{$type}:{$id}"])) {
static::$parentEntities["{$type}:{$id}"] = $paragraph
->getParentEntity();
}
return static::$parentEntities["{$type}:{$id}"];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LayoutParagraphsRendererService:: |
protected | property | The entity type manager service. | |
LayoutParagraphsRendererService:: |
protected | property | The layout plugin manager service. | |
LayoutParagraphsRendererService:: |
protected static | property | An array of parent entities. | |
LayoutParagraphsRendererService:: |
public | function | Build the render array for Layout Paragraph Section. | |
LayoutParagraphsRendererService:: |
protected | function | Returns the parent entity for a given paragraph. | |
LayoutParagraphsRendererService:: |
public | function | Renders a single Layout Paragraph Section for the provided paragraph. | |
LayoutParagraphsRendererService:: |
public | function | Class constructor. |