You are here

public function FacetListBuilder::loadGroups in Facets 8

Loads facet sources and facets, grouped by facet sources.

Return value

\Drupal\Core\Config\Entity\ConfigEntityInterface[][] An associative array with two keys:

  • facet sources: All available facet sources, each followed by all facets attached to it.
  • lone_facets: All facets that aren't attached to any facet source.
1 call to FacetListBuilder::loadGroups()
FacetListBuilder::buildForm in src/FacetListBuilder.php
Form constructor.

File

src/FacetListBuilder.php, line 343

Class

FacetListBuilder
Builds a listing of facet entities.

Namespace

Drupal\facets

Code

public function loadGroups() {
  $facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
  $facets = $this
    ->load();
  $facets_summaries = [];
  if (\Drupal::moduleHandler()
    ->moduleExists('facets_summary')) {
    $facets_summaries = FacetsSummary::loadMultiple();
  }
  $facet_sources = $facet_source_plugin_manager
    ->getDefinitions();
  $facet_source_groups = [];
  foreach ($facet_sources as $facet_source) {
    $facet_source_groups[$facet_source['id']] = [
      'facet_source' => $facet_source,
      'facets' => [],
    ];
    foreach ($facets as $facet) {

      /** @var \Drupal\facets\FacetInterface $facet */
      if ($facet
        ->getFacetSourceId() == $facet_source['id']) {
        $facet_source_groups[$facet_source['id']]['facets'][$facet
          ->id()] = $facet;

        // Remove this facet from $facet so it will finally only contain those
        // facets not belonging to any facet_source.
        unset($facets[$facet
          ->id()]);
      }
    }
    foreach ($facets_summaries as $summary) {

      /** @var \Drupal\facets_summary\FacetsSummaryInterface $summary */
      if ($summary
        ->getFacetSourceId() == $facet_source['id']) {
        $facet_source_groups[$facet_source['id']]['facets'][$summary
          ->id()] = $summary;

        // Remove this facet from $facet so it will finally only contain those
        // facets not belonging to any facet_source.
        unset($facets_summaries[$summary
          ->id()]);
      }
    }
  }
  return [
    'facet_source_groups' => $facet_source_groups,
    'lone_facets' => $facets,
  ];
}