2016-07-30 3 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

그것은 전처리 기능을 사용하여 배열을 렌더링 변경하는 것이 가능하지만 경우에 그것의 좋은 생각이 아닙니다. 당신이 말하는 링크는 필드 포맷터를 렌더링 한 결과입니다. 따라서 현재 'Label'포맷터 대신 '유형'필드에 대해 다른 필드 포맷터가 필요합니다.

새 형식 기 작성은 매우 쉽습니다 (예 : 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; 
    } 

} 

를, 당신은 '링크'포맷을해야합니다 모든 'Entity reference'필드에 대해, 포맷터 설정에서 링크 텍스트를 사용자 정의 할 수 있습니다.