2016-07-30 97 views
0

我在Drupal 8和node.html.twig模板中創建自定義主題我使用{{content.field_type}}來顯示當前頁面允許的類別鏈接用戶鏈接回該類別中的頁面列表。利用這一點,頁面呈現:在生成的鏈接字段上更改鏈接名稱

<a href="/drupal/main-cat/sub-cat" hreflang="en">Sub Cat Name</a>

什麼我需要做更改爲:

<a href="/drupal/main-cat/sub-cat" hreflang="en">My Custom Link</a>

回答

2

這是可能的改變使使用預處理功能陣列,但在你的情況下,它不是一個好主意。你所說的鏈接是字段格式化程序的渲染結果。所以,你只需要另一個字段格式化程序爲您的'類型'字段,而不是當前的'標籤'格式化程序。

創建新的格式化程序非常簡單(特別是如果使用EntityReferenceLabelFormatter作爲示例)。假設你有一個叫做entity_reference_link_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; 
    } 

} 

啓用這個模塊後(或清除緩存,如果這個模塊是較早啓動後),您將有「鏈接」格式化爲所有'實體引用'字段,允許您在格式化程序設置中自定義鏈接文本。