4

I have installed field_collection and have a collection called field_contact, each entry of which has a field called field_contact_url

I'm trying to customise the display of that field by overriding the view template, using the file views-view-field--field-contact.tpl.php

The template is being used, if I put a simple print 'hello' I can see the hello come up, but I cannot figure out how to access the field_collection properties. I've gone through every answer and example I can find on google, but all of them seem to rely on a variable that is not there.

  • $content is not present
  • $node is not present
  • $field is present, but $field->field->id is empty
  • $items is not present

I tried using this example, but the $rows is not showing up (perhaps because I put the functions in the wrong file... I put them both in the template.php file.

I have spent 6 hours trawling through Drupal docs, would really appreciate if someone could help me figure out what variables I need or where I need to put the template file so the right variables become available.

3 Answers 3

0

This is the solution I came up with. The first line is a bit of a hack and I think is not very robust.

Also, I think i'm not using the entity_metadata "way" of accessing the contact fields - but it works for now.

Would welcome suggested improvements.

$candidate_node = node_load($row->node_node_nid);
$survey_node = node_load($row->{$field->field_alias});

$candidate_wrapper = entity_metadata_wrapper('node', $candidate_node);
$survey_wrapper = entity_metadata_wrapper('node', $survey_node);

foreach ($candidate_wrapper->field_contact->value() as $contact) {
    if (array_key_exists('und', $contact->field_type)) {
            $key = $contact->field_type['und'][0]['value'];
            $value = $contact->field_contact_url['und'][0]['safe_value'];
            print sprintf($format[$key], $key, $value, ucfirst($key), $score);
    }
}
0

Theming field collections is more like theming a node than a field since they are custom entities. I haven't tried to render a field collection as a views field, but I'm assuming it's similar to rendering it on a field level.

It'll probably give you a list of entity ID's instead handy field arrays. You'll have to load the entities from there and then render all the fields inside.

I preprocess the crap out of field collections instead of trying to handle everything in TPL's.

$fcIDs = /* array of Field Collection ID's */;
$fcRaw = entity_load('field_collection_item', $fcIDs);
// Reset array to start at 0 instead of using ID as index
  $fcData = array_values($fcRaw);

  // Figure out what fields the FC has and normalize them for optimal theming
  $fcFieldsInfo = field_info_instances('field_collection_item', $fieldName);
  $fcFieldNames = array_keys($fcFieldsInfo);
  foreach($fcData as $i => $fcDataInstance){
    foreach($fcFieldNames as $fcFieldName){
      $fcData[$i]->$fcFieldName =
        field_get_items(
          'field_collection_item',
          $fcDataInstance,
          $fcFieldName);
    }
  }

Field collections are powerful, but a bit unwieldly. I wrote myself a number of helper functions to help work with them. Hopefully this helps!

0

Define the preprocess hook (hook_preprocess_HOOK) for your view, so you can write a logic there and expose it into the variable, e.g.

/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_views_view_field(&$vars) {
  $view = $vars['view'];
  switch ($view->name) {
    case 'my_view':
      $node = entity_metadata_wrapper('node', $vars['row']->nid);
      // For single valued field.
      $field = $node->field_contact->field_contact_url->value();
      // For multi valued field.
      foreach ($node->field_contact->getIterator() as $item) {
        $vars['value'][] = $item->field_contact_url->value();
      }
    break;
  }
}

Then print your value in the template file (views-view-field--field-contact.tpl.php), e.g.

<?= $value ?>

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .