public function UserFlagWidget::build in Flag Search API 8
Builds the facet widget for rendering.
Parameters
\Drupal\facets\FacetInterface $facet: The facet we need to build.
Return value
array A renderable array.
Overrides WidgetPluginBase::build
File
- src/
Plugin/ facets/ widget/ UserFlagWidget.php, line 63
Class
- UserFlagWidget
- The user_flag widget.
Namespace
Drupal\flag_search_api\Plugin\facets\widgetCode
public function build(FacetInterface $facet) {
// Get current user ID.
$uid = $this->currentUser
->id();
$userFlagResult = FALSE;
// Get results.
$results = $facet
->getResults();
foreach ($results as $result) {
// Select a result that matches the current user.
if ($result
->getRawValue() == $uid) {
$userFlagResult = $result;
$userFlagResult
->setDisplayValue($this
->getConfiguration()['flags_label']);
}
}
if ($userFlagResult) {
// Replace all results with the selected result.
$facet
->setResults([
$userFlagResult,
]);
}
else {
// Replace all results with an empty result.
$emptyResult = new Result($facet, $uid, $this
->getConfiguration()['no_flags_label'], 0);
$facet
->setResults([
$emptyResult,
]);
}
// Go through normal build process with checkboxes.
$build = parent::build($facet);
$build['#attributes']['class'][] = 'js-facets-checkbox-links';
$build['#attached']['library'][] = 'facets/drupal.facets.checkbox-widget';
return $build;
}