Es ist möglich machen Arrays Funktionen Vorprozess zu ändern, aber in Ihrem Fall ist es keine gute Idee. Der Link, über den Sie sprechen, ist ein Ergebnis der Darstellung von Feldformatierern. Sie brauchen also nur einen anderen Feldformatierer für das Feld "Typ" anstelle des aktuellen Formatierer "Label".
Erstellen neuer Formatierer ist ziemlich einfach (vor allem, wenn Sie EntityReferenceLabelFormatter
als ein Beispiel verwenden). Angenommen, Sie haben ein Modul namens entity_reference_link_formatter
. Dann in dem Verzeichnis dieses Moduls src/Plugin/Field/FieldFormatter
Ordner anlegen und dort die folgenden EntityReferenceLinkFormatter.php
Datei:
<?php
/**
* @file
* Contains Drupal\entity_reference_link_formatter\Plugin\Field\FieldFormatter\EntityReferenceLinkFormatter
*/
namespace Drupal\entity_reference_link_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'entity reference link' formatter.
*
* @FieldFormatter(
* id = "entity_reference_link",
* label = @Translation("Link"),
* description = @Translation("Display the link to the referenced entity."),
* field_types = {
* "entity_reference"
* }
*)
*/
class EntityReferenceLinkFormatter extends EntityReferenceFormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'text' => 'View',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['text'] = [
'#title' => t('Text of the link to the referenced entity'),
'#type' => 'textfield',
'#required' => true,
'#default_value' => $this->getSetting('text'),
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = t('Link text: @text', ['@text' => $this->getSetting('text')]);
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
if (!$entity->isNew()) {
try {
$uri = $entity->urlInfo();
$elements[$delta] = [
'#type' => 'link',
'#title' => t('!text', ['!text' => $this->getSetting('text')]),
'#url' => $uri,
'#options' => $uri->getOptions(),
];
if (!empty($items[$delta]->_attributes)) {
$elements[$delta]['#options'] += array('attributes' => array());
$elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and shouldn't be rendered in the field template.
unset($items[$delta]->_attributes);
}
}
catch (UndefinedLinkTemplateException $e) {
// This exception is thrown by \Drupal\Core\Entity\Entity::urlInfo()
// and it means that the entity type doesn't have a link template nor
// a valid "uri_callback", so don't bother trying to output a link for
// the rest of the referenced entities.
}
}
$elements[$delta]['#cache']['tags'] = $entity->getCacheTags();
}
return $elements;
}
}
Nach der Aktivierung dieses Moduls (oder nach dem Löschen des Cache, wenn dieses Modul zuvor aktiviert wurde), werden Sie ‚Link‘ Formatter haben Für alle Felder der Entitätsreferenz können Sie den Linktext nur in den Formatierungseinstellungen anpassen.