0

也许你有一个想法,我可以如何解决以下问题。我在我的template.php中重写了字段集合字段以更改输出。因此,我只是添加了一个新的var($ my_classes),其中包含一个特定的值。该值来自字段集合。一切工作正常(我的班加 - 耶),除此之外,我得到了以下错误的问题:字段集合输出:未定义的索引:template_field__custom_field中的实体drupal 7

Notice: Undefined index: entity in template_field__field_fc_page_fields() (line 333 of ..

此错误弹出四次,所以每一个即将进行的实地(-collection)抛出这个错误。这里是我的代码:

function template_field__field_fc_page_fields($variables) { 
kpr($variables); 
$output = ''; 

// Render the label, if it's not hidden. 
if (!$variables['label_hidden']) { 
    $output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ': </div>'; 
} 

// Render the items. 
foreach ($variables['items'] as $delta => $item) { 
// Custom class 
    $my_classes = $variables['items'][$delta]['entity']['field_collection_item'][$delta+1]['field_layout']['#items'][0]['value']; 

    $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even'); 
    $output .= '<div class="' . $classes . ' ' . $my_classes .'"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>'; 
} 
// Render the top-level DIV. 
$output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>'; 

return $output; 

我不是程序员,所以我希望你能帮助我!非常感谢!!!

这里是解决方案: 的问题是,当你尝试改变现场采集的输出也改变你的领域收集在继承领域不具有实体ID。所以你只需要使用$ classes上的isset(感谢@Hans Nilson)并提取实体的id以在你的函数中使用它。这里是代码的解决方案:

function template_field__field_fc_page_fields($variables) { 
     // kpr($variables); 
     $output = ''; 

     // Render the label, if it's not hidden. 
     if (!$variables['label_hidden']) { 
      $output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ': </div>'; 
     } 
     // Render the items. 
     foreach ($variables['items'] as $delta => $item) { 
      if (isset($variables['items'][$delta]['entity']) && (isset($variables['element']['#items'][$delta]['value']))) { 
       $fc_id = ($variables['element']['#items'][$delta]['value']); 
      $my_classes = $variables['items'][$delta]['entity']['field_collection_item'][$fc_id]['field_layout']['#items'][0]['value']; 
      } 
      if (isset($variables['items'][$delta]['entity'])) { 
       $classes = 'field-item-custom ' . $my_classes . ' ' . ($delta % 2 ? 'odd' : 'even'); 
      } 
      else { 
       $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even'); 
      } 
      $output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>'; 
     } 
     // Render the top-level DIV. 
     $output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>'; 

     return $output; 
    } 

回答

0

这意味着,在这条线:

$my_classes = $variables['items'][$delta]['entity']['field_collection_item'][$delta+1]['field_layout']['#items'][0]['value']; 

键“实体”并不在此存在$三角洲

你可以添加一个检查:

if (isset($variables['items'][$delta]['entity'])) { } 

但它可能会更好地理解,试图找出为什么特定的三角洲没有实体的关键,如果你beli前夕它应该在那里。

+0

但它存在,我的意思是,所有的值都从这个var中获得,如果$ delta不存在,我应该得到我的值吗?我想我只是缺少一些东西...... – jci 2013-03-05 10:48:45

+0

$增量可能存在,但它没有关键'实体'。 – 2013-03-05 10:51:44

+0

没关系,$ delta只是一个数字,我可以用像i = 0这样的自定义变量替换它,并通过项目数组计算,但这也不起作用。 – jci 2013-03-05 10:58:33