Browse Source

attempt to add the typed relation prefix to the formatted output

master
Paul Pound 1 day ago
parent
commit
b2d17d8010
  1. 40
      src/Plugin/Field/FieldFormatter/SolrFacetTextFieldFormatter.php

40
src/Plugin/Field/FieldFormatter/SolrFacetTextFieldFormatter.php

@ -2,6 +2,7 @@
namespace Drupal\solr_facet_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldItemInterface;
@ -228,11 +229,50 @@ class SolrFacetTextFieldFormatter extends FormatterBase implements ContainerFact
$url = Url::fromUserInput($path, ['query' => $query]);
$link = Link::fromTextAndUrl($value, $url);
$element[$delta] = [$link->toRenderable()];
// For Islandora typed relation fields, show the relation type as a
// prefix outside the link (e.g. "Author: Jane Doe"). Set on the wrapper
// element so it renders before the link, not inside the anchor text.
$prefix = $this->getTypedRelationPrefix($item);
if ($prefix !== NULL) {
$element[$delta]['#prefix'] = new FormattableMarkup('@prefix: ', ['@prefix' => $prefix]);
}
}
return $element;
}
/**
* Builds the relation-type prefix for an Islandora typed relation item.
*
* The check is based on the field type id so the module has no hard
* dependency on controlled_access_terms; the typed relation methods are only
* called once the field type is confirmed.
*
* @param \Drupal\Core\Field\FieldItemInterface $item
* The field item.
*
* @return string|null
* The human readable relation type label, or NULL if the item is not a
* typed relation or has no relation type set.
*/
protected function getTypedRelationPrefix(FieldItemInterface $item) {
if ($item->getFieldDefinition()->getType() !== 'typed_relation') {
return NULL;
}
$rel_type_key = $item->rel_type;
if (empty($rel_type_key)) {
return NULL;
}
// getRelTypes() maps the stored key to a human readable label.
$rel_types = $item->getRelTypes();
$label = $rel_types[$rel_type_key] ?? $rel_type_key;
return $label !== '' ? $label : NULL;
}
/**
* The available "Facet value" options for the settings form.
*

Loading…
Cancel
Save