I hope the title is ok. I've seen many discussion here about using custom templates in a module when using views, but i lack a piece of answer : how to use specific variables from an entity in that template ?

Backgound information about my problem : I'm making a module 'youtube2drupal', using a custom table 'youtube_video', having some fields (one of them is 'vhash', the video hash in youtube). With this table i create an entity 'ytv'. I added this key to mymodule_entity_info() to use with Views : 'views controller class' => 'EntityDefaultViewsController', Then to test it i create a view displaying one video with all its informations => everything is ok.

Everything BUT, to display the video, i need a iframe and i cant't use the rewrite of the output for the 'vhash' field i added in the view field.

The code needs to be :

<iframe width="560" height="315" src="//www.youtube.com/embed/<?php print $vhash; ?>" frameborder="0" allowfullscreen></iframe>

In the suggested template for the view, there is one i can use : views-view-field--vhash.tpl.php

I don't want to override any theme : i'd like to have all the code in my module. So I created this file 'views-view-field--vhash.tpl.php' in my /templates dir in my module.

I looked for solutions to tell Drupal to use it:

First, I've checked this lead, with hook_theme_registry_alter() https://swsblog.stanford.edu/blog/using-templates-custom-drupal-module

and i tried

function youtube2drupal_theme_registry_alter(&$theme_registry) {
  $module_path = drupal_get_path('module', 'youtube2drupal');
  $theme_registry['views_view_field__vhash']['theme path'] = $module_path . '/templates';
  $theme_registry['views_view_field__vhash']['template'] = 'views-view-field--vhash';

but the 'views_view_field__vhash' key doesn't exists, so it seems this is not the right solution to register this template.

Then i saw this discussion https://stackoverflow.com/questions/5305114/drupal-7-how-to-load-a-template-file-from-a-module#answer-5307126

so i tried

function youtube2drupal_theme($existing, $type, $theme, $path){
  return array(
    'views_view_field__vhash' => array(
      'variables' => array('vhash' => NULL),
      'template' => 'views-view-field--vhash',
      'path' => drupal_get_path('module', 'youtube2drupal') . '/templates'
function youtube2drupal_page(){
  // some code to generate $vhash variable : this is where i'm stuck !
  return theme('views_view_field__vhash', array('vhash' => $vhash));

=> This seems the right way to register correctly the template but i don't know how to set vhash variable in this context : i want to use something like $myentity->vhash. but i can't use $myentity = entity_load('ytv', array($entity_id)); because $entity_id is not defined in youtube2drupal_page() context.

Am i on the right track using youtube2drupal_theme() / youtube2drupal_page() to register the template for the module ? If so how can i tell Drupal to use the right value of vhash for this entity i created ?

----- Edit ------- I tried the preprocess function : I changed :

function youtube2drupal_theme($existing, $type, $theme, $path){
  return array(
    'views_view_field__vhash' => array(
      'template' => 'templates/views-view-field--vhash',
      'file' => 'includes/youtube2drupal.theme.inc',
      'base hook' => 'views-view-field',

and added in the includes/youtube2drupal.theme.inc :

function youtube2drupal_preprocess_views_view_field__vhash(&$variables, $hook) {
  $variables['vhash'] = $variables['view']->result[0]->youtube_video_vhash;
  //sets the right value into $vhash, but only for the first element

This doesn't "break" the site, but obviously the $variables['view']->result[0]->youtube_video_vhash; is valid only for the first element in the list. If i want to display 2 or more videos, the keys for $variables['view']->result should be 1, 2, ...

So, how can i dynamically set this vhash variable with the correct value ? Is it possible that way ? Thanks in advance.

1 Answer 1


Try to get the value from row object instead of view object like $variables['row']

  • I can't vote up for you since i'm new on this Drupal board, but i thumb up ! Using row IS the solution. I should have tried it . But when dpm($variables), the row bloc showed infinite recursion. I just had to dpm($variables['row']); and all is bright now. Thank you
    – Overdose
    Commented Jul 9, 2014 at 8:43
  • In any case, i can display what i need thanks to your answer, but just for my education : the way i get it, is it correct ? using mymodule_theme() to register the template hook and then use the hook preprocess function to change the variable ? Thanks anyway
    – Overdose
    Commented Jul 9, 2014 at 9:00
  • Yes, It is correct.
    – msmani
    Commented Jul 9, 2014 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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