سؤال

perhaps you have an idea how I can solve the following problem. I rewrite a field-collection-field in my template.php to change the output. Therefore I just added a new var ($my_classes) which contains a specific value. This value comes out of the field-collection. Everything works fine (my classes are added - yeah), besides the problem that I got the following error:

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

This error pops up four times, so every upcoming field(-collection) throws this error. Here is my code:

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;

I am not a programmer, so I hope you can help me out! Thanks a lot!!!

Here is the solution: The problem is, that when you try to alter the output of a field-collection you alter also the inherit fields in your field-collection which have no entity-id. so you just have to use the isset on the $classes (thanks to @Hans Nilson) AND extract the id of the entity to use it in your function. here is the solution in code:

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;
    }
هل كانت مفيدة؟

المحلول

It means that in this line:

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

key 'entity' does not exist in this $delta

You could add a check:

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

but it might make better sense to try and figure out why a particular delta doesn't have the entity key, if you believe it should be there.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top