I'm working on a custom block that outputs information from facets and have them mostly working, but I don't understand why getActiveItems is returning empty:

<?php
    $facet_manager = \Drupal::service('facets.manager');
    $facet_storage = \Drupal::entityManager()->getStorage('facets_facet');
    $facet = $facet_storage->load($facet_id);
    $items = $facet_manager->build($facet);
    // works and I can loop through the results
    ksm($facet->getActiveItems());
    // this doesn't work, it returns an empty array
    // I looked at the breadcrumb stuff in facets.module, but didn't get it still
?>

Comments

bennybobw created an issue. See original summary.

ddomee’s picture

I had the same problem, you were only missing one line:

    $facet_manager = \Drupal::service('facets.manager');
    $facet_storage = \Drupal::entityManager()->getStorage('facets_facet');
    $facet = $facet_storage->load($facet_id);
    // this is what you were missing:
    $processed = $facetManager->returnProcessedFacet($facet);
    $items = $facet_manager->build($processed);
    ksm($processed->getActiveItems());
borisson_’s picture

Status: Active » Fixed

Thanks for helping to fix this support request @ddomee.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

fmb’s picture

For whatever reason, returnProcessedFacet() returned NULL for me, so I ended up using this code :

$facet_manager = \Drupal::service('facets.manager');
$facets = $facet_manager->getFacetsByFacetSourceId($source_id);
kint($facets[$facet_id]->getActiveItems());