2

I've been writing a custom module to spit a content type into 2 pages, with custom fields accessible via a tab. For ease I've built the test case on a fresh d7 install and using the predefined article content type.

My module folder has the following files:

module folder view

Now, the template file is simply a copy of the bartik themes node.tpl.php file, but for some reason the module will not load it. I've read through the hook_theme() page and used the examples for developers theming_example, but I just can't get it to work.

Any help would be greatly appreciated.

-------- UPDATED CODE ----------------

    <?php
    /**
     * Implements hook_menu().
     */
    function article_page_menu() {

      $items['node/%/info'] = array(
        'title' => 'Info',
        'page callback' => 'article_page_page_callback',
        'page arguments'   => array(1),
        'access callback' => 'article_page_access_callback',
        'access arguments' => array(1),
        'type' => MENU_LOCAL_TASK, // tab instead of MENU_CALLBACK
        'weight' => '1000', // last tab item
      );

      return $items;
    }

    /**
     *  Page Callback
     */
    function article_page_page_callback($nid) {
      if (is_numeric($nid)) {
        $node = node_load($nid);

        if($node->type == 'article') {
          return theme('article_page_template', array('node' => $node));
        }

        drupal_not_found();
        return;
      }
      return FALSE;
    }

    /**
     *  Create the access callback
     */
    function article_page_access_callback($nid) {
      if (is_numeric($nid)) {
        $node = node_load($nid);
        // only show for article types
        if($node->type != 'article'){ 
          return FALSE;
        } else {
          return TRUE;
        }
      }
      return FALSE;
    }

    /**
     * Implements hook_theme().
     */
    function article_page_theme($existing, $type, $theme, $path) {
      return array(
        'article_page_template'  => array(
          'template' => 'article_page_template',
        ),
      );
    }

    function theme_article_page_template($variables) {
      $node = $variables['node'];
      $build = node_view($node);
      $output = drupal_render($build);
      return $output;
    }

------------ END UPDATED CODE -----------

--------- BELOW IS THE OLD CODE ---------------------

Here is the code from the module file:

        <?php
        /**
         * Implements hook_menu().
         */
        function article_page_menu() {

          $items['node/%/info'] = array(
            'title' => 'Info',
            'page callback' => 'article_page_load',
            'page arguments'   => array(1),
            'access callback' => 'article_page_access',
            'access arguments' => array(1),
            'type' => MENU_LOCAL_TASK, // tab instead of MENU_CALLBACK
            'weight' => '1000', // last tab item
          );

          return $items;
        }

        /**
         * Constructs a descriptive page.
         */
        function article_page_load($nid) {
          if (is_numeric($nid)) {
            $node = node_load($nid);

            if($node->type == 'article') {
              // convert object to array
              // else we get an error
              $build = node_view($node);
              $render = drupal_render($build);
              return $render;
            }
            drupal_not_found();
            return;
          }
          return FALSE;
        }

        /**
         *  Create the access callback
         */
        function article_page_access($nid) {
          if (is_numeric($nid)) {
            $node = node_load($nid);
            // only show for article types
            if($node->type != 'article'){ 
              return FALSE;
            } else {
              return TRUE;
            }
          }
          return FALSE;
        }


        /**
         * Implements hook_theme().
         */
        function article_page_theme($existing, $type, $theme, $path) {
          return array(
            'article_page_template'  => array(
              'template' => 'article_page_template',
            ),
          );
        }
3

It seems like there are quite a few elements in your module code that are used incorrectly. For example, your page callback should not be a hook (you are using hook_load()) and the callback should call theme() and invoke your custom theme function. In addition, your hook_theme() implementation is incorrect and you will also need to implement hook_theme_registry_alter() in your module to let the theme system know about your template file.

There is a good example of all this that you can base your code off of in this comment in the API for hook_theme()

Also, you seem to be mixing together the terms 'node' and 'page' so I am unsure whether you are using the terms correctly (i.e. I'm not sure if you're mistakenly using a node template as your basis when you may really want to use a page template, etc.)

  • Hi Sheena and thanks for your answer. I called the module article_page, but it should create a 1 to 1 copy of the full node view (which in the template file is defined as $page == true), but that doesn't work as the elements for if(!page){} are shown. Yes, the template file is the node.tpl.php file and I wish to display the $node and not $page content. So basically I want to display an exact copy of node/1 at the url node/1/info and with use of the template file show the specific fields I need. – tecjam Feb 9 '12 at 15:41
  • Ok, i've edited my code - it's posted above - now the template is loaded (i know because i added a bit of text in the first line), but none of the defined variables in the template present. I guess I have to call them all using $node->element. Note that I did not use hook_theme_registry_alter. It says: 'Alter the theme registry information returned from hook_theme().' Why would I need to alter the info I set in my hook_theme() function? – tecjam Feb 9 '12 at 16:02
  • 1
    I would say that your issues with creating the additional node page and redering its content are a separate issue from the template call not working and I'd suggest opening an new question for that issue. – sheena_d Feb 9 '12 at 16:28
0

You have a quick tutorial for hook_theme in D8 here if you need: http://westweb-solutions.com/blog/drupal-8-how-use-hooktheme. It's a bit more tricky, but D8 is gaining ground quickly.

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.