You are here

public function OverviewController::getParagraphsTypesGroupedByGridLayouts in Paragraphs Collection 8

Lists grid layouts with the Paragraphs Types that allow them.

Return value

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

[
  '1_2_1_column_layout' => [
    'grid_pt' => $grid_paragraphs_type_object,
  ],
];
1 call to OverviewController::getParagraphsTypesGroupedByGridLayouts()
OverviewController::layouts in src/Controller/OverviewController.php
Generates an overview page of available layouts for the grid layout plugin.

File

src/Controller/OverviewController.php, line 125

Class

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

Namespace

Drupal\paragraphs_collection\Controller

Code

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

  // Find all enabled grid layouts for each Paragraphs Type.
  // An empty array as the second-level value means that all grid layouts are
  // enabled for that Paragraphs type.
  $grid_layouts_grouped_by_paragraphs_types = [];
  foreach ($paragraphs_types as $paragraph_type_id => $paragraphs_type) {

    /** @var ParagraphsType $paragraphs_type */
    $configuration = $paragraphs_type
      ->getBehaviorPlugin('grid_layout')
      ->getConfiguration();
    if (isset($configuration['enabled']) && $configuration['enabled']) {
      $grid_layouts_grouped_by_paragraphs_types[$paragraph_type_id] = [];
      foreach ($configuration['available_grid_layouts'] as $key => $value) {
        if ($value) {
          $grid_layouts_grouped_by_paragraphs_types[$paragraph_type_id][] = $key;
        }
      }
    }
  }

  // Get all grid layouts ordered by title.
  $layouts = $this->gridLayoutDiscovery
    ->getGridLayouts();
  uasort($layouts, function ($layout1, $layout2) {
    return strcasecmp($layout1['title'], $layout2['title']);
  });

  // Group Paragraphs Types by grid layouts.
  $paragraphs_types_grouped_by_grid_layouts = [];
  foreach ($layouts as $layout_id => $layout) {
    $paragraphs_types_grouped_by_grid_layouts[$layout_id] = [];
    foreach ($grid_layouts_grouped_by_paragraphs_types as $paragraphs_type_id => $enabled_layouts) {
      if ($enabled_layouts == [] || in_array($layout_id, $enabled_layouts)) {
        $paragraphs_types_grouped_by_grid_layouts[$layout_id][$paragraphs_type_id] = $paragraphs_types[$paragraphs_type_id];
      }
    }
  }
  return $this->paragraphsTypesGroupedByGridLayouts = $paragraphs_types_grouped_by_grid_layouts;
}