1

I in the middle of creating a content type with multiple fields. All the fields need to have all the extra markup removed, except for a specific 3 and I would like these to actually be wrapped in <h3> tags.

I've copied the field.tpl file into my theme and renamed to be used just for this specific content type, which is working fine with this code:

  <?php if (!$label_hidden): ?>
  <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?> </div>
  <?php endif; ?>

  <?php foreach ($items as $delta => $item): ?>
  <?php print render($item); ?>    <?php endforeach; ?>

My trouble is that I have no idea the syntax I need to target a specific field. So I'm im guessing once I figure out how to target the specific field, I can just use:

  <?php foreach (something here to find the right field): ?>
  <h2><?php print render($item); ?></h2    <?php endforeach; ?>

What would that be if my field name was field_sizing_title?

I found the Field Wrappers Module, but it seem like overkill and I really like to learn more about how to work with the field.tpl file

2

You need identify the field machine name. For example if you have a field called Name and have a machine name field_name the field tpl name should be field--field-name.tpl.php.

See this thread as well as it provides a comprehensive guide on how to override field template using hook_theme and preprocess function.

One of the most handy helper I used is $conf['theme_debug'] = TRUE; in settings.php. It will show a template suggestion in HTML source.

| improve this answer | |
  • Do I have to have a separate field specific field.tpl file? I already have the one that's targeted to that specific content type. Seems I should be able to use that to target individual fields throughout that content type. – Erik Hanson Aug 28 '15 at 23:24
  • No need to have individual tpl for each field as you are not overriding them. This only applies to specific field that you want to override. – Gulok Aug 28 '15 at 23:26
  • So if I have 3 fields I want to override, I need 3 seperate tpl files? I cannot override those 3 individually in the one content type focused field.tpl file? – Erik Hanson Aug 28 '15 at 23:29
  • In my experience given your scenario, yes I will have 3 separate tpl files. I would avoid mixing them for maintainability. I might be wrong with your use case. – Gulok Aug 28 '15 at 23:32
  • So, this didn't work for me. I tried adding the fieldname--field.tpl file and it never altered the output. I checked and triple checked naming conventions. Is there a priority of themeing here? I didn't delete my node--field.tpl while I was working on the field specific file. – Erik Hanson Aug 31 '15 at 23:51
1

To find the right field, you should use template_process_HOOK to load it first and expose into your template file as variable.

Here is example using with Entityforms:

/**
 * Implements template_process_HOOK().
 */ 
function mymodule_preprocess_myfieldhook(&$variables) {
  $entity = $variables['form']['#entity'];
  $field_name = $variables['form']['#field_name'];
  $variables['info'] = field_info_field($field_name);
  $variables['instance'] = field_info_instance($entity->entityType(), $field_name, $entity->type);
}

Then you should have in your TPL file two extra variables: $info and $instance where you can access some field settings.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.