Setup
- Drupal 8.2 + Views + Facets
- View
articles
…- … with first sub-view
page_1
- … has facets
- … with first sub-view
- View filter:
field_title
- View facet:
field_facet
allows multiple values, e.g.A
andB
Concept
I would like to fetch the view results programmatically by reusing the $_GET
query values. (The $_GET
values are serialized and stored for later usage.)
Problem
The results are correct when working with only non-facet filters. Facet filter values are ignored and won't be filtering the results by the provided snippet.
Is the View working as expected?
Yes, the view is working as expected: Filtering works when visiting the website with a HTTP client and the query ?field_title=Hello&field_facet[0]=A&field_facet[1]=B
.
Snippet
<?php
use Drupal\views\Views;
$view = Views::getView('articles');
$query = [
// Filters results with title `Hello` as expected.
'field_title' => 'Hello',
// The programatically executed View ignores
// the following facet based values for filtering.
'field_facet' => ['A', 'B'],
];
$view->setExposedInput($query);
// bool(true)
var_dump($view->execute('page_1'));
$result = [];
/** @var Drupal\views\ResultRow $row */
foreach ($view->result as $row) {
/** @var Drupal\search_api\Item\Item $item */
$item = $row->_item;
/** @var Drupal\node\Entity\Node $node */
$node = $item->getOriginalObject()->getValue();
$result[] = [
'nid' => $node->id(),
'title ($item)' => $item->getField('title')->getValues()[0],
'title ($node)' => $node->get('title')->getValue(),
];
}
Experiments with the REST module
The REST module allows to create views with JSON results. Sadly, this module is also ignoring facet based filters.
Question
How do I inject/attach the facets to use their filters?