1

The code below works, but when I try to add the line in my MYMODULE-page.tpl.php the content is rendered twice with some errors. For example the $title is printed, but i get an error message that the $title is not set and when I add some test text in the template file i get it also twice. I didn`t find any solution for this.

**** EDIT ****

function MYMODULE_theme() {
  return array(
    'page-MYMODULE' => array (
      'path' => drupal_get_path('module', 'MYMODULE') . '/templates',
      'template' => 'page-MYMODULE',
      'render element' => 'page',
    ),
    'content-MYMODULE' => array (
      'path' => drupal_get_path('module', 'MYMODULE') . '/templates',
      'template' => 'content-MYMODULE',
      'render element' => 'page',
      'variables' => array (
        'title' => NULL,
        'path' => NULL,
      ),
    ),
  );
}

function MYMODULE_preprocess_page(&$variables, $hook) {  
  if (arg(0) == 'MYMODULE') {
    $variables['theme_hook_suggestions'][] = 'page-MYMODULE'; //underscores doesn't work for me
  };
}

function MYMODULE_menu() {
  $items = array(
    'MYMODULE' => array(
      'title' => 'Page Title',   // -> page-MYMODULE.tpl.php
      'page callback' => 'MYMODULE_page_main',
      'access callback' => TRUE,
      'type' => MENU_CALLBACK,
    ),
  );
  return $items;
}

function MYMODULE_page_main() {
  $path = drupal_get_path("module", "MYMODULE");
  $page = array();

  $page['MYMODULE'] array(
    '#theme' => 'page-MYMODULE',
    '#path' => '$path',
    '#title' => 'Content Title',  // -> content-MYMODULE.tpl.php
    '#attached' => array (
      'css' => array (
      'type' => 'file',
      'data' => $module_path.'/css/layout.css', //works only if layout.css was disable by hook_css_alter()
      'weight' => 'CSS_THEME',
     ),
  );
  return $page;
}

function MYMODULE_css_alter(&$css){
  if (arg(0) == 'MYMODULE') {
    foreach($css as $key => $val){
      if ($val['group'] == '100') {
        unset($css[$key]);
      };
    };
  }
}

page-MYMODULE.tpl.php

<?php print $title; ?>

<div id="content">
  <?php print render($page['content']); ?>
</div>

content-MYMODULE.tpl.php

<?php print $title; ?>
<?php print $path; ?>
2
  • What does your template look like? I have a theory, that you're basically calling the template twice, but I can't be sure without seeing the template and asking more questions. But basically, if you have a $page['content'] or similar region and it's being replaced with the entire contents of MYMODULE-page again, I'd say that your _page_MYMODULE() callback is the problem. – Erin McLaughlin Oct 23 '15 at 18:03
  • Ok, i know now why the tpl.php is duplicatetd! The function MYMODULE_preprocess_page(&$variables, $hook) rendered it twice. Have you any idea how i can avoid it? – Bruno Oct 26 '15 at 12:31
1

I believe that what gets returned in _page_MYMODULE() ends up being the contents of $page['content']. What you want to do is have a completely separate template for _page_MYMODULE() to return that outputs what you want to have in the area of where $page['content'] is output. Does that make sense?

Edit:

<?php print render($page['content']);?>

will render everything in whatever file the theme function in the array that you return in _page_MYMODULE() uses. This means that you want to have a different theme function and different file than you used for the page template. Something like MYMODULE-results.tpl.php.

So, to do that, you need to add a second theme in hook_theme(), so that the function looks something like this:

function MYMODULE_theme() {
  return array(
    'MYMODULE-page' => array(
      'path' => drupal_get_path('module', 'MYMODULE') . '/templates',
      'template' => 'MYMODULE-page',
      'render element' => 'page',
    ),
    'MYMODULE-results' => array(
      'path' => drupal_get_path('module', 'MYMODULE') . '/templates',
      'template' => 'MYMODULE-results',
      'variables' => array(
        'results' => array(),
      ),
    ),
  );
}

You'll notice that, instead of having a render element, you have variables. This is an array of variables that you're going to pass when you call theme() that you can then use in the template by using $results. It is currently set to default to an empty array, so if you don't pass anything as the results variable, you'll still have an empty array in your template to use, so things won't error out.

Next, you'll need to use this new theme in your _page_MYMODULE() function. That would look something like this:

function _page_MYMODULE() {
  // Some sort of DB query that returns results, such as:
  $results = db_query("SELECT * FROM node")->fetchAll();

  return theme('MYMODULE-results', array('results' => $results));
}

Then, in your MYMODULE-results.tpl.php file, you can loop through the $results array and add whatever HTML you want to surround each result.

Does this make sense?

6
  • You mean with en extra tpl for node and so on? i dont know! I try many tutorials and read in 2 books, but i cant find out the issue. One thing i will try next is the path. i think a % is needed in there. – Bruno Oct 23 '15 at 18:54
  • What, exactly, are you trying to accomplish with your MYMODULE page? Like, are you displaying custom HTML? Are you trying to embed a node based on the path? Are you just trying to create a page wrapper for every page on the site? – Erin McLaughlin Oct 23 '15 at 18:59
  • I want to display a database query in a seperate theme and maybe later a block to change some variables in it. It's only for me to learn more about the CMS. So i run in this issue and want to solve it, but it' s not necasary for my main goal. – Bruno Oct 23 '15 at 19:13
  • Okay, cool! So you're definitely on the right track and what I said above applies. I'm going to edit my answer with more details for what you're trying to do, though. Hope it will help! – Erin McLaughlin Oct 23 '15 at 19:17
  • Thx to you ... really! But i still run in the same issue. Now the $page['content'] still print all twice. The variables out of 'varibales' in page callback will shown, but the variables in the template file creates error messages. If i call the varibales directly in the template, then comes nothing. I will edit the code in my article tomorrow. Here is one link wich i worked out. github.com/duke-libraries/drupal-dul-timeline/wiki/…! – Bruno Oct 23 '15 at 20:38
1

You haven't passed a variable named $title to the template, so one won't be available.

To do so, add variables to your hook_theme implementation:

function MYMODULE_theme() {
  return array(
    'MYMODULE-page' => array(
      'path' => drupal_get_path('module', 'MYMODULE') . '/templates',
      'template' => 'MYMODULE-page',
      'render element' => 'page',
      'variables' => array('title' => ''),
      ),
    ),
  );
}

and in your page callback:

return array(
  '#theme' => 'MYMODULE-page',
  '#title' => 'Title here',
);
1
  • Thx, i write my template file to the post, with some explanations. Maybe i have basal issue in my script. The drupal installation is new and i add only the custom module to it. – Bruno Oct 23 '15 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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