EntityReferenceLabelFormatterAjax.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Drupal\edlp_ajax\Plugin\Field\FieldFormatter;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Core\Field\FieldItemInterface;
  5. use Drupal\Core\Field\FieldItemListInterface;
  6. use Drupal\Core\Field\FormatterBase;
  7. use Drupal\Core\Form\FormStateInterface;
  8. use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;
  9. /**
  10. * Plugin implementation of the 'entity_reference_label_formatter_ajax' formatter.
  11. *
  12. * @FieldFormatter(
  13. * id = "entity_reference_label_formatter_ajax",
  14. * label = @Translation("Label link ajax ready"),
  15. * field_types = {
  16. * "entity_reference"
  17. * }
  18. * )
  19. */
  20. class EntityReferenceLabelFormatterAjax extends EntityReferenceLabelFormatter {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function defaultSettings() {
  25. return [
  26. // Implement default settings.
  27. ] + parent::defaultSettings();
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function settingsForm(array $form, FormStateInterface $form_state) {
  33. return [
  34. // Implement settings form.
  35. ] + parent::settingsForm($form, $form_state);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. // public function settingsSummary() {
  41. // $summary = [];
  42. // // Implement settings summary.
  43. //
  44. // return $summary;
  45. // }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function viewElements(FieldItemListInterface $items, $langcode) {
  50. $elements = [];
  51. $output_as_link = $this->getSetting('link');
  52. $current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
  53. foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
  54. if ($entity->hasTranslation($current_langcode)) {
  55. $entity = $entity->getTranslation($current_langcode);
  56. }
  57. $label = $entity->label();
  58. // If the link is to be displayed and the entity has a uri, display a
  59. // link.
  60. if ($output_as_link && !$entity->isNew()) {
  61. try {
  62. $uri = $entity->urlInfo();
  63. $options = $uri->getOptions();
  64. $options += array(
  65. 'attributes' => array(
  66. 'class' => ['ajax-link'],
  67. 'nid'=>$entity->id(),
  68. 'data-drupal-link-system-path' => $uri->getInternalPath()
  69. )
  70. );
  71. $uri->setOptions($options);
  72. }
  73. catch (UndefinedLinkTemplateException $e) {
  74. // This exception is thrown by \Drupal\Core\Entity\Entity::urlInfo()
  75. // and it means that the entity type doesn't have a link template nor
  76. // a valid "uri_callback", so don't bother trying to output a link for
  77. // the rest of the referenced entities.
  78. $output_as_link = FALSE;
  79. }
  80. }
  81. if ($output_as_link && isset($uri) && !$entity->isNew()) {
  82. $elements[$delta] = [
  83. '#type' => 'link',
  84. '#title' => $label,
  85. '#url' => $uri,
  86. '#options' => $uri->getOptions(),
  87. ];
  88. if (!empty($items[$delta]->_attributes)) {
  89. $elements[$delta]['#options'] += ['attributes' => []];
  90. $elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes;
  91. // Unset field item attributes since they have been included in the
  92. // formatter output and shouldn't be rendered in the field template.
  93. unset($items[$delta]->_attributes);
  94. }
  95. }
  96. else {
  97. $elements[$delta] = ['#plain_text' => $label];
  98. }
  99. $elements[$delta]['#cache']['tags'] = $entity->getCacheTags();
  100. }
  101. return $elements;
  102. }
  103. /**
  104. * Generate the output appropriate for one field item.
  105. *
  106. * @param \Drupal\Core\Field\FieldItemInterface $item
  107. * One field item.
  108. *
  109. * @return string
  110. * The textual output generated.
  111. */
  112. protected function viewValue(FieldItemInterface $item) {
  113. // The text value has no text format assigned to it, so the user input
  114. // should equal the output, including newlines.
  115. return nl2br(Html::escape($item->value));
  116. }
  117. }