diff --git a/src/Controller/OverviewController.php b/src/Controller/OverviewController.php index f45d004..78316a8 100644 --- a/src/Controller/OverviewController.php +++ b/src/Controller/OverviewController.php @@ -3,9 +3,6 @@ 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; use Drupal\paragraphs_collection\StyleDiscoveryInterface; @@ -31,35 +28,25 @@ class OverviewController extends ControllerBase { protected $styleDiscovery; /** - * All IDs of Paragraphs which have a grid layout enabled plus that layout. + * Arrays of grid layout machine names keyed by Paragraph Type IDs. * - * @var array - */ - protected $paragraphsLayoutEnabled; - - /** - * Arrays of grid layouts keyed by Paragraph Type IDs which have them enabled. - * - * An empty array means that all layouts are enabled for that Paragraphs Type. + * Each Paragraph Type has all enabled layouts in its array. Exception: An + * empty array means that all layouts are enabled for that Paragraphs Type. * * @var array */ - protected $paragraphsTypesLayoutEnabled; + protected $paragraphsTypesLayoutsEnabled; /** - * All IDs of Paragraphs which have a style enabled plus that layout. + * Machine names of style groups keyed by IDs of Paragraph Types. * - * @var array - */ - protected $paragraphsStyleEnabled; - /** - * Arrays of styles keyed by Paragraph Type IDs which have them enabled. - * - * An empty array means that all styles are enabled for that Paragraphs Type. + * Each Paragraph Type has the style group is uses as the value. Exception: An + * empty string means that all style groups are enabled for that Paragraphs + * Type. * * @var array */ - protected $paragraphsStyleLayoutEnabled; + protected $paragraphsTypesStyleGroupsUsed; /** * Constructs a \Drupal\paragraphs_collection\Controller\OverviewController object. @@ -85,56 +72,25 @@ class OverviewController extends ControllerBase { } /** - * 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. + * 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 getParagraphsTypesLayoutEnabled() { - if (isset($this->paragraphsTypesLayoutEnabled)) { - return $this->paragraphsTypesLayoutEnabled; + 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) { + 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) { + if (isset($configuration['enabled']) && $configuration['enabled']) { $paragraphs_types_enabled[$paragraph_type_id] = []; foreach ($configuration['available_grid_layouts'] as $key => $value) { if ($value) { @@ -143,46 +99,52 @@ class OverviewController extends ControllerBase { } } } - $this->paragraphsTypesLayoutEnabled = $paragraphs_types_enabled; + $this->paragraphsTypesLayoutsEnabled = $paragraphs_types_enabled; - return $this->paragraphsTypesLayoutEnabled; + return $this->paragraphsTypesLayoutsEnabled; } /** - * Finds all Paragraphs which use a particular grid layout behaviour. + * Finds all Paragraphs Types which allow a style behaviour. * - * @param string $grid_layout - * The machine name of the grid layout. * @return array - * Array of IDs of Paragraphs that use the grid layout. + * 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 getParagraphsPerLayout($grid_layout) { - $paragraphs_enabled = $this->getParagraphsLayoutEnabled(); + public function getParagraphsTypesStyleGroupsUsed() { + if (isset($this->paragraphsTypesStyleGroupsUsed)) { + return $this->paragraphsTypesStyleGroupsUsed; + } - $paragraphs = []; - foreach ($paragraphs_enabled as $paragraph => $used_layout) { - if ($grid_layout == $used_layout) { - $paragraphs[] = $paragraph; + $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 $paragraphs; + return $this->paragraphsTypesStyleGroupsUsed; } /** * Finds all Paragraphs Types which allow a particular grid layout behaviour. * - * @param string $grid_layout + * @param string $layout * The machine name of the grid layout. - * @return array + * +*@return array * Array of IDs of Paragraphs Types that use the grid layout. */ - public function getParagraphsTypesPerLayout($grid_layout) { - $paragraphs_types_enabled = $this->getParagraphsTypesLayoutEnabled(); - + public function getParagraphsTypesPerLayout($layout) { $paragraphs_types = []; - foreach ($paragraphs_types_enabled as $paragraphs_type => $enabled_layouts) { - if ($enabled_layouts == [] || in_array($grid_layout, $enabled_layouts)) { + foreach ($this->getParagraphsTypesLayoutsEnabled() as $paragraphs_type => $enabled_layouts) { + if ($enabled_layouts == [] || in_array($layout, $enabled_layouts)) { $paragraphs_types[] = $paragraphs_type; } } @@ -191,27 +153,23 @@ class OverviewController extends ControllerBase { } /** - * Finds the node using a Paragraph. + * Finds all Paragraphs Types which allow a particular style behaviour. * - * @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. + * @param string $style + * The machine name of the style. + * @return array + * Array of IDs of Paragraphs Types that use the style. */ - 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); + 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 NULL; + + return $paragraphs_types; } /** @@ -222,8 +180,13 @@ class OverviewController extends ControllerBase { */ public function layouts() { $layouts = $this->gridDiscovery->getGridLayouts(); - $paragraphs_enabled = $this->getParagraphsLayoutEnabled(); - $paragraphs_types_enabled = $this->getParagraphsTypesLayoutEnabled(); + 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']); } @@ -236,6 +199,13 @@ class OverviewController extends ControllerBase { */ 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']); } @@ -256,5 +226,4 @@ class OverviewController extends ControllerBase { return $build; } - }