Skip to content

Instantly share code, notes, and snippets.

@daggerhart
Last active August 4, 2020 05:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daggerhart/d6b3dd619c65cb1efb87e8bb7a6f2164 to your computer and use it in GitHub Desktop.
Save daggerhart/d6b3dd619c65cb1efb87e8bb7a6f2164 to your computer and use it in GitHub Desktop.
Simple Drupal 7 hook_theme() and render array examples
<?php
/**
* Implements hook_theme()
* @return array
*/
function mymodule_theme(){
$items = array();
$items['mymodule_custom_template'] = array(
'path' => drupal_get_path('module', 'mymodule'),
'template' => 'mymodule_custom_template',
'variables' => array(
'account' => NULL,
'name' => '',
'mail' => '',
),
);
return $items;
}
/**
* Example usage within an arbitrary function
*/
function _mymodule_example_usage(){
global $user;
print theme( 'mymodule_custom_template', array(
'account' => $user,
'name' => format_username( $user ),
'mail' => $user->mail,
);
}
/**
* Implements hook_preprocess_HOOK()
* In this case HOOK is "mymodule_custom_template". The name of our custom template's key in the above array.
*/
function mymodule_preprocess_mymodule_custom_template( &$variables ){
$account = $variables['account'];
// link the name to the user's page
$variables['name'] = l( $variables['name'], "user/{$account->uid}");
}
/**
* Implements hook_preprocess_HOOK()
*
* Render array examples attached to the main page content.
*
* @param $variables
*/
function mymodule_preprocess_page( &$variables )
{
// default render array #type is markup which accepts any HTML in
// the #markup property
$variables['page']['content']['my_render_array1'] = array(
'#markup' => '<p>Arbitrary HTML goes here</p>',
);
// example render array type 'html_tag'
$variables['page']['content']['my_render_array2'] = array(
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => 'Find Me',
);
// render array that uses a theme hook
// variables are passed to the function as #-prefixed properties
global $user;
$variables['page']['content']['my_render_array3'] = array(
'#theme' => 'mymodule_custom_template',
'#account' => $user,
'#name' => format_username( $user ),
'#mail' => $user->mail,
);
}
<?php
/**
* Available variables
*
* $account object - drupal user account
* $name string - the account's formatted and linked name
* $mail string - the account's email address
*/
?><p><strong>Name: <?php print $name; ?></p>
<p><strong>Email: <?php print $mail; ?></p>
@alexanderzatko
Copy link

very useful. thanks.

@robertfy
Copy link

Great! but you need to update something:

1- Add this line
$account = $variables['account'];

before this one

$variables['name'] = l( $variables['name'], "user/{$account->uid}");

2- You need to add a hook_menu item to show your output, just use _mymodule_example_usage

something like:

function mymodule_menu() {
  $items = array();
  $items['a-relative-url'] = array(
    'title' => "Using hook_theme",
    'page callback' => '_mymodule_example_usage',
    'access callback' =>  TRUE,
    'access arguments' =>  TRUE,
    'type' => MENU_CALLBACK,
  );
  
  return $items;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment