You are here

class OverviewController in Paragraphs Collection 8

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

The grid layout and style behavior plugins use pluggable YAML files which define the concrete layouts and styles that can be then used by specific Paragraph entities. There are plugin-specific classes for the discovery of these files. Paragraphs Types can restrict the available items. This controller creates overview pages for these items.

Hierarchy

Expanded class hierarchy of OverviewController

See also

\Drupal\paragraphs_collection\Plugin\paragraphs\Behavior\ParagraphsGridLayoutPlugin

\Drupal\paragraphs_collection\Plugin\paragraphs\Behavior\ParagraphsStylePlugin

\Drupal\paragraphs_collection\GridLayoutDiscoveryInterface

\Drupal\paragraphs_collection\StyleDiscoveryInterface

File

src/Controller/OverviewController.php, line 27

Namespace

Drupal\paragraphs_collection\Controller
View source
class OverviewController extends ControllerBase {

  /**
   * The discovery service for grid layout files.
   *
   * @var \Drupal\paragraphs_collection\GridLayoutDiscoveryInterface
   */
  protected $gridLayoutDiscovery;

  /**
   * The discovery service for style files.
   *
   * @var \Drupal\paragraphs_collection\StyleDiscoveryInterface
   */
  protected $styleDiscovery;

  /**
   * A nested array of Paragraphs Type objects.
   *
   * 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:
   * @code
   * [
   *   '1_2_1_column_layout' => [
   *     'grid_pt' => $grid_paragraphs_type_object,
   *   ]
   * ]
   * @endcode
   *
   * @var array
   */
  protected $paragraphsTypesGroupedByGridLayouts;

  /**
   * A nested array of Paragraphs Type objects.
   *
   * 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:
   * @code
   * [
   *   'blue_style' => [
   *     'style_pt' => $style_paragraphs_type_object,
   *   ]
   * ]
   * @endcode
   *
   * @var array
   */
  protected $paragraphsTypesGroupedByStyles;

  /**
   * Constructs a \Drupal\paragraphs_collection\Controller\OverviewController object.
   *
   * @param \Drupal\paragraphs_collection\GridLayoutDiscoveryInterface $grid_layout_discovery
   *   The discovery service for grid layout files.
   * @param \Drupal\paragraphs_collection\StyleDiscoveryInterface $style_discovery
   *   The discovery service for style files.
   */
  public function __construct(GridLayoutDiscoveryInterface $grid_layout_discovery, StyleDiscoveryInterface $style_discovery) {
    $this->gridLayoutDiscovery = $grid_layout_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'));
  }

  /**
   * Lists grid layouts with the Paragraphs Types that allow them.
   *
   * @return 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:
   *   @code
   *   [
   *     '1_2_1_column_layout' => [
   *       'grid_pt' => $grid_paragraphs_type_object,
   *     ]
   *   ]
   *   @endcode
   */
  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;
  }

  /**
   * Lists styles with the Paragraphs Types that allow them.
   *
   * @return 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:
   *   @code
   *   [
   *     'blue_style' => [
   *       'style_pt' => $style_paragraphs_type_object,
   *     ]
   *   ]
   *   @endcode
   */
  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;
  }

  /**
   * Generates an overview page of available layouts for the grid layout plugin.
   *
   * @return array
   *   The output render array.
   */
  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) {

        /** @var ParagraphsType $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;
  }

  /**
   * Generates an overview page of available styles for the styles plugin.
   *
   * @return array
   *   The output render array.
   */
  public function styles() {
    $grouped_styles = $this
      ->getParagraphsTypesGroupedByStyles();
    return $this
      ->formBuilder()
      ->getForm('Drupal\\paragraphs_collection\\Form\\StylesOverviewForm', $grouped_styles);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OverviewController::$gridLayoutDiscovery protected property The discovery service for grid layout files.
OverviewController::$paragraphsTypesGroupedByGridLayouts protected property A nested array of Paragraphs Type objects.
OverviewController::$paragraphsTypesGroupedByStyles protected property A nested array of Paragraphs Type objects.
OverviewController::$styleDiscovery protected property The discovery service for style files.
OverviewController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
OverviewController::getParagraphsTypesGroupedByGridLayouts public function Lists grid layouts with the Paragraphs Types that allow them.
OverviewController::getParagraphsTypesGroupedByStyles public function Lists styles with the Paragraphs Types that allow them.
OverviewController::layouts public function Generates an overview page of available layouts for the grid layout plugin.
OverviewController::styles public function Generates an overview page of available styles for the styles plugin.
OverviewController::__construct public function Constructs a \Drupal\paragraphs_collection\Controller\OverviewController object.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.