0

When fields are rendered they have classes. For example here is the template for a taxonomy term reference field:

<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <?php if (!$label_hidden) : ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>
    <?php foreach ($items as $delta => $item) : ?>
      <div style="display:inline;" class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>>
        <?php 
          print render($item);
          // Add comma if not last item
          if ($delta < (count($items) - 1)) {
            print ','; 
          }
        ?>
      </div>
    <?php endforeach; ?>
  </div>
</div>

In the first line you'l see <?php print $classes ?>, in that $classes string there are some classes and I don't want. Obviously I can use php to str_replace in the tpl file to remove the classes I want, but that seems like a very wrong solution. What's the correct way to modify $classes?

1

You can try to redefine theme function or catch and remove these classes in hook_form_alter (in some cases, classes are available only in after_build function).

2

You can implement hook_preprocess_field() in your template.php and remove the classes you want after checking the field name .

Update: As @tenken suggested you can also look into Display Suite module.

  • hmm ... or use Display Suite, or Fences which is much more lean of a module. :) – tenken Jul 1 '14 at 5:17
  • @tenken I know that we can add CSS classes using Display Suite, but can we remove it? – wolverine Jul 1 '14 at 5:21
  • 1
    both Fences and DS provide their own ability of a full reset for a field template -- eg you may control all the markup generated by drupal. For DS see this page drupal.org/node/1795368. Op asked a specific question "how do I remove a class" you can implement a ton of little functions to meet all your needs, or leverage a module like Fences or DS with a ton of flexability built in. – tenken Jul 1 '14 at 5:25
  • @tenken I do agree that it is better to use a module like Display Suite or Fences, which gives lot of features. But he was asking about removing certain classes and moreover doing a full reset means that you have to write your own CSS to style it, right? – wolverine Jul 1 '14 at 5:40
  • no you can use DS to add CSS classes to any field template. you just need to define those classes, then per view mode of the field you can assign any extra classes you want (in addition to the default drupal classes) see: drupal.org/node/1795368 at the bottom of the page, it's all done through the UI with no hand-made php. – tenken Jul 1 '14 at 5:44
1

You can use either of this options

  • hook_preprocess_field() in the template.php of your theme where hook is your theme name
  • Creating a template file in your theme with the same code (that you have mentioned above) with your customisation in the html.

e.g. We have user.tpl.php in core folders, we can override it by putting this file in our theme folder with our changes.

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.