By atb


2012-12-19 15:42:23 8 Comments

Instead of adding tpl files (in this case, media-youtube-video.tpl.php) to the theme. How can I override field templates from a custom module? Including when the field is used in a View.

2 comments

@David 2015-07-27 08:21:44

You can also decalare new theme in module this way:

/**
* Implements hook_theme().
*/
function yourmodule_theme($existing, $type, $theme, $path) {
  $theme = array();

  $theme['field__field_nameofyourfield'] = array(
    'render element' => 'content',
    'base hook' => 'field',
    'template' => 'field--field-nameofyourfield',
    'path' => drupal_get_path('module', 'yourmodule') . '/templates',
  );

  return $theme;
}

Then put in a /template directory file that contains field template like this (standard) and name it field--field-nameofyourfield.tpl.php:

<div class="<?php print $classes; ?>"<?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 class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
    <?php endforeach; ?>
  </div>
</div>

After clearing cache your theme will use this filed template unless it's not overriden by theme itself, which means you can still override this template in your theme.

@Amarnath Ravikumar 2012-12-19 16:28:07

I'm sure there must be an easier way to do this but this is what I typically do:

1. Register an overriding theme implementation with the Drupal theme registry. So, in mymod_theme(), add a new item. The variables key must match that of the media_youtube_video theme i.e.

/**
 * Implements hook_theme().
 */
function mymod_theme() {
  return array(
    'my_media_youtube_video' => array(
      'variables' => array('uri' => NULL, ...), // see media_youtube_theme() for this
      // bundle the template file with the module itself
      // i.e. theme/my-media-youtube-video.tpl.php
      'template' => 'my-media-youtube-video',
      'path' => drupal_get_path('module', 'mymod') . '/theme
    )
  );
}

2. Add a preprocess hook for the original theme implementation and suggest your new implementation here.

/*
 * Implements hook_preprocess_media_youtube_video().
 *
 * Or more generally, hook_preprocess_THEME().
 */
function mymod_preprocess_media_youtube_video(&$variables) {
  // If your overriding implementation is not a template but 
  // is implemented in a different file, 
  // then remember to include the file explicitly at this point..
  $variables['theme_hook_suggestions'][] = 'my_media_youtube_video';
}

Suggestions are evaluated by the theme system in a LIFO fashion. You can read more about this here.

Suppose you are aware that another module is also following the same approach as this one to override the implementation, then you can implement hook_module_implements_alter() and force your hook_preprocess_THEME() (see above) to be called last. You can read about hook_module_implements_alter() here.

This holds good for Views also. In summary, you just need to find out the correct unique name of the original theme implementation you want to override (typically defined in the source module), add a preprocess hook and add your overriding suggestion there.

@Fazeela Abu Zohra 2015-04-02 10:25:42

In my custom module, I've a select list for selecting the template to apply. I have 3 tpl files. So How can I do this.

@Sukhjinder Singh 2015-12-11 12:29:51

How can we override body field of node form if there are many node types?

Related Questions

Sponsored Content

2 Answered Questions

1 Answered Questions

How to override template for view mode of a entity within a module?

  • 2014-05-26 11:47:14
  • Codium
  • 530 View
  • 1 Score
  • 1 Answer
  • Tags:   7 theming entities

1 Answered Questions

2 Answered Questions

[SOLVED] Override custom module template

  • 2017-08-13 19:23:29
  • Elros Romeo
  • 300 View
  • 0 Score
  • 2 Answer
  • Tags:   7 theming

1 Answered Questions

[SOLVED] how can i define a maintenance template within a custom module?

  • 2017-05-15 15:50:50
  • Matoeil
  • 58 View
  • 0 Score
  • 1 Answer
  • Tags:   7 theming

0 Answered Questions

Override Drupal views fields module with custom module

2 Answered Questions

[SOLVED] How do I override a module template (.tpl.php) from my custom subtheme?

  • 2015-04-03 03:48:06
  • sker
  • 868 View
  • 0 Score
  • 2 Answer
  • Tags:   theming

2 Answered Questions

[SOLVED] Override default table template In custom module

  • 2014-11-12 02:36:49
  • Ward
  • 506 View
  • 1 Score
  • 2 Answer
  • Tags:   7 views theming

2 Answered Questions

Custom module template override not loaded

  • 2014-09-10 13:26:23
  • Thony
  • 108 View
  • 0 Score
  • 2 Answer
  • Tags:   7 blocks theming

1 Answered Questions

Sponsored Content