Solved

Joomla 3 - JFactory::getDocument()->countModules

Posted on 2013-11-11
7
1,339 Views
Last Modified: 2013-11-12
Hi Experts,

Can you please help me understand why line8 isn't working?

What I'm trying to achieve is something similar to $this->countModules() in a non-index.php file.

Thanks
Rick

function topSpanSize(){

    
    // count modules in the top container

    $modCount=0;
    for($i=1; $i<=3; $i++){
        $module=JFactory::getDocument()->countModules('top-'.$i);
        if(!$module==0){  
            $modCount++;
        }
    }
     

    /* Calculate the span size based
     * on module count. e.g. If only 1 
     * module exist in the top-container 
     * then it should take up the whole row. 
     * i.e. span12  */

    if ($modCount<>0){
       $topSpanNum = 12/$modCount; 
    }
    else{
       $topSpanNum = 0;
    }
    
    return $topSpanNum;
}

Open in new window

0
Comment
Question by:RiCzN
  • 5
  • 2
7 Comments
 

Author Comment

by:RiCzN
ID: 39640675
I was following this link here https://groups.google.com/forum/#!topic/joomla-dev-general/4qXKUV8ofrk. It isn't working for me. Any ideas?
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 39640950
What is happening when you try that code?  Have you tried dumping any of the variables to see what is being returned?

function topSpanSize(){
    // count modules in the top container
    $modCount=0;
    for($i=1; $i<=3; $i++){
        $x=JFactory::getDocument();     // <------------- what happens here?
        die(print_r($x,1));             // <------------- what happens here?
        $module=JFactory::getDocument()->countModules('top-'.$i);
        if(!$module==0){  
            $modCount++;
        }
    }
    /* Calculate the span size based
     * on module count. e.g. If only 1 
     * module exist in the top-container 
     * then it should take up the whole row. 
     * i.e. span12  */
    if ($modCount<>0){
       $topSpanNum = 12/$modCount; 
    }
    else{
       $topSpanNum = 0;
    }
    return $topSpanNum;
}

Open in new window

0
 

Author Comment

by:RiCzN
ID: 39641004
Thanks for posting routinet. As per your code above, when at line 5, $x variable of type JDocumentHTML is recognised. After line 6 it prints the following and dies:

 
JDocumentHTML Object ( [_links] => Array ( [http://localhost/joomla3_1/] => Array ( [relation] => canonical [relType] => rel [attribs] => Array ( ) ) ) [_custom] => Array ( ) [template] => alpha1_0 [baseurl] => /joomla3_1 [params] => JRegistry Object ( [data:protected] => stdClass Object ( ) ) [_file] => C:\xampp\htdocs\Joomla3_1/templates/alpha1_0/index.php [_template:protected] => [_template_tags:protected] => Array ( ) [_caching:protected] => [_html5:JDocumentHTML:private] => [title] => Home [description] => [link] => [base] => http://localhost/joomla3_1/index.php [language] => en-gb [direction] => ltr [_generator] => Joomla! - Open Source Content Management [_mdate] => [_tab] => [_lineEnd] => [_charset] => utf-8 [_mime] => text/html [_namespace] => [_profile] => [_scripts] => Array ( [/joomla3_1/media/system/js/mootools-core.js] => Array ( [mime] => text/javascript [defer] => [async] => ) [/joomla3_1/media/system/js/core.js] => Array ( [mime] => text/javascript [defer] => [async] => ) [/joomla3_1/media/system/js/caption.js] => Array ( [mime] => text/javascript [defer] => [async] => ) [/joomla3_1/media/jui/js/jquery.min.js] => Array ( [mime] => text/javascript [defer] => [async] => ) [/joomla3_1/media/jui/js/jquery-noconflict.js] => Array ( [mime] => text/javascript [defer] => [async] => ) [/joomla3_1/media/jui/js/bootstrap.min.js] => Array ( [mime] => text/javascript [defer] => [async] => ) ) [_script] => Array ( [text/javascript] => window.addEvent('load', function() { new JCaption('img.caption'); }); ) [_styleSheets] => Array ( [/joomla3_1/media/jui/css/bootstrap.min.css] => Array ( [mime] => text/css [media] => [attribs] => Array ( ) ) [/joomla3_1/media/jui/css/bootstrap-responsive.min.css] => Array ( [mime] => text/css [media] => [attribs] => Array ( ) ) [/joomla3_1/media/jui/css/bootstrap-extended.css] => Array ( [mime] => text/css [media] => [attribs] => Array ( ) ) ) [_style] => Array ( ) [_metaTags] => Array ( [http-equiv] => Array ( [content-type] => text/html; charset=utf-8 ) [standard] => Array ( [keywords] => [rights] => [author] => Super User ) ) [_engine] => [_type] => html )

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:RiCzN
ID: 39641017
When I run my original codes and it hits line 8. The module count = 0. This is incorrect as I am definitely sure the module count should equal to 3. When I test the codes with $this->countModules() on the index file. It gives the correct count.

The index.php file is currently pulling in an external file via:

require dirname(__FILE__) . '\php\htmlOutputFns.php';

Open in new window

0
 
LVL 50

Accepted Solution

by:
Steve Bink earned 500 total points
ID: 39643407
Run this in place of your for loop (line 4):
    $mydie='';
    $x=JFactory::getDocument();
    for($i=1; $i<=3; $i++){
        $mydie .= "count for top-$i=".$x->countModules('top-'.$i)."<br />\n";
    }
    die($mydie);

Open in new window

0
 

Author Comment

by:RiCzN
ID: 39643426
output as per above code:

count for top-1=0
count for top-2=0
count for top-3=0

Open in new window

0
 

Author Closing Comment

by:RiCzN
ID: 39643438
Thanks... Stupid of me. I forgot to switch it on.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now