diff --git a/paragraphs_collection.links.menu.yml b/paragraphs_collection.links.menu.yml new file mode 100644 index 0000000..4ad0cdf --- /dev/null +++ b/paragraphs_collection.links.menu.yml @@ -0,0 +1,6 @@ +paragraphs_collection.layouts: + title: 'Paragraphs Collection' + parent: system.admin_reports + description: 'Overview of all layouts and styles for the respective plugins.' + route_name: paragraphs_collection.layouts + menu_name: admin diff --git a/paragraphs_collection.links.task.yml b/paragraphs_collection.links.task.yml new file mode 100644 index 0000000..b27a824 --- /dev/null +++ b/paragraphs_collection.links.task.yml @@ -0,0 +1,12 @@ +# @todo Add all the tab links. + +paragraphs_collection.layouts: + title: Layouts + route_name: paragraphs_collection.layouts + base_route: paragraphs_collection.layouts + +paragraphs_collection.styles: + title: Styles + route_name: paragraphs_collection.styles + base_route: paragraphs_collection.layouts + diff --git a/paragraphs_collection.routing.yml b/paragraphs_collection.routing.yml new file mode 100644 index 0000000..fef898f --- /dev/null +++ b/paragraphs_collection.routing.yml @@ -0,0 +1,18 @@ +# @todo Just one class with two content functions, or two classes inheriting +# from one? + +paragraphs_collection.layouts: + path: '/admin/reports/paragraphs_collection/layouts' + defaults: + _controller: '\Drupal\paragraphs_collection\Controller\OverviewController::layouts' + _title: 'Available layouts (grid layout plugin)' + requirements: + _permission: 'administer paragraphs types' # @todo Change this? + +paragraphs_collection.styles: + path: '/admin/reports/paragraphs_collection/styles' + defaults: + _controller: '\Drupal\paragraphs_collection\Controller\OverviewController::styles' + _title: 'Available Styles (styles plugin)' + requirements: + _permission: 'administer paragraphs types' # @todo Change this? diff --git a/src/Controller/OverviewController.php b/src/Controller/OverviewController.php new file mode 100644 index 0000000..f45d004 --- /dev/null +++ b/src/Controller/OverviewController.php @@ -0,0 +1,260 @@ +gridDiscovery = $grid_discovery; + $this->styleDiscovery = $style_discovery; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('paragraphs_collection.grid_layout_discovery'), + $container->get('paragraphs_collection.style_discovery') + ); + } + + /** + * Finds all Paragraphs which use a grid layout behaviour. + * + * @return array + * All IDs of Paragraphs which have a grid layout enabled plus that layout. + */ + public function getParagraphsLayoutEnabled() { + if (isset($this->paragraphsLayoutEnabled)) { + return $this->paragraphsLayoutEnabled; + } + + $paragraphs_types_enabled = $this->getParagraphsTypesLayoutEnabled(); + $paragraph_ids = \Drupal::entityQuery('paragraph')->execute(); + $paragraphs_enabled = []; + foreach ($paragraph_ids as $paragraph_id) { + /** @var Paragraph $paragraph */ + $paragraph = Paragraph::load($paragraph_id); + $layout = $paragraph->getBehaviorSetting('grid_layout', 'layout'); + $paragraphs_type_id = $paragraph->getParagraphType()->id(); + if (isset($layout) && isset($paragraphs_types_enabled[$paragraphs_type_id])){ + $available_layouts = $paragraphs_types_enabled[$paragraphs_type_id]; + if ($available_layouts == [] || in_array($layout, $available_layouts)) { + $paragraphs_enabled[$paragraph_id] = $layout; + } + } + } + $this->paragraphsLayoutEnabled = $paragraphs_enabled; + + return $this->paragraphsLayoutEnabled; + } + + /** + * Finds all Paragraphs Types which allow a grid layout behaviour. + * + * @return array + * Arrays of grid layouts keyed by Paragraph Type IDs which have them + * enabled. A Paragraphs Type ID with an empty array means that all layouts + * are enabled for that Paragraphs Type. + */ + public function getParagraphsTypesLayoutEnabled() { + if (isset($this->paragraphsTypesLayoutEnabled)) { + return $this->paragraphsTypesLayoutEnabled; + } + + $paragraph_type_ids = \Drupal::entityQuery('paragraphs_type')->execute(); + $paragraphs_types_enabled = []; + foreach ($paragraph_type_ids as $paragraph_type_id) { + /** @var ParagraphsType $paragraphs_type */ + $paragraphs_type = ParagraphsType::load($paragraph_type_id); + $configuration = $paragraphs_type->getBehaviorPlugin('grid_layout')->getConfiguration(); + if (isset($configuration['enabled']) && $configuration['enabled'] == TRUE) { + $paragraphs_types_enabled[$paragraph_type_id] = []; + foreach ($configuration['available_grid_layouts'] as $key => $value) { + if ($value) { + $paragraphs_types_enabled[$paragraph_type_id][] = $key; + } + } + } + } + $this->paragraphsTypesLayoutEnabled = $paragraphs_types_enabled; + + 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. + * + * @return array + * The output render array. + */ + public function layouts() { + $layouts = $this->gridDiscovery->getGridLayouts(); + $paragraphs_enabled = $this->getParagraphsLayoutEnabled(); + $paragraphs_types_enabled = $this->getParagraphsTypesLayoutEnabled(); + + return $this->content(['layouts']); + } + + /** + * Generates an overview page of available styles. + * + * @return array + * The output render array. + */ + public function styles() { + $styles = $this->styleDiscovery->getStyles(); + + return $this->content(['styles']); + } + + /** + * Generates an overview page of available layouts and/or styles. + * + * @param array $item_types + * Array of item types to list. Supported types are 'styles' and 'layouts'. + * @return array + * The output render array. + */ + public function content($item_types) { + $build = [ + '#type' => 'markup', + '#markup' => t('Hello World! (' . implode('', $item_types) . ')'), + ]; + return $build; + } + + +}