Hi!

I'm building a module that fetches data from WebServices using cURL and then renders HTML using that data.

In the hook_menu implementation I've defined path(s) containing a "auto-loader" wildcard as stated on http://api.drupal.org/api/drupal/modules--system--system.api.php/functio...
For example, I'm doing something like this:

<?php
function stuff_menu() {
  $items['stuff/%stuff'] = array(
    'page callback' => 'stuff_page_view',
    'page arguments' => array(1),
  );
  return $items;
}
  
function stuff_load($name) {
  $stuff = stuff_api_load($name);
  if ($stuff === FALSE) {
    return FALSE;
  }
  
  return (object) $stuff;
}
?>

This seems to work great. What confusing me is that hook_load seems to be a hook in the node module or has something to do with the entity API.
I'm not making use of either of thoose in my module, still the name "stuff_load" confirms to the rules for being an implementation of hook_load?

Also, in "stuff_page_view" (menu callback) I want to do something similar to what the node module does (letting other modules hook in to modify the renderable array before being rendered).

My first thought was to do something like this:

 <?php
function stuff_page_view($stuff) {
  $stuff->content = array();
   
  $stuff->content['description'] = array(
    '#theme' => 'description',
    '#desc' => $stuff->description,
   );
   
  // Let other modules modify $stuff->content
  module_invoke_all('stuff_view', $stuff);
}
?>

But, then again, as with hook_load, hook_view seems to have something do to with the node module or the entity API.

Can I use the auto-loader "stuff_load" and "stuff_view" function without using the Entity API or node module without getting in to any trouble?

I'm thankful for any recomendations or suggestions on how to do this the best way. Links to other modules doing something similar is also of great interest.

Comments

jaypan’s picture

The simple answer to your question is yes.

Wildcard loaders are different from hook_load(), and your implementation of stuff_view_page() is not an implementation of hook_view().

If you implement hook_node_info() and define a content type in your module, you will run into issues, but if you are not implementing a node type in your module then you are fine, as these hooks are only called in modules that define content types.

Contact me to contract me for D7 -> D10/11 migrations.

rolodmonkey’s picture

This is correct, but in order to prevent confusion, and possible future collisions, you may want to make your dynamic function name more distinct:


function stuff_menu() {
  $items['stuff/%stuff_name'] = array(
    'page callback' => 'stuff_page_view',
    'page arguments' => array(1),
  );
  return $items;
}
function stuff_name_load($name) {
  $stuff = stuff_api_load($name);
  if ($stuff === FALSE) {
    return FALSE;
  }
  return (object) $stuff;
}

--

Read more at iRolo.net

mklynx’s picture

I have a entity called zz_products. I want to display that with a auto-loader wild card.
I have the following code

function zazzle_menu() {

  $items = array();
  $items['zazzle/%zazzleprod'] = array(
    'title callback' => 'zazzle_page_title',
    'title arguments' => array(1),
    'page callback' => 'zazzle_page_view',
    'page arguments' => array(1),
  );
}

function zazzleprod_load($id, $reset = FALSE) {

	  $result = entity_load('zz_products', array($id));
	  return $result ? reset($result) : FALSE;

}

But i am getting the error "The requested page could not be found."
What am i missing.

Marcel

nancydru’s picture

Did you clear the menu cache?

pdcarto’s picture

... I wrote a hook_menu and forgot to return the $items array...