|
|
|
|
@ -88,6 +88,11 @@ class SolrFacetTextFieldFormatter extends FormatterBase implements ContainerFact
|
|
|
|
|
'facet_source' => '', |
|
|
|
|
// The machine name of the facet to filter on. |
|
|
|
|
'facet_id' => '', |
|
|
|
|
// Which value of the field to send to the facet: 'auto' (referenced |
|
|
|
|
// entity label for reference fields, field value otherwise), 'label' |
|
|
|
|
// (force the referenced entity label) or 'raw' (the stored value, i.e. |
|
|
|
|
// the entity id for reference fields). |
|
|
|
|
'value_source' => 'auto', |
|
|
|
|
// Whether to reset an exposed fulltext filter on the target page so the |
|
|
|
|
// link produces a clean, facet-only search. |
|
|
|
|
'reset_fulltext' => TRUE, |
|
|
|
|
@ -122,6 +127,14 @@ class SolrFacetTextFieldFormatter extends FormatterBase implements ContainerFact
|
|
|
|
|
'#required' => TRUE, |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$form['value_source'] = [ |
|
|
|
|
'#type' => 'select', |
|
|
|
|
'#title' => $this->t('Facet value'), |
|
|
|
|
'#description' => $this->t('Which value of the field to filter the facet by. Only affects reference fields (entity reference, typed relation, …).'), |
|
|
|
|
'#options' => $this->getValueSourceOptions(), |
|
|
|
|
'#default_value' => $this->getSetting('value_source'), |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$form['reset_fulltext'] = [ |
|
|
|
|
'#type' => 'checkbox', |
|
|
|
|
'#title' => $this->t('Reset fulltext search'), |
|
|
|
|
@ -158,6 +171,13 @@ class SolrFacetTextFieldFormatter extends FormatterBase implements ContainerFact
|
|
|
|
|
'%facet' => $this->getFacetLabel($facet_id), |
|
|
|
|
'%source' => $this->getFacetSourceLabel($facet_source), |
|
|
|
|
]); |
|
|
|
|
$value_source = $this->getSetting('value_source'); |
|
|
|
|
if ($value_source && $value_source !== 'auto') { |
|
|
|
|
$options = $this->getValueSourceOptions(); |
|
|
|
|
if (isset($options[$value_source])) { |
|
|
|
|
$summary[] = $this->t('Facet value: @value.', ['@value' => $options[$value_source]]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
$summary[] = $this->t('Not configured: select a facet source and facet.'); |
|
|
|
|
@ -213,13 +233,31 @@ class SolrFacetTextFieldFormatter extends FormatterBase implements ContainerFact
|
|
|
|
|
return $element; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* The available "Facet value" options for the settings form. |
|
|
|
|
* |
|
|
|
|
* @return array |
|
|
|
|
* An array of human readable labels keyed by value source id. |
|
|
|
|
*/ |
|
|
|
|
protected function getValueSourceOptions() { |
|
|
|
|
return [ |
|
|
|
|
'auto' => $this->t('Automatic (referenced entity label, or the field value)'), |
|
|
|
|
'label' => $this->t('Referenced entity label'), |
|
|
|
|
'raw' => $this->t('Raw stored value (entity ID for reference fields)'), |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Extracts the value to filter the facet by from a field item. |
|
|
|
|
* |
|
|
|
|
* For reference fields (entity_reference, typed_relation, …) the referenced |
|
|
|
|
* entity's label is used, so the facet is filtered by the readable value |
|
|
|
|
* rather than the internal entity id. For all other field types the field's |
|
|
|
|
* main property value is used. |
|
|
|
|
* The "Facet value" setting controls how reference fields (entity_reference, |
|
|
|
|
* typed_relation, …) are resolved: |
|
|
|
|
* - 'auto': the referenced entity's label, so the facet is filtered by the |
|
|
|
|
* readable value rather than the internal entity id. |
|
|
|
|
* - 'label': force the referenced entity's label. |
|
|
|
|
* - 'raw': the stored value (the entity id for reference fields). |
|
|
|
|
* |
|
|
|
|
* For non-reference fields the field's main property value is always used. |
|
|
|
|
* |
|
|
|
|
* @param \Drupal\Core\Field\FieldItemInterface $item |
|
|
|
|
* The field item. |
|
|
|
|
@ -228,11 +266,15 @@ class SolrFacetTextFieldFormatter extends FormatterBase implements ContainerFact
|
|
|
|
|
* The facet value, or NULL if none could be determined. |
|
|
|
|
*/ |
|
|
|
|
protected function getFacetValue(FieldItemInterface $item) { |
|
|
|
|
if ($item instanceof EntityReferenceItem) { |
|
|
|
|
$value_source = $this->getSetting('value_source'); |
|
|
|
|
|
|
|
|
|
if ($value_source !== 'raw' && $item instanceof EntityReferenceItem) { |
|
|
|
|
$entity = $item->entity; |
|
|
|
|
if ($entity instanceof EntityInterface) { |
|
|
|
|
return $entity->label(); |
|
|
|
|
} |
|
|
|
|
// For an explicit 'label' request with no loadable entity, fall through |
|
|
|
|
// to the raw value below rather than returning nothing. |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$main_property = $item->getFieldDefinition() |
|
|
|
|
|