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?
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
do you need it in a custom template?
Consulenza Contao CMS https://www.intco.it
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
should do the jobCode:<?php global $objPage; // id echo $objPage->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
AFAIK the only solution is to make a queryOriginally Posted by Medianomaly
(I assume you mean alias for name)
you can do this in (at least) two ways:
or you can install zedseries library and use it this way: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
an alternative solution is to create a custom insert tag to obtain the result you wantCode:// 2nd way $page = new ZedSeriesModel('tl_page'); $page->findBy('id', $id); // from here on you can use $page->alias
this is a bit longer than the above solutions but it is not so difficult
Consulenza Contao CMS https://www.intco.it
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
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)Originally Posted by Medianomaly
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
You are welcome, feel free to point out spelling errors or whatever that can bring to better understandingOriginally Posted by Medianomaly
Consulenza Contao CMS https://www.intco.it
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
if you post the template I'll be happy to help youOriginally Posted by Medianomaly
Consulenza Contao CMS https://www.intco.it
Nice! Well, here's what I'm doing right now (the relevant part) --
And if you scroll down here, you'll see $this->arrData, if that helps (but there isn't much more to it):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; ?>
http://jr.medianomaly.com/index.php
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).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
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
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
you have to put this code before theCode:$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);
and then replace theCode:<?php foreach($this->elements as $colid=>$coloptions): ?>
with this:Code:{{link::<?php echo $pageid;
(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)
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 .Originally Posted by ga.n
Consulenza Contao CMS https://www.intco.it
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
You are welcomeOriginally Posted by Medianomaly
Consulenza Contao CMS https://www.intco.it
Bookmarks