开发者

Can a Joomla module "know" what position it's in?

开发者 https://www.devze.com 2023-04-10 18:58 出处:网络
I\'m fairly new to Joomla (I\'ve been more of a Wordpress guy) and I have a question about module positions.

I'm fairly new to Joomla (I've been more of a Wordpress guy) and I have a question about module positions.

Can a module know what position it's in. For instance can I do something like this:

if(modulePosition =='left'){
    Do this...
}else{
    Do 开发者_开发知识库that...
}

It seems easy enough, but I've searched for hours and can't find anything that will help me with that. I know there is a countModules function but from what I can tell, that just checks to see if the module is active or not.

Thanks for your help!


I found the answer! Mostly thanks to @Hanny. His idea of using the modules id got me googling for that and I came across the answer. For anyone else that happens to be looking to do something similar here it is.

You use a global variable $module (who'd a thought, right?)

So my code now looks like this:

$class = '';
if($module->position == 'position1'){
     $class = 'class1';
}
and so on...

Pretty simple, huh?

To find out what else you can do with the global variable $module just put this in your code and see what info you can use:

echo(print_r($module));

Thanks for all your help!


The short answer is 'yes', you'll assign a module a position based on your template. When it shows up you can have conditionals like that regarding that position (different templates have different naming conventions for positions, so make sure you know what they are before coding).

For example, some use "Position12", others may use "leftcol", etc. You just have to check in the template files to see (you can check the .xml file in the template directory to see the positions listed in the template, or look in the index.php file for the jdoc includes).

In some of my experience, the only time you'll really ever need code like that is in the core layout files of the template (for example, if you have different widths of columns depending on modules being present or not), otherwise there won't really be a time where you 'may or may not' have a module showing up - because you'll explicitly be telling them where to be and when on the back end.


I tried to comment under john's solution but I don't have a enough rep points-- I wanted to add it doesn't matter what you name the module position in your template case-wise the position name you get back from $module->position is always all lowercase regardless of how you named the position in the template... ie. in your template xml somewhere you might have topBar position will be named 'topbar' not 'topBar' when you try to check it with

if($module->position == 'topBar') //always false... use instead
if($module->position == 'topbar') //what you need to use


I'm going to disagree with Hanny. I think the answer is no, not as you describe.

The template knows when it has reached a module position, and it gets a list of modules assigned to that position, then calls for them to be rendered. But it doesn't pass that information on. It's not stored in JApplication or JDocument etc either (like, nothing stores where in the template the rendering is up to or anything).

There are some hacky ways to almost get what you want though. If you know the template positions you need to search (sadly there's no easy method for getting this from the template - otherwise you could parse your template's .XML file for <position> elements...), then you could do something like:

<?php

$positions = array('left', 'right', 'top', 'bottom')

$found_in = false;

foreach ($positions as $cur_position)
{
    $module_positions = JModuleHelper::getModules($cur_position);
    foreach ($module_positions as $cur_module_in_pos)
    {
        if ($cur_module_in_pos->module == 'mod_MYMODULE')
        {
            $found_in = $cur_position;
        }
    }
}

if ($found_in)
...

Of course, this doesn't work well if your module is included multiple times on the page, but maybe that's an assumption you can make?

Otherwise it'd be up to hacking the core - you could use a JDispatcher::trigger() call before the template calls a module, set some var etc. Unfortunately there's no such event in core to start (a pre_module_render or something).


A module instance is assigned to a single position and this is stored in the database and normally you would style the position in the template. A module instance can only be assigned to one position. So while it's an interesting question, it's not really a practical one.

The exceptions to this are the following: loadposition ... you might want to know if a module is being loaded using the plugin because this would put it potentially somewhere besides the styled area for the position. THough i would recommend always making a new instance for this precisely so you have more control.

loadmodule ... module loaded by name using the plugin. In this case again you are probably better off making a new instance of the module and styling it. Also I'd put it in a span or div anyway, depending what it is.

jdocinclude:module ... loading a module directly in a template. Again if you are doing this I would wrap it in a span or div. In this case you are also allowed to include a string of inline styles if you like that kind of thing.

Rendering the module to a string and echoing it, again that is basically a very customized solution and you would want to set the styles and options.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号