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

相關問題