0

I am putting a custom module together. It will take data submitted from a form from site users and store it in the DB. Admins will then be able to review the data and make changes to it. It is easy enough to display the DB data with something like:

return theme('item_list', array('header' => $header,'items' => $rows));

But I need to customize it to have custom HTML markup. Any thoughts, answers or direction?

1
  • You can literally enter anything as the theme function and its list of arguments provided by Drupal core, or, create your own via hook_theme and implement it. But this question is rather broad because its not clear on what you are actually trying to achieve beyond that.
    – Kevin
    Sep 21, 2017 at 1:23

1 Answer 1

0

There are 2 ways of doing it. First one is to define your own theme function using hook_theme and then either create a theme function of a template file inside of your module or theme folder. example code is given here

/**
 * Implements hook_theme().
 */
function MYMODULE_theme($existing, $type, $theme, $path) {
  $custom_theme = array();
  $custom_theme['my_new_theme'] = array(
    // example theme template register
    // for 
    //sites/all/modules/mymodule/templates/my_new_theme.tpl.php
    'template' => 'my-new-theme',//dash will convert to underscore in template name.
    'path' => drupal_get_path('module', 'mymodule') . '/templates',
    'variables' => array('header' => null,'rows'=>null),
  );

  return $custom_theme;
}

You can then use

theme('my_new_theme', array('header' => $header, 'rows' => $rows));

instead of your theme function call. And then inside of your my_new_theme.tpl.php you can access $header and $rows variables.

Second method is not recommended but a possible way of doing this. So you can just create your HTML output in your callback function where you are creating that list. Store your full html to be out-puted in some variable and finally just return that variable at the end. Drupal will render your HTML as it is.

1
  • Still just sorting out how exactly it the system delivers content and uses templates in themes and modules. This helped!
    – Anthony R
    Sep 21, 2017 at 15:18

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