You are here

public function Facet::getFacetSources in Facets 8

Loads the facet sources for this facet.

Parameters

bool $only_enabled: Only return enabled facet sources.

Return value

\Drupal\facets\FacetSource\FacetSourcePluginInterface[] An array of facet sources.

Overrides FacetInterface::getFacetSources

File

src/Entity/Facet.php, line 812

Class

Facet
Defines the facet configuration entity.

Namespace

Drupal\facets\Entity

Code

public function getFacetSources($only_enabled = FALSE) {
  if (!isset($this->facetSourcePlugins)) {
    $this->facetSourcePlugins = [];

    /* @var $facet_source_plugin_manager \Drupal\facets\FacetSource\FacetSourcePluginManager */
    $facet_source_plugin_manager = \Drupal::service('plugin.manager.facets.facet_source');
    foreach ($facet_source_plugin_manager
      ->getDefinitions() as $name => $facet_source_definition) {
      if (class_exists($facet_source_definition['class']) && empty($this->facetSourcePlugins[$name])) {

        // Create our settings for this facet source..
        $config = isset($this->facetSourcePlugins[$name]) ? $this->facetSourcePlugins[$name] : [];

        /* @var $facet_source \Drupal\facets\FacetSource\FacetSourcePluginInterface */
        $facet_source = $facet_source_plugin_manager
          ->createInstance($name, $config);
        $this->facetSourcePlugins[$name] = $facet_source;
      }
      elseif (!class_exists($facet_source_definition['class'])) {
        \Drupal::logger('facets')
          ->warning('Facet Source @id specifies a non-existing @class.', [
          '@id' => $name,
          '@class' => $facet_source_definition['class'],
        ]);
      }
    }
  }

  // Filter facet sources by status if required.
  if (!$only_enabled) {
    return $this->facetSourcePlugins;
  }
  return array_intersect_key($this->facetSourcePlugins, array_flip($this->facetSourcePlugins));
}