Results 1 to 14 of 14

Thread: Getting a Page's Name by ID

  1. #1
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Getting a Page's Name by ID

    How would I go about getting a page's name (as opposed to it's title) by ID?

    Is there an undocumented insert tag to do this?
    Brian

  2. #2
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Getting a Page's Name by ID

    do you need it in a custom template?
    Consulenza Contao CMS https://www.intco.it

  3. #3
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Getting a Page's Name by ID

    Yes -- specifically I need to create a link to it in a custom template, but the link text needs to be it's name (and not its title).
    Brian

  4. #4
    User Toflar's Avatar
    Join Date
    06-19-09.
    Location
    Lyss, Switzerland
    Posts
    170

    Default Re: Getting a Page's Name by ID

    Code:
    <?php
    global $objPage;
    // id
    echo $objPage->id;
    ?>
    should do the job
    Regards

    Yanick - Contao core developer @terminal42 gmbh

  5. #5
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Getting a Page's Name by ID

    Thanks Toflar.

    Let me rephrase -- I already know the page's ID (it's another page on the site) and I need to get that page's name (not the title) from this ID.

    Does that make more sense?

    Thanks again!
    Brian

  6. #6
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Getting a Page's Name by ID

    Quote Originally Posted by Medianomaly
    Yes -- specifically I need to create a link to it in a custom template, but the link text needs to be it's name (and not its title).
    AFAIK the only solution is to make a query

    (I assume you mean alias for name)

    you can do this in (at least) two ways:

    Code:
    // 1st way
    $this->import('Database');
    $res = $this->Database->prepare('SELECT id, alias FROM tl_page WHERE id=?')->execute($id)->next();
    // from here on you can use $res->alias
    or you can install zedseries library and use it this way:

    Code:
    // 2nd way
    $page = new ZedSeriesModel('tl_page');
    $page->findBy('id', $id);
    // from here on you can use $page->alias
    an alternative solution is to create a custom insert tag to obtain the result you want

    this is a bit longer than the above solutions but it is not so difficult
    Consulenza Contao CMS https://www.intco.it

  7. #7
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Getting a Page's Name by ID

    Interesting -- thanks for the info. (EDIT -- Sorry, this ended up getting really long...)

    Since I have to resort to queries, what if I need to find several (about 25) -- same solution(s) would work best?

    Maybe it would be best if I backed up for a second and told you overall what I'm trying to accomplish, and if you have time, you could lend me some quick advice on how I'm doing it.

    Take a look at this mockup -- http://jr.medianomaly.com/assets/mockups/
    -- in the footer there is a section titled "Quick Links" -- essentially 5 columns of links to various pages on the site.

    I want to give the client control over what links appear there, but I don't want them to have access to modules if at all possible -- and I want to make it streamlined and simple for them.

    So what I did was use Staen's kick-ass dma_elementgenerator extension, and created a new content element called "Quick Links" that is made up of 5 page tree wizards with checkbox selections. Each controls one column of quick links.

    Now those selections come through to the template as one of those serialized strings. Here's an example of one:
    a:5:{i:0;s:2:"11";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1: "9";i:4;s:2:"10";}

    I'm not really sure what most of that means, but when I run it through the deserialize() function, I get an array of page IDs that correspond to what was selected in the backend.

    So it should be easy just to plug that into a {{link::id}} insert tag -- but here's the catch. There are two separate fields when you define a new page:

    1. Page Name -- "Criminal Defense" -- This is what TL uses to generate the link text for built-in navigation modules, and this is the one I want to grab for my quick links.

    2. Page Title -- "Criminal Defense Overview" -- This is used for title tags and for headlines and stuff. I don't want my quick links to have this link text, but all of the built-in insert tags return this instead of the name.

    So that's the whole story. What do you think? I can use the code you've provided and adapt that to get what I need, but still the best way?

    By the way, I just discovered your blog last night. It looks to be really useful. It's so hard for us English-only speakers to get our "foot in the door" when it comes to developing for TL, because there are so few English resources for doing so. But I really appreciate the time you and Kamil are putting into developer tips and tutorials.
    Brian

  8. #8
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Getting a Page's Name by ID

    Quote Originally Posted by Medianomaly
    Interesting -- thanks for the info. (EDIT -- Sorry, this ended up getting really long...)

    Since I have to resort to queries, what if I need to find several (about 25) -- same solution(s) would work best?

    Take a look at this mockup -- http://jr.medianomaly.com/assets/mockups/
    -- in the footer there is a section titled "Quick Links" -- essentially 5 columns of links to various pages on the site.

    So what I did was use Staen's kick-ass dma_elementgenerator extension, and created a new content element called "Quick Links" that is made up of 5 page tree wizards with checkbox selections. Each controls one column of quick links.

    Now those selections come through to the template as one of those serialized strings. Here's an example of one:
    a:5:{i:0;s:2:"11";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1: "9";i:4;s:2:"10";}

    So it should be easy just to plug that into a {{link::id}} insert tag -- but here's the catch. There are two separate fields when you define a new page:

    1. Page Name -- "Criminal Defense" -- This is what TL uses to generate the link text for built-in navigation modules, and this is the one I want to grab for my quick links, but all of the built-in insert tags return page title instead of the name.

    So that's the whole story. What do you think? I can use the code you've provided and adapt that to get what I need, but still the best way?
    IMO the best way is to make a single query to obtain all the 25 titles, this way you do not have to make 25 queries to database and mysql will surely appreciate this (you have to forget about the ZedSeriesModel hint)
    but I never used the dma_generator extension so I do not know if there is a way to obtain all the 25 ids before doing the query (i.e. if you have one template or if you have five templates).
    Anyway five queries are better than 25

    Quote Originally Posted by Medianomaly
    By the way, I just discovered your blog last night. It looks to be really useful. It's so hard for us English-only speakers to get our "foot in the door" when it comes to developing for TL, because there are so few English resources for doing so. But I really appreciate the time you and Kamil are putting into developer tips and tutorials.
    You are welcome, feel free to point out spelling errors or whatever that can bring to better understanding
    Consulenza Contao CMS https://www.intco.it

  9. #9
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Getting a Page's Name by ID

    Thanks again.

    Yeah, it's about time I learned how to query stuff anyway, so that's what I'll do.

    FYI -- yes, it's all in one template, so I can do it with one query (provided I can figure it out).
    Brian

  10. #10
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Getting a Page's Name by ID

    Quote Originally Posted by Medianomaly
    Thanks again.

    Yeah, it's about time I learned how to query stuff anyway, so that's what I'll do.

    FYI -- yes, it's all in one template, so I can do it with one query (provided I can figure it out).
    if you post the template I'll be happy to help you
    Consulenza Contao CMS https://www.intco.it

  11. #11
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Getting a Page's Name by ID

    Nice! Well, here's what I'm doing right now (the relevant part) --

    Code:
    <?php foreach($this->elements as $colid=>$coloptions): ?>
    	<?php if(!empty($coloptions)): ?>
    	<ul class="<?php echo $colid; ?>">
    		<?php $count = 1; foreach(deserialize($coloptions) as $itemid=>$pageid): ?>
    			<li<?php if($count == count(deserialize($coloptions))): ?> class="last"<?php endif; ?>>{{link::<?php echo $pageid; // <- This is inserting the page title ?>}}
    		<?php $count++; endforeach; ?>
    	[/list]
    	<?php endif; ?>
    <?php endforeach; ?>
    And if you scroll down here, you'll see $this->arrData, if that helps (but there isn't much more to it):
    http://jr.medianomaly.com/index.php

    Code:
    // 1st way
    $this->import('Database');
    $res = $this->Database->prepare('SELECT id, alias FROM tl_page WHERE id=?')->execute($id)->next();
    // from here on you can use $res->alias
    I understand very basic MySQL syntax (I had just started to study it when I got really busy), but it's the TL-specifc methods that throw me, and also "id=?" (what the ? means), and also execute($id) (where $id comes from).

    Dude, I won't drag this thread out with 100 additional questions, but if you can help get me started I'd be extremely grateful.

    Thanks again!
    Brian

  12. #12
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Getting a Page's Name by ID

    please note that this is only a starting point, some things can be optimized
    please note also that this is untested code but it should work

    first of all you need to collect all the page's ids and titles you need

    Code:
    $allIds = array();
    
    foreach ($this->arrData['elements'] as $arrPage) {
        $arrTmp = deserialize($arrPage);
        // maybe user has not selected pages
        if (is_array($arrTmp)) {
            $allIds = array_merge($allIds, $arrTmp);
        }
    }
    // now we have all page's id in a single array
    // let's do the query
    
    /**
     * if you are paranoid, and paranoia is really a good thing in web programming :),
     * you want to be sure that the array $allIds contains only numeric values before doing the query
     */
    
    // the use of "IN" equals to do many id=1 OR id=2 OR id=etc.
    $res = $this->Database->execute('SELECT id, title, alias FROM tl_page WHERE id IN('.implode(',', $allIds).')');
    
    // store the ids to an array
    $keys   = $res->fetchEach('id');
    // store the titles to an array
    $values = $res->fetchEach('title');
    // store the aliases to an array
    $aliases = $res->fetchEach('alias');
    
    /**
     *  create an associative array whose keys are the id of the pages and
     *  whose values are the pages title
     */
    $arrPages = array_combine($keys, $values);
    /** same thing for aliases, aliases will be used to build the links
    */
    $arrAliases = array_combine($keys, $aliases);
    you have to put this code before the
    Code:
    <?php foreach($this->elements as $colid=>$coloptions): ?>
    and then replace the
    Code:
    {{link::<?php echo $pageid;
    with this:
    (I assume you enable url rewriting so you use aliases instead page id for linking pages.
    I suggest you to avoid using insert_tags because insert_tags lead to additional queries)

    Quote Originally Posted by ga.n
    I understand very basic MySQL syntax (I had just started to study it when I got really busy), but it's the TL-specifc methods that throw me, and also "id=?" (what the ? means), and also execute($id) (where $id comes from).
    I used $id to tell you "some id" the "?" tells to TYPOlight (aka Contao :D) that it must be replaced with a value that is "untrusted" so it must be "sanitized" to prevent sql injection .
    Consulenza Contao CMS https://www.intco.it

  13. #13
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default Re: Getting a Page's Name by ID

    Like a charm, baby. Like... a... charm...

    Can't thank you enough -- now I can title my page something like "Overview of Criminal Defense" and NOT have that be the link text in the footer. Nice...

    I WILL remember this.
    Brian

  14. #14
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Getting a Page's Name by ID

    Quote Originally Posted by Medianomaly
    Like a charm, baby. Like... a... charm...

    Can't thank you enough -- now I can title my page something like "Overview of Criminal Defense" and NOT have that be the link text in the footer. Nice...

    I WILL remember this.
    You are welcome
    Consulenza Contao CMS https://www.intco.it

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
  •