You are here

public function OverviewController::getParagraphsTypesGroupedByStyles in Paragraphs Collection 8

Lists styles with the Paragraphs Types that allow them.

Return value

array A nested array. The first level is keyed by style machine names. The second level is keyed Paragraphs Type IDs. The second-level values are Paragraphs Type objects that allow the respective grid layout. Styles are ordered by name. Example:

[
  'blue_style' => [
    'style_pt' => $style_paragraphs_type_object,
  ],
];
1 call to OverviewController::getParagraphsTypesGroupedByStyles()
OverviewController::styles in src/Controller/OverviewController.php
Generates an overview page of available styles for the styles plugin.

File

src/Controller/OverviewController.php, line 187

Class

OverviewController
The controller for overviews of Paragraphs Collection's discoverable items.

Namespace

Drupal\paragraphs_collection\Controller

Code

public function getParagraphsTypesGroupedByStyles() {
  if (isset($this->paragraphsTypesGroupedByStyles)) {
    return $this->paragraphsTypesGroupedByStyles;
  }
  $paragraph_type_ids = \Drupal::entityQuery('paragraphs_type')
    ->execute();
  $paragraphs_types = ParagraphsType::loadMultiple($paragraph_type_ids);

  // Find the used style group for each Paragraphs Type.
  // An as empty string as the second-level value means that the Paragraphs
  // Type uses all style groups.
  $styles_grouped_by_paragraphs_types = [];
  foreach ($paragraphs_types as $paragraph_type_id => $paragraphs_type) {

    /** @var ParagraphsType $paragraphs_type */
    $configuration = $paragraphs_type
      ->getBehaviorPlugin('style')
      ->getConfiguration();
    if (isset($configuration['enabled']) && $configuration['enabled']) {
      $styles_grouped_by_paragraphs_types[$paragraph_type_id] = array_keys($configuration['groups']);
    }
  }

  //Get all styles ordered by title.
  $styles = $this->styleDiscovery
    ->getStyles();
  uasort($styles, function ($style1, $style2) {
    return strcasecmp($style1['title'], $style2['title']);
  });

  // Group Paragraphs Types by styles.
  $paragraphs_types_grouped_by_styles = [];
  foreach ($styles as $style_id => $style) {
    $paragraphs_types_grouped_by_styles[$style_id] = [];
    foreach ($styles_grouped_by_paragraphs_types as $paragraphs_type_id => $used_style_groups) {
      $enabled_styles = [];
      foreach ($used_style_groups as $used_style_group) {
        $enabled_styles += $this->styleDiscovery
          ->getStyleOptions($used_style_group);
      }
      if (in_array($style_id, array_keys($enabled_styles))) {
        $paragraphs_types_grouped_by_styles[$style_id][$paragraphs_type_id] = $paragraphs_types[$paragraphs_type_id];
      }
    }
  }
  return $this->paragraphsTypesGroupedByStyles = $paragraphs_types_grouped_by_styles;
}