Hi every body, i know how to make a module with a node . However, how can i make a module with many nodes ? For example, i want to make a module company with 7 nodes (buy,sell,news,fake news,..). All nodes will be presented in the same page like tabs. How can i do that ? Thanks for your supporting.

Comments

Suuch’s picture

See api.drupal.org and read up on hook_node_info.

Suuch Solutions supports The GhanaThink Foundation

nadongtae’s picture

I read that but the information is so general. Soory,but can you give an simple example that 1 module can have 1 more node. Thank you very much !!!

dwees’s picture

Here is an example:

/*
 *  Implementation of hook_nodeinfo
 */
function mymodule_node_info() {
  return array( 
    'banana' => array( 
      'name' => t('Banana'), 
      'module' => 'mymodule', 
      'description' => t("A yellow fruit often stereotypically eaten by monkeys."), 
    ),
    'apple' => array( 
      'name' => t('Apple'), 
      'module' => 'mymodule', 
      'description' => t("A green or red fruit usually eaten with a loud crunchy sound."), 
    )
  );
}

/*
 *  Implementation of hook_view
 */
function mymodule_view($node, $teaser = FALSE, $page = FALSE) {
  // Theme your node in here.
  // Will need to split the logic up based on node types.
  if ($node->type == 'apple') {
    // Logic here for the apple node type
  }
  if ($node->type == 'banana') {
    // Logic here for the banana node type
  }
  return $node;
}

And so each hook related to a node needs to have its logic split in the same way.

Dave

dwees’s picture

ff1’s picture

Thanks for the above info. Is this the only way to achieve multiple node types in a single module?

I currently have a module written for drupal 4.6. This uses separate hooks for each node type, so the view code is like this:

<?php
/**
 * Implementation of hook_node_types().
 */
function mymodule_node_types() {
  return array('apple', 'banana');
}

/**
 * Implementation of hook_node_name().
 */
function mymodule_node_name($node) {
  return t('My Module');
}

/**
 * Implementation of hook_node_name().
 */
function apple_node_name($node) {
  return t('Apple');
}

/**
 * Implementation of hook_node_name().
 */
function banana_node_name($node) {
  return t('Banana');
}

/*
*  Implementation of hook_view
*/
function apple_view(&$node, $main = 0, $page = 0) {
  // Theme your node in here.
    // Logic here for the apple node type
  return $node;
}

/*
*  Implementation of hook_view
*/
function banana_view(&$node, $main = 0, $page = 0) {
  // Theme your node in here.
    // Logic here for the banana node type
  return $node;
}
?>

I know that logic for hook_node_types() and hook_view() has changed since 4.6 (and I have updated my module accordingly), but can you still use separate hooks for each node type? I can't find anything in the documentation that tells me that this has changed since 4.6. My module defines 4 different node types, so some hooks such as hook_form() could end up being very complex. Also, I have each node type defined in separate include files to make them easier to maintain. If I must combine the hooks, I can no longer use the include files.

Your help is greatly appreciated.

dwees’s picture

My impression is that if you look at the function 'module_invoke_all' what you'll see that it has a foreach loop in it, and that it calls the name of each module using 'module_implements' which apparently creates an array of every module that is using a particular hook, and calls each hook.

If we look at 'module_implements' we can see that is starts a list of all of the modules, and uses 'module_hook', presumably to check to see if a particular hook is implemented in the particular module, and if we check that last function, we can see that is actually what it checks.

So this means that unless 'apple' is in the list of modules in your directory, 'apple_form' won't be called.

This means that the way you currently have it set up shouldn't work.

Dave

ff1’s picture

Thanks for that info Dave. I'll have to update my code.

Maybe it was just luck it ever worked in the first place! :)

Ian
Fantasy Formula 1