1
votes

I'm feeling like I must be really close, but am just a little off from rendering out the my first Drupal module correctly (though of course the next step is submitting and processing the form via ajax,guess I'm not quite out of the woods yet)

Anyways, from following along through "Pro Drupal Development 7", reading and asking some questions here on stack, I've gotten to the point where I've got my Drupal module set up.

 \sites\all\modules\custom\mymodule
  ---- mymodule.info
  ---- mymodule.css
  ---- mymodule.js
  ---- mymodule.tpl
  ---- mymodule.module

All of the requirements seem to be functioning to get the module in my blocks list, the CSS, & JS are visible on page if I choose to view source.

For testing purposes, my TPL file just has the following in it.

 <h1> HERE </h1>

The module file (from what I understand, it's the place where I write my theme hook) has the following (from what I gather) key functions.

function mymodule_theme($existing, $type, $theme, $path){
  return array(
    'mymodule' => array(
        'variables' => array('content' => "FOO"),
        'file'      => 'mymodule', 
        'path'      => drupal_get_path('module', 'mymodule') 
    )
  );
}

function mymodule_page(){

  $content = "";
  return theme('mymodule', $content); 
}

I'm pretty sure I'm just having a small, relatively simple issue - but have been hammering away for an hour on my own. Does anyone know the answer, what am I missing here ?

Thanks!

2

2 Answers

3
votes

Hey anyone who finds this in the future... As it turns out, the function that renders a view to the page in drupal is <modulename>_block-view the content of that block is what gets rendered.

function mymodule_block_view($delta = '') {
$build['mymodule'] = array(
    '#theme' => 'mymodule',
    '#title' => "FOO"
);

$block['subject'] = t('Title of second block (example_empty)');
// $block['content'] = drupal_render($build); // this was wrong
$block['content'] = $build;

return $block;

}

I found the example from downloading and stepping through the code in

http://drupal.org/project/examples

specifically unzip and install this http://ftp.drupal.org/files/projects/examples-7.x-1.x-dev.zip to /sites/all/modules and you can see live example on your own drupal site while stepping through the code.

note: the answer from NMC below was correct in that the module file had to be renamed to mymodule.tpl.php

EDIT as per comment below, the drupal_render call on the commented out line was not required. It worked in one environment, but not in another. Best not to do it for consistency.

1
votes

Try

function mymodule_theme($existing, $type, $theme, $path){
  return array(
    'mymodule' => array(
        'variables' => array('content' => "FOO"),
        'template'  => 'mymodule', 
    )
  );
}

and rename your mymodule.tpl to mymodule.tpl.php