shenkwen
shenkwen

Reputation: 3870

Can a Joomla module “know” what position it's in? (2)

This is my very first question at StackOverflow. I was trying to post comment in the post Can a Joomla module "know" what position it's in?, but could find no way to do that so I have to post another question here. Any tips regarding how to do this properly is greatly apprieciated. Anyway, here is my questions:

I tried the code mentioned in the above post but it seemed didn't work. I'm not only quite new to PHP, but also coding, so I'm not sure if it's me or the code. here's what I did to the module's default.php file:

1)to ensure I insert the code at the right place, I insert

 <?php echo(print_r($module)); ?> 

and it output 1 at the right position;

2)the position-name that I need to determine is "showcase-a", so I insert code

 <?php if ($module->position == 'showcase-a'):?>test<?php endif;?>

to the above place, but this time it does't show anything;

3)then I tried this code:

 <?php if ($module):?><span>test</span><?php endif; ?>

But still it does't display "test" at the position as I expected.

4)I tried

 <?php if (1):?><span>test</span><?php endif; ?>

and the test "test" displays as good. So I didn't code the IF statement wrong.

Now I'm totally lost. Since print_r($module) outputs 1, $module must be positive, why PHP ignore test in 3)? This is just a side question for the sake of PHP learning. What I still need to solve is let the module determine which position itself is in. Please help. Thank you!

Upvotes: 4

Views: 535

Answers (2)

StiGMaT
StiGMaT

Reputation: 760

If $module->position didn't work for you. $module->position may only work for joomla 1.7, 2.5 and + but i'm not sure. $module->position works in joomla 2.5.

Otherwise you need to make a module.php function

Check out the file

/templates/you-template/html/modules.php

For example you make your position the following in your template (for example index.php)

<jdoc:include type="modules" name="header" style="includeposition" />

inside of /templates/you-template/html/modules.php

You make a function like this :

<?php
function modChrome_includeposition($module, &$params, &$attribs){
    //Set a value to $module->position
    $module->position='WHATEVER YOUWANT';
    ?>
    <div class="moduletable <?php echo $params->get('moduleclass_sfx').' '.$module->name; ?>">
        <?php
        if($module->showtitle){
        ?>
        <div class="modtitle">
            <div class="fleche">
                <span>
                    <?php
                        echo $module->title;
                    ?>
                </span>
            </div>
        </div>
        <?php
        }
        ?>
        <div class="modcontent">
            <?php echo $module->content; ?>
            <div class="clear">
            </div>
        </div>
    </div>
    <?php
}
?>

make different functions names that sets different values if needed.

the function name will match the syle you set:

modChrome_includeposition($module, &$params, &$attribs)

because

style="includeposition"

inside of your jdoc.

Upvotes: 1

Riccardo Zorn
Riccardo Zorn

Reputation: 5615

I'm not sure about using it in the template (although it doesn't seem wrong altogether) but my modules oftentimes access the position like this:

$module->position

in the module (so mod_something.php) so try to put it there, if it's available just set a variable and it will be available in the view too.

Upvotes: 1

Related Questions