Drupal 更改生成的链接字段上的链接名称

Drupal 更改生成的链接字段上的链接名称,drupal,twig,drupal-theming,drupal-8,Drupal,Twig,Drupal Theming,Drupal 8,我正在Drupal 8中创建一个自定义主题,并在node.html.twig模板上使用{{content.field\u type}显示当前页面所在类别的链接,以允许用户链接回该类别中的页面列表。使用此选项,页面将呈现: 我需要做什么将此更改为: 可以使用预处理函数更改渲染数组,但在您的情况下,这不是一个好主意。您所说的链接是字段格式化程序渲染的结果。因此,您只需要为“Type”字段使用另一个字段格式化程序,而不是当前的“Label”格式化程序 创建新的格式化程序非常容易(特别是当您使用它作为

我正在Drupal 8中创建一个自定义主题,并在node.html.twig模板上使用
{{content.field\u type}
显示当前页面所在类别的链接,以允许用户链接回该类别中的页面列表。使用此选项,页面将呈现:

我需要做什么将此更改为:


可以使用预处理函数更改渲染数组,但在您的情况下,这不是一个好主意。您所说的链接是字段格式化程序渲染的结果。因此,您只需要为“Type”字段使用另一个字段格式化程序,而不是当前的“Label”格式化程序

创建新的格式化程序非常容易(特别是当您使用它作为示例时)。假设您有一个名为
entity\u reference\u link\u formatter
的模块。然后在此模块的目录中创建
src/Plugin/Field/FieldFormatter
文件夹,并将以下
EntityReferenceLinkFormatter.php
文件放在那里:

<?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;
  }

}

可以使用预处理函数更改渲染数组,但在您的情况下,这不是一个好主意。您所说的链接是字段格式化程序渲染的结果。因此,您只需要为“Type”字段使用另一个字段格式化程序,而不是当前的“Label”格式化程序

创建新的格式化程序非常容易(特别是当您使用它作为示例时)。假设您有一个名为
entity\u reference\u link\u formatter
的模块。然后在此模块的目录中创建
src/Plugin/Field/FieldFormatter
文件夹,并将以下
EntityReferenceLinkFormatter.php
文件放在那里:

<?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;
  }

}