I've been creating pseudo fields (display-only) using the hook_field_extra_fields function as outlined in the blog post http://www.treehouseagency.com/learn/view-modes/extra-fields.

The blog post renders the output directly in code as opposed to using a template file. I've been trying to track down how I might go about rendering the output with a template file.

I wasn't sure if it would be something like field__extra-fields__some-identifier.tpl.php or if i need to add template suggestions.

Has anyone attempted this or have an idea on how to accomplish this?

Comments

nevets’s picture

Looking at the example, each extra field is defined something like

  $node->content['created'] = array(
    '#theme' => 'extra_fields_created',
    '#node' => $node,
    '#view_mode' => $view_mode
    );

The theme function would be defined in hook_theme() and can be defined to use a template file instead of a function.

caschbre’s picture

Thanks nevets! I was able to define a template file in hook_theme(). To follow the sample you posted, here's what it looks like.

/**
 * Implements hook_theme().
 */
function extra_fields_theme($existing, $type, $theme, $path) {
  return array(
    'extra_fields_created' => array(
      'variables' => array('node' => NULL, 'view_mode' => NULL),
      'template' => 'extra-field--created',
    ),
  );
}

Is there anything in addition to the 'template' option that should be included?

caschbre’s picture

So adding the 'template' item to the array allowed me to create extra-field--created.tpl.php in the module folder. However, if I create a copy of that same template file in the theme folder it's not being picked up.

Any ideas?

nevets’s picture

Did you try clearing the theme registry?

caschbre’s picture

Yep, I've run drush cc all several times.

Here's the whole code for my module.

function tw_global_field_extra_fields() {
  $extra = array();
  foreach (node_type_get_types() as $type)
    $extra['node'][$type->type] = array(
      'display' => array(
        'date_box' => array(
          'label' => t('Extra Field: Date Box: Post Date'),
          'weight' => 0,
          'description' => t('Post Date rendered as a Mon/Day box.'),
        ),
      ),
    );
  return $extra;
}
function tw_global_node_view($node, $view_mode, $langcode) {
  $node->content['date_box'] = array(
    '#theme' => 'tw_global_date_box',
    '#node' => $node,
    '#view_mode' => $view_mode,
  );
}
function tw_global_theme($existing, $type, $theme, $path) {
  return array(
    'tw_global_date_box' => array(
      'variables' => array('node' => NULL, 'view_mode' => NULL),
      'template' => 'extra-field--date-box',
    ),
  );
}

The template file extra-field--date-box.tpl.php is picked up in the module directory but not in my theme directory.

Jaypan’s picture

Visit the themes page. I've had issues with Drush clearing the entire theme registry in the past.

caschbre’s picture

I went to the themes page. I also went to admin/config/development/performance and cleared the cache there.

So from the sounds of it the code appears correct and it's just tracking down why the theme isn't picking it up?

El Bandito’s picture

Hi caschbre,

Did you ever work this one out ?

Thanks

caschbre’s picture

No, I never did... and then I got distracted with other project tasks so I haven't come back to it. :-/

bohemier’s picture

What worked for me was to let #theme be handled by field and simply change #field_name to my extra field name. The the field module would then pick my template in my theme directory.

    $summary = field_view_field('node', $node, 'body', array(
      'type' => 'text_summary_or_trimmed',
      'label' => 'hidden'
    ));
    $summary['#field_name'] = 'body_summary';
    $node->content['body_summary'] = $summary;

template file: field--body_summary.tpl.php

J-F Bohémier
Angelicode.com