2017-05-24 145 views
0

我需要渲染字段集合中的單個字段,以便根據特定條件調整字段順序,字段內容,刪除包裝DOM節點等。渲染單個FieldCollectionItemEntity字段

我一直在閱讀this thread,因爲它似乎是關於此問題的最佳資源,但我無法弄清楚爲什麼此字段集合渲染器不起作用。

它只是輸出什麼。

node--component-icon-promo.tpl.php

<? if (!empty($content['field_icon_promo_items'])) : ?> 

    <div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>> 

    <?php foreach ($content['field_icon_promo_items']['#items'] as $entity_uri): ?> 
     <?php 
     $item = entity_load('field_collection_item', $entity_uri); 
     ?> 

     <?php foreach ($item as $item_object): ?> 

     <?php print render($item_object->field_cta); ?> 

     <?php endforeach; ?> 
    <?php endforeach; ?> 

    </div> 

<? endif; ?> 

添加權之前print renderdpm($item_object);輸出這一點。

Field output

而改變<?php print render($item_object->field_cta); ?><?php print render($item_object); ?>只是導致錯誤。

Recoverable fatal error: Object of class FieldCollectionItemEntity could not be converted to string in include() (line 99 of /var/www/html/sites/all/themes/xerox/templates/node--component-icon-promo.tpl.php).

我知道那是因爲$ item_object就是這樣,一個物體render doesn't like。 API的字段集合位似乎是一團糟。

任何幫助將不勝感激。

回答

1

會建議使用這樣的方式,使用entity_metadata_wrapper:

<? if (!empty($content['field_icon_promo_items'])) : ?> 

    <div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>> 

    <?php foreach ($content['field_icon_promo_items']['#items'] as $entity_uri): ?> 
     <?php 
     $item = entity_load('field_collection_item', $entity_uri); 
     ?> 

     <?php foreach ($item as $item_object): ?> 

     <?php 
      $wrapper = entity_metadata_wrapper('field_collection_item', $item_object); 
      $field_cta = $wrapper->field_cta->value(); 
      //render your values with your own html or using theme_link function 
     ?> 

     <?php endforeach; ?> 
    <?php endforeach; ?> 

    </div> 

<? endif; ?> 

關於元數據封裝訪問更多的幫助this link

+0

很喜歡我需要它這個代碼沒有工作,但metadata包裝將我推向了正確的方向! – Hawxby