Hello,

In one of my node types, I display 5 blocks. These blocks display information from master node. So I loaded the array $node in these blocks by module.

This is precisely where the problem lies. To get to transmit the information node in the blocks, I used the function hook_preprocess() by adding the line: $node = node_load(arg(1)); the problem is that it creates me an error in taxonomy and especially that node_load() works with all types of node.

It should therefore that I can add one if ($node->type == 'modele_2r') {}

I think that there should exist a special function to load a node in a block?

I hope you can help me!

That's part of my module that I load the 5 blocks to the node:

<?php
// $Id$

/
* Permissions du module
* @return array Tableau de permissions valides pour le module helloworld
*/
function modele_2r_perm() {
  return array('access modele_2r content');
}

/
* Implémentation de hook_block
*/
function modele_2r_block($op = 'list', $delta = 0, $edit = array ()) {
   switch($op) {
      case 'list':
         return array('modele_2r_presentation_block'=>array('info'=>'Modele 2R: Block Presentation'), 'modele_2r_fiche_technique_block'=>array('info'=>'Modele 2R: Block Fiche Technique'), 'modele_2r_coloris_block'=>array('info'=>'Modele 2R: Block Coloris'), 'modele_2r_galerie_photos_block'=>array('info'=>'Modele 2R: Block Galerie photos'), 'modele_2r_argus_block'=>array('info'=>'Modele 2R: Block Argus'));
      case 'view': {
         if ($delta=='modele_2r_presentation_block') {

               return array(
                    'subject'=>'Presentation du modele',
                    'content'=>theme('modele_2r_presentation_block', $node));
         }
         if ($delta=='modele_2r_fiche_technique_block') {
               return array(
                    'subject'=>'Fiche technique du modele',
                    'content'=>theme('modele_2r_fiche_technique_block', $node));
        }
         if ($delta=='modele_2r_coloris_block') {
               return array(
                    'subject'=>'Coloris disponible pour ce modele',
                    'content'=>theme('modele_2r_coloris_block', $node));
      }       
         if ($delta=='modele_2r_galerie_photos_block') {
               return array(
                    'subject'=>'Galerie photos pour ce modele',
                    'content'=>theme('modele_2r_galerie_photos_block', $node));
         }
         if ($delta=='modele_2r_argus_block') {
               return array(
                    'subject'=>'Argus pour ce modele',
                    'content'=>theme('modele_2r_argus_block', $node));
       }
   }
   }
}

function modele_2r_preprocess(&$variables, $hook ) {
  if(arg(0) == 'node') {
    $node = node_load(arg(1));
    $variables['node'] =  $node;
  }
}

function modele_2r_theme($existing, $type, $theme, $path) {
   return array(
      'modele_2r_presentation_block' => array(
          'arguments' => array('node'),
          'template' => 'modele_2r_presentation_block'),
      'modele_2r_fiche_technique_block' => array(
          'arguments' => array('node'),
          'template' => 'modele_2r_fiche_technique_block'),
      'modele_2r_coloris_block' => array(
          'arguments' => array('node'),
          'template' => 'modele_2r_coloris_block'),
      'modele_2r_galerie_photos_block' => array(
          'arguments' => array('node'),
          'template' => 'modele_2r_galerie_photos_block'),
      'modele_2r_argus_block' => array(
          'arguments' => array('node'),
          'template' => 'modele_2r_argus_block')
  );
}

Comments

nevets’s picture

Some thoughts.

First, modele_2r_preprocess is called with different values of $hook, you probably want modele_2r_preprocess_block (though see next point.

It seems inappropriate to use a preprocess function for this, I would use something like

      case 'view':
						if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
							$node = node_load(arg(1));
							if ($node->type == 'modele_2r') {
								if ($delta=='modele_2r_presentation_block') {
								
								     return array(
								          'subject'=>'Presentation du modele',
								          'content'=>theme('modele_2r_presentation_block', $node));
								}
								if ($delta=='modele_2r_fiche_technique_block') {
								     return array(
								          'subject'=>'Fiche technique du modele',
								          'content'=>theme('modele_2r_fiche_technique_block', $node));
								}
								if ($delta=='modele_2r_coloris_block') {
								     return array(
								          'subject'=>'Coloris disponible pour ce modele',
								          'content'=>theme('modele_2r_coloris_block', $node));
								}      
								if ($delta=='modele_2r_galerie_photos_block') {
								     return array(
								          'subject'=>'Galerie photos pour ce modele',
								          'content'=>theme('modele_2r_galerie_photos_block', $node));
								}
								if ($delta=='modele_2r_argus_block') {
								     return array(
								          'subject'=>'Argus pour ce modele',
								          'content'=>theme('modele_2r_argus_block', $node));
								}
							}
           	}
           	break;

Checking that arg(1) is a number avoids calling node_load for paths like node or node/add/*

Clément’s picture

Thank you for your kind reply and fast.

So I deleted the hook_preprocess() function and I replaced my code with the one you have proposed.

It seems that I can not recover the $node array in my blocks.

If I put this code in the block modele_2r_presentation_block.tpl.php example, the result is null:

<pre>
<?php print_r ($node); ?>
</pre>
nevets’s picture

A couple of things, I noticed there is a missing 'if'

($node->type == 'modele_2r') {

should be

if ($node->type == 'modele_2r') {

and what do you see for $node if you add this after the node_load

drupal_set_message("Node:<pre>".print_r($node,TRUE).'</pre>');

Also you might change your

<pre>
<?php print_r ($node); ?>
</pre>

to

<pre>
<?php print print_r ($node,TRUE); ?>
</pre>
Clément’s picture

In the abscence of if I had noticed and corrected.

Now I added

drupal_set_message("Node:<pre>".print_r($node,TRUE).'</pre>');

The node is displayed.

By adding cons:

<pre>
<?php print print_r ($node,TRUE); ?>
</pre>

in the block, there is no result (the block is displayed).

nevets’s picture

You code looks ok, have you tried clearing the theme cache?

Clément’s picture

Yes (flush all caches). Still no result...

I do not understand why it does not work!

Thank you for your patience.

nevets’s picture

In hook_theme array('node') needs to be array('node' => NULL). Remember to change for all your template and to clear the theme cache after the change.

Clément’s picture

Yes it works now!!!

Thank you very much! You're the best ;)

Greetings.
Clement