EntityReferenceLabelFormatter.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
  3. use Drupal\Core\Entity\EntityInterface;
  4. use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
  5. use Drupal\Core\Field\FieldItemListInterface;
  6. use Drupal\Core\Form\FormStateInterface;
  7. /**
  8. * Plugin implementation of the 'entity reference label' formatter.
  9. *
  10. * @FieldFormatter(
  11. * id = "entity_reference_label",
  12. * label = @Translation("Label"),
  13. * description = @Translation("Display the label of the referenced entities."),
  14. * field_types = {
  15. * "entity_reference"
  16. * }
  17. * )
  18. */
  19. class EntityReferenceLabelFormatter extends EntityReferenceFormatterBase {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function defaultSettings() {
  24. return [
  25. 'link' => TRUE,
  26. ] + parent::defaultSettings();
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function settingsForm(array $form, FormStateInterface $form_state) {
  32. $elements['link'] = [
  33. '#title' => t('Link label to the referenced entity'),
  34. '#type' => 'checkbox',
  35. '#default_value' => $this->getSetting('link'),
  36. ];
  37. return $elements;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function settingsSummary() {
  43. $summary = [];
  44. $summary[] = $this->getSetting('link') ? t('Link to the referenced entity') : t('No link');
  45. return $summary;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function viewElements(FieldItemListInterface $items, $langcode) {
  51. $elements = [];
  52. $output_as_link = $this->getSetting('link');
  53. foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
  54. $label = $entity->label();
  55. // If the link is to be displayed and the entity has a uri, display a
  56. // link.
  57. if ($output_as_link && !$entity->isNew()) {
  58. try {
  59. $uri = $entity->urlInfo();
  60. }
  61. catch (UndefinedLinkTemplateException $e) {
  62. // This exception is thrown by \Drupal\Core\Entity\Entity::urlInfo()
  63. // and it means that the entity type doesn't have a link template nor
  64. // a valid "uri_callback", so don't bother trying to output a link for
  65. // the rest of the referenced entities.
  66. $output_as_link = FALSE;
  67. }
  68. }
  69. if ($output_as_link && isset($uri) && !$entity->isNew()) {
  70. $elements[$delta] = [
  71. '#type' => 'link',
  72. '#title' => $label,
  73. '#url' => $uri,
  74. '#options' => $uri->getOptions(),
  75. ];
  76. if (!empty($items[$delta]->_attributes)) {
  77. $elements[$delta]['#options'] += ['attributes' => []];
  78. $elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes;
  79. // Unset field item attributes since they have been included in the
  80. // formatter output and shouldn't be rendered in the field template.
  81. unset($items[$delta]->_attributes);
  82. }
  83. }
  84. else {
  85. $elements[$delta] = ['#plain_text' => $label];
  86. }
  87. $elements[$delta]['#cache']['tags'] = $entity->getCacheTags();
  88. }
  89. return $elements;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. protected function checkAccess(EntityInterface $entity) {
  95. return $entity->access('view label', NULL, TRUE);
  96. }
  97. }