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..78316a8 --- /dev/null +++ b/src/Controller/OverviewController.php @@ -0,0 +1,229 @@ +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 Types which allow a grid layout behaviour. + * + * @return array + * Arrays of grid layout machine names keyed by IDs of Paragraph Types which + * have them enabled. A Paragraphs Type ID with an empty array as its value + * means that all grid layouts are enabled for that Paragraphs Type. + */ + public function getParagraphsTypesLayoutsEnabled() { + if (isset($this->paragraphsTypesLayoutsEnabled)) { + return $this->paragraphsTypesLayoutsEnabled; + } + + $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']) { + $paragraphs_types_enabled[$paragraph_type_id] = []; + foreach ($configuration['available_grid_layouts'] as $key => $value) { + if ($value) { + $paragraphs_types_enabled[$paragraph_type_id][] = $key; + } + } + } + } + $this->paragraphsTypesLayoutsEnabled = $paragraphs_types_enabled; + + return $this->paragraphsTypesLayoutsEnabled; + } + + /** + * Finds all Paragraphs Types which allow a style behaviour. + * + * @return array + * Style groups machine names keyed by IDs of Paragraph Types which have + * them enabled. A Paragraphs Type ID with an empty sting as its value means + * that all styles are enabled for that Paragraphs Type. + */ + public function getParagraphsTypesStyleGroupsUsed() { + if (isset($this->paragraphsTypesStyleGroupsUsed)) { + return $this->paragraphsTypesStyleGroupsUsed; + } + + $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('style')->getConfiguration(); + if (isset($configuration['enabled']) && $configuration['enabled']) { + $paragraphs_types_enabled[$paragraph_type_id] = $configuration['group']; + } + } + $this->paragraphsTypesStyleGroupsUsed = $paragraphs_types_enabled; + + return $this->paragraphsTypesStyleGroupsUsed; + } + + /** + * Finds all Paragraphs Types which allow a particular grid layout behaviour. + * + * @param string $layout + * The machine name of the grid layout. + * +*@return array + * Array of IDs of Paragraphs Types that use the grid layout. + */ + public function getParagraphsTypesPerLayout($layout) { + $paragraphs_types = []; + foreach ($this->getParagraphsTypesLayoutsEnabled() as $paragraphs_type => $enabled_layouts) { + if ($enabled_layouts == [] || in_array($layout, $enabled_layouts)) { + $paragraphs_types[] = $paragraphs_type; + } + } + + return $paragraphs_types; + } + + /** + * Finds all Paragraphs Types which allow a particular style behaviour. + * + * @param string $style + * The machine name of the style. + * @return array + * Array of IDs of Paragraphs Types that use the style. + */ + public function getParagraphsTypesPerStyle($style) { + $paragraphs_types = []; + foreach ($this->getParagraphsTypesStyleGroupsUsed() as $paragraphs_type => $used_style_group) { + $enabled_styles = array_keys($this->styleDiscovery->getStyleOptions($used_style_group)); + if (in_array($style, $enabled_styles)) { + $paragraphs_types[] = $paragraphs_type; + } + } + + return $paragraphs_types; + } + + /** + * Generates an overview page of available grid layouts. + * + * @return array + * The output render array. + */ + public function layouts() { + $layouts = $this->gridDiscovery->getGridLayouts(); + foreach (array_keys($layouts) as $layout_id) { + $paragraphs_type_ids = $this->getParagraphsTypesPerLayout($layout_id); + foreach ($paragraphs_type_ids as $paragraphs_type_id) { + $paragraphs_type = ParagraphsType::load($paragraphs_type_id); + $path = $paragraphs_type->toUrl()->getInternalPath(); + } + } + + return $this->content(['layouts']); + } + + /** + * Generates an overview page of available styles. + * + * @return array + * The output render array. + */ + public function styles() { + $styles = $this->styleDiscovery->getStyles(); + foreach (array_keys($styles) as $style_id) { + $paragraphs_type_ids = $this->getParagraphsTypesPerStyle($style_id); + foreach ($paragraphs_type_ids as $paragraphs_type_id) { + $paragraphs_type = ParagraphsType::load($paragraphs_type_id); + $path = $paragraphs_type->toUrl()->getInternalPath(); + } + } + + 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; + } + +}