Paragraph Lineage Module
The Paragraph Lineage module provides functionality to display a paragraph entity along with its ancestors in a hierarchical view.
Features
- Render a paragraph entity using the default view mode.
- Display the lineage of a paragraph entity.
- Retrieve available view modes for a paragraph bundle.
Installation
- Place the
paragraph_lineage
module in theweb/modules/custom
directory. - Enable the module using Drush or the Drupal admin interface.
drush en paragraph_lineage
Usage
Rendering a Paragraph with Lineage
The ParagraphLineageViewController class is responsible for rendering a paragraph entity along with its lineage. The build method loads the paragraph from the route, retrieves its lineage, and renders the paragraph using the default view mode.
Getting Available View Modes
The ParagraphViewModes class provides a method to retrieve available view modes for a specific paragraph bundle using the entity_display.repository service.
Code Examples
Rendering a Paragraph
public function build(): array {
// Get the paragraph from the route.
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $this->route_match->getParameter('paragraph');
if (!$paragraph instanceof ParagraphInterface) {
throw new \InvalidArgumentException('No paragraph found in the route.');
}
$lineage = [];
$this->getLineage($lineage, $paragraph);
// Render the paragraph using the default view mode.
$rendered_paragraph = $this->entity_type_manager
->getViewBuilder('paragraph')
->view($paragraph, 'default');
return [
'#theme' => 'paragraph_lineage',
'#paragraph' => [
'type' => $paragraph->getEntityTypeId(),
'bundle' => $paragraph->bundle(),
'content' => $rendered_paragraph,
],
'#lineage' => $lineage,
];
}
Getting Available View Modes
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParagraphViewModes {
protected $entityDisplayRepository;
public function __construct(EntityDisplayRepositoryInterface $entity_display_repository) {
$this->entityDisplayRepository = $entity_display_repository;
}
public function getViewModes(string $bundle): array {
return $this->entityDisplayRepository->getViewModes('paragraph', $bundle);
}
public static function create(ContainerInterface $container): ParagraphViewModes {
return new static(
$container->get('entity_display.repository')
);
}
}