2017-05-24 70 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