View source
<?php
namespace Drupal\paragraphs_collection\Controller;
use Drupal\Component\Utility\Html;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\paragraphs_collection\GridLayoutDiscoveryInterface;
use Drupal\paragraphs_collection\StyleDiscoveryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OverviewController extends ControllerBase {
protected $gridLayoutDiscovery;
protected $styleDiscovery;
protected $paragraphsTypesGroupedByGridLayouts;
protected $paragraphsTypesGroupedByStyles;
public function __construct(GridLayoutDiscoveryInterface $grid_layout_discovery, StyleDiscoveryInterface $style_discovery) {
$this->gridLayoutDiscovery = $grid_layout_discovery;
$this->styleDiscovery = $style_discovery;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('paragraphs_collection.grid_layout_discovery'), $container
->get('paragraphs_collection.style_discovery'));
}
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);
$grid_layouts_grouped_by_paragraphs_types = [];
foreach ($paragraphs_types as $paragraph_type_id => $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;
}
}
}
}
$layouts = $this->gridLayoutDiscovery
->getGridLayouts();
uasort($layouts, function ($layout1, $layout2) {
return strcasecmp($layout1['title'], $layout2['title']);
});
$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;
}
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);
$styles_grouped_by_paragraphs_types = [];
foreach ($paragraphs_types as $paragraph_type_id => $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']);
}
}
$styles = $this->styleDiscovery
->getStyles();
uasort($styles, function ($style1, $style2) {
return strcasecmp($style1['title'], $style2['title']);
});
$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;
}
public function layouts() {
$filters = [
'#type' => 'fieldset',
'#attributes' => [
'class' => [
'table-filter',
'js-show',
'form--inline',
],
'data-table' => '.paragraphs-collection-overview-table',
],
'#title' => $this
->t('Filter'),
];
$filters['text'] = [
'#type' => 'search',
'#title' => $this
->t('Grid layout label or ID'),
'#size' => 40,
'#attributes' => [
'class' => [
'table-filter-text',
],
'autocomplete' => 'off',
'title' => $this
->t('Enter a part of the style label or ID to filter by.'),
],
];
$header = [
'label' => $this
->t('Grid layout'),
'details' => $this
->t('Details'),
'use' => $this
->t('Used in'),
];
$layouts = $this->gridLayoutDiscovery
->getGridLayouts();
$rows = [];
foreach ($this
->getParagraphsTypesGroupedByGridLayouts() as $layout_id => $value) {
$layout = $layouts[$layout_id];
$paragraphs_type_link_list = [];
foreach ($value as $paragraphs_type_id => $paragraphs_type) {
if ($paragraphs_type_link_list != []) {
$paragraphs_type_link_list[] = [
'#plain_text' => ', ',
];
}
$paragraphs_type_link_list[] = [
'#type' => 'link',
'#title' => $paragraphs_type
->label(),
'#url' => $paragraphs_type
->toUrl(),
'#attributes' => [
'class' => [
'table-filter-paragraphs-type-source',
],
],
];
}
$row['label'] = [
'#type' => 'container',
'#plain_text' => $layout['title'],
'#attributes' => [
'class' => [
'table-filter-text-source',
],
],
];
$row['details'] = [
'#type' => 'details',
'#title' => !empty($layout['description']) ? $layout['description'] : $this
->t('Description not available.'),
'#open' => FALSE,
'#attributes' => [
'class' => [
'overview-details',
],
],
];
$row['details']['id'] = [
'#type' => 'item',
'#title' => $this
->t('ID'),
'#prefix' => '<span class="container-inline">',
'#suffix' => '</span>',
'item' => [
'#type' => 'container',
'#plain_text' => $layout_id,
'#attributes' => [
'class' => [
'table-filter-text-source',
],
],
],
];
$row['use'] = $paragraphs_type_link_list;
$rows[] = $row;
}
$table = [
'#type' => 'table',
'#header' => $header,
'#sticky' => TRUE,
'#attributes' => [
'class' => [
'paragraphs-collection-overview-table',
],
],
];
$table += $rows;
$build['filters'] = $filters;
$build['table'] = $table;
$build['#attached']['library'] = [
'paragraphs_collection/overview',
];
return $build;
}
public function styles() {
$grouped_styles = $this
->getParagraphsTypesGroupedByStyles();
return $this
->formBuilder()
->getForm('Drupal\\paragraphs_collection\\Form\\StylesOverviewForm', $grouped_styles);
}
}