Plugin implementation of the 'taxonomy_term_reference_link' formatter.
@FieldFormatter(
id = "taxonomy_term_reference_link",
module = "taxonomy",
label = @Translation("Link"),
field_types = {
"taxonomy_term_reference"
}
)
Expanded class hierarchy of LinkFormatter
class LinkFormatter extends TaxonomyFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
// Terms without tid do not exist yet, theme such terms as just their name.
foreach ($items as $delta => $item) {
if (!$item['tid']) {
$elements[$delta] = array(
'#markup' => check_plain($item['entity']
->label()),
);
}
else {
$term = $item['entity'];
$uri = $term
->uri();
$elements[$delta] = array(
'#type' => 'link',
'#title' => $term
->label(),
'#href' => $uri['path'],
'#options' => $uri['options'],
);
}
}
return $elements;
}
}