diff --git a/src/Controller/OverviewController.php b/src/Controller/OverviewController.php index b8e38e1..f45d004 100644 --- a/src/Controller/OverviewController.php +++ b/src/Controller/OverviewController.php @@ -3,6 +3,8 @@ namespace Drupal\paragraphs_collection\Controller; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\node\Entity\Node; use Drupal\paragraphs\Entity\Paragraph; use Drupal\paragraphs\Entity\ParagraphsType; use Drupal\paragraphs_collection\GridLayoutDiscoveryInterface; @@ -145,6 +147,73 @@ class OverviewController extends ControllerBase { return $this->paragraphsTypesLayoutEnabled; } + + /** + * Finds all Paragraphs which use a particular grid layout behaviour. + * + * @param string $grid_layout + * The machine name of the grid layout. + * @return array + * Array of IDs of Paragraphs that use the grid layout. + */ + public function getParagraphsPerLayout($grid_layout) { + $paragraphs_enabled = $this->getParagraphsLayoutEnabled(); + + $paragraphs = []; + foreach ($paragraphs_enabled as $paragraph => $used_layout) { + if ($grid_layout == $used_layout) { + $paragraphs[] = $paragraph; + } + } + + return $paragraphs; + } + + /** + * Finds all Paragraphs Types which allow a particular grid layout behaviour. + * + * @param string $grid_layout + * The machine name of the grid layout. + * @return array + * Array of IDs of Paragraphs Types that use the grid layout. + */ + public function getParagraphsTypesPerLayout($grid_layout) { + $paragraphs_types_enabled = $this->getParagraphsTypesLayoutEnabled(); + + $paragraphs_types = []; + foreach ($paragraphs_types_enabled as $paragraphs_type => $enabled_layouts) { + if ($enabled_layouts == [] || in_array($grid_layout, $enabled_layouts)) { + $paragraphs_types[] = $paragraphs_type; + } + } + + return $paragraphs_types; + } + + /** + * Finds the node using a Paragraph. + * + * @param Paragraph $paragraph + * The Paragraph entity. + * @return Node|null + * The node using the Paragraph. NULL if there are unexpected content types + * or missing ancestors in the hierarchy of references. + */ + public static function nodeUsingParagraph(Paragraph $paragraph) { + /** @var $parent ContentEntityInterface */ + $parent = $paragraph->getParentEntity(); + $parent_type_id = $parent ? $parent->getEntityTypeId() : NULL; + if ($parent_type_id == 'node') { + /** @var $parent Node */ + return $parent; + } + if ($parent_type_id == 'paragraph' ) { + /** @var $parent Paragraph */ + return static::nodeUsingParagraph($parent); + } + return NULL; + } + /** * Generates an overview page of available grid layouts. *