Results 1 to 12 of 12

Thread: Level of Page from the site structure

  1. #1

    Default Level of Page from the site structure

    Is there a way to find the level of Page from the site structure with php?

    An example structure:

    website root A (level_0)
    - Page B (level_1)
    -- Page C (level_2)

    I wanted to add this level information to the pages body class. From above example page C's body tag should have class level_2.

    Following ideas are not practical.
    1. Adding level class manually to page
    2. Defining a new layout for each level
    OM MANI PEME HUNG! how many has to die for freedom and dignity. Save this world

  2. #2
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    One way might be to go up the tree by inspecting the parent page with pid and check if it's type is root. Do this in a loop and count till you're successful.

    Maybe the easiest way will be to analyize your navigation menu with JS. Search for active item and fetch the class level_x.
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  3. #3
    User tetrijeb's Avatar
    Join Date
    04-19-12.
    Location
    Bosnia&Herzegovina
    Posts
    114

    Default

    You could use query like this one:

    Code:
    SELECT id, concat("level_",ROUND((LENGTH(parents) - LENGTH( REPLACE (parents, ",", ""))))) FROM tl_page
    and echo it.

    PS.

    Possible PHP code

    Code:
    $pageID=$objPage->id;
    
    $query="SELECT concat("level_",ROUND((LENGTH(parents) - LENGTH( REPLACE (parents, ",", ""))))) FROM tl_page WHERE id={$pageID}";
    
    $get_level=mysqli_query($your_db_connection,$query);
    
    $level=mysqli_fetch_row($get_level);
    
    echo $level;
    Last edited by tetrijeb; 11/06/2015 at 16:50.

  4. #4
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    Don't understand your SELECT, but you should use the Database class from Contao.
    PHP Code:
    $objDatabase = \Database::getInstance(); 
    Take a look at https://github.com/contao/core/blob/...o/Database.php for methods.

    Also search within Contao core php files to find some examples.

    Now I also found that you can use this one:
    PHP Code:
    global $objPage;
    $level count(\Database::getInstance()->getParentRecords($objPage->id'tl_page')); 
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  5. #5
    User tetrijeb's Avatar
    Join Date
    04-19-12.
    Location
    Bosnia&Herzegovina
    Posts
    114

    Default

    Quote Originally Posted by Andreas View Post
    Don't understand your SELECT, but you should use the Database class from Contao.
    Well, it's a really alternative approach, but since I don't know the core of CMS very well, I was exploring for the simplest solution I can figure out...

    I was watching the structure of the table tl_page and found that parents column has pattern from which I can extract the level of page.

    E.g.

    1
    23,5
    190,23,5
    ...

    It seems that the number of commas is same as the level of page, so I wrote SELECT that counts the commas.

    Don't laugh, I was in the inspiring mood.
    Last edited by tetrijeb; 11/06/2015 at 16:49.

  6. #6
    User tetrijeb's Avatar
    Join Date
    04-19-12.
    Location
    Bosnia&Herzegovina
    Posts
    114

    Default

    PS.

    Because the root page is level_0, this should be modified by adding 1 to the page level if the page itself is not a root page. :/

    concat("level_",ROUND((LENGTH(parents) - LENGTH( REPLACE (parents, ",", ""))+1)))

  7. #7
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    No, I don't laugh, it was just a usefull info how to easily handle DB-querys within Contao.

    If you have this field tl_page.parents, you can easily do
    PHP Code:
    global $objPage;
    $level count(explode(','$objPage->parents)); 
    But I think this field comes by an extension.
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  8. #8
    User tetrijeb's Avatar
    Join Date
    04-19-12.
    Location
    Bosnia&Herzegovina
    Posts
    114

    Default

    Quote Originally Posted by Andreas View Post
    But I think this field comes by an extension.
    Yes, you have right. I've just checked.

    [parentslist]

    Two new fields are added to tabel tl_page: parents and rootId. They offer direct and fast access to all parents, grandparents, etc. and the root object for this page.

    The field rootId stores the ID of the root object for this page. It allows direct access to that root object with one single SQL selection.

    The field parents stores a comma separated list of the parent, the grandparent, ... up to the root object.Using this list you can use one single SQL select to get them all instead of recursively search through the database.
    Example: select * from tl_page where id in (27,5,2);

    The field childrens stores a list of all children, their children, etc. in a comma seperated list.
    And it seems it's availabe for 2.11 - 3.2.
    Last edited by tetrijeb; 11/06/2015 at 17:12.

  9. #9
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    Yes, I remember. I tested [parentslist] some weeks ago. Goal was to get the root page language.

    But now I know, that I don't need this extension as I easily can get root page id of a particular page by fetching the last array item of
    PHP Code:
    \Database::getInstance()->getParentRecords($objPage->id'tl_page'
    Last edited by Andreas; 11/06/2015 at 17:28.
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  10. #10

    Default

    HI buddies, thanks for nice ideas. But I've solved it with HOOKS

    Insde Config
    PHP Code:
    $GLOBALS['TL_HOOKS']['generatePage'][] = array('myclass''get_parentPageInfo'); 
    Inside myclass
    PHP Code:
       ...
        public 
    $level 0;
       ....
     
       
    //Hook function Finding parent Page Info 
        
    public function get_parentPageInfo($objPage$objLayout$objThis) {
            
    $this->parentPageInfo($objPage->id);
            
    $objThis->Template->level ' plevel_' $this->level;
        }

        
    //Helper Function, Finding page level recursively
        
    public function parentPageInfo($id) {
            
    $parentPage $this->Database->prepare("SELECT id, pid, title, type FROM tl_page WHERE id=?")
                ->
    limit(1)
                ->
    execute($id);

            if(
    $parentPage->type == 'root') {
                return;
            } else  {
               
    $this->level++;
               
    $this->parentPageInfo($parentPage->pid);
            }
        } 
    And now inside tl_page Template you have $this->level available.
    OM MANI PEME HUNG! how many has to die for freedom and dignity. Save this world

  11. #11
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    499

    Default

    So why don't you simply use this? Whithout the helper function.
    PHP Code:
    //Hook function Finding parent Page Info
    public function get_parentPageInfo($objPage$objLayout$objThis) {
      
    $level count($this->Database->getParentRecords($objPage->id'tl_page'));
      
    $objThis->Template->level ' plevel_' $level 1;

    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  12. #12

    Default

    Yes yours one works too, may be in fact more compact and faster.
    PHP Code:
    $objThis->Template->level ' plevel_' . ($level 1); 
    OM MANI PEME HUNG! how many has to die for freedom and dignity. Save this world

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •