Field template in Drupal.

templating

What is field templating

Some time we need to change the layout/design of a field. We can create a complete node–contetntype.tpl But it is not the best practice. If we need to design a single field the we need to create a field template file.

How to do this???

Suppose we have a field departments and user can be select multiple departments and we need to display this departments field in a table format with some different styles. Then we need to create a field template file.

How to do this

We need to create a file under theme folder i.e. project_name/sites/all/themes/theme_name/ .

What should be the name of this file.

we will have to create a file with fiels–field_name.tpl.php. Suppose my field name is ‘field-department-list‘ then my file name will be field–field-department-list.tpl.php and the complete path for this file will be project_name/sites/all/themes/theme_name/field–field-department-list.tpl.php

<?php if (!$label_hidden): ?>
    <div class="field-label"<?php print $title_attributes; ?>><h4>Department Info</h4></div>
  <?php endif; ?>
<table class="field-group-format group_additional">
  <thead>
    <tr>
      <th>Field</th>
      <th>Value</th>
    </tr>
  </thead>
  <tbody>

  <div class="field-items"<?php print $content_attributes; ?>>
    <?php foreach ($items as $delta => $item): ?>

<tr>
    <th class="field-label">Department</th>
    <td class="field-items">
      <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
    </td>
    </tr>
  <?php endforeach; ?>
</div>
  </tbody>
  </table>

1 Comments

Leave a Comment.