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 Answers
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.
-
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. Apr 2, 2015 at 10:25
-
How can we override body field of node form if there are many node types? Dec 11, 2015 at 12:29
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 ?>: </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.