By Clément on
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
Some thoughts. First,
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
Checking that arg(1) is a number avoids calling node_load for paths like node or node/add/*
Thank you for your kind reply
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:
A couple of things, I noticed
A couple of things, I noticed there is a missing 'if'
should be
and what do you see for $node if you add this after the node_load
Also you might change your
to
In the abscence of if I had
In the abscence of if I had noticed and corrected.
Now I added
The node is displayed.
By adding cons:
in the block, there is no result (the block is displayed).
You code looks ok, have you
You code looks ok, have you tried clearing the theme cache?
Yes (flush all caches). Still
Yes (flush all caches). Still no result...
I do not understand why it does not work!
Thank you for your patience.
Ok, I see whats missing
In hook_theme
array('node')needs to bearray('node' => NULL). Remember to change for all your template and to clear the theme cache after the change.Yes it works now!!! Thank you
Yes it works now!!!
Thank you very much! You're the best ;)
Greetings.
Clement