Way to get title of page (i.e. Collection name) of page by URL path?

Permalink 1 user found helpful
I am working on an Add On to allow a site owner to add their own gallery images to a Gallery page.

I want each thumbnail to have an appropriate Alt tag with the name of the photo in it. That photo name also happens to be the name of the page on which a larger version of that photo is found. The site owner adds a URL to that photo page within the Add Thumbnail Add On.

So I know what the URL that each thumbnail points to.

Each such URL corresponds to a page on the C5 site.

If I can get the name of the photo page somehow (i.e. the Collection name of that page) from the URL, using PHP code, from within the view.php file (or inside the view override function in the controller) of my Add On I can fill in the Alt correctly for each thumbnail added.

So is there a way for me to get the Collection name based on a URL to a C5 page if I know the URL to that page?

I can have the owner fill in the Alt tag as part of the Add On of course but I am trying to automate as much as I can to alleviate them of having to provide that information themselves.

Carlos

 
12345j replied on at Permalink Best Answer Reply
12345j
$page = Page::getByPath("/path");
gives you the page object- seehttp://www.concrete5.org/documentation/developers/pages/overview...
carlos123 replied on at Permalink Reply
Outstanding 12345j!!

I've been Googling and looking at how many messages on the forum here for what seems like over an hour trying to get somewhere with this and couldn't find anything that worked.

Thanks again!

Carlos
carlos123 replied on at Permalink Reply
Hmmm...I can't get it to work and would appreciate further insight from you 12345j or anyone else.

Here's what I put into my view.php...

<?php
   $page = Page::getByPath($image_url);
   echo "<pre style='background-color: white;'>";
   print_r($page);
   echo "</pre>";
   ?>


That code returns an empty $page object.

Same thing happens if I put the above code into the view() override function inside the controller for the block. Nothing.

$image_url is definitely there and corresponds to the name of the page that I am trying to get the name of. It's a value entered by the user into my Add On and is stored in the block table.

So I am guessing the above code should work. At least with respect to getting the Page object. But it doesn't for some reason.

Anybody got any further tips?

Thanks.

Carlos
12345j replied on at Permalink Reply
12345j
can you give the output of $image_url
carlos123 replied on at Permalink Reply
Oops. I just realized that the $image_url is to a page on the hosted site and not on my local site!

I did it that way in testing because I didn't want to have to go back and change all the thumbnail URL's being pointed to by the thumbnails once the site goes live.

Let me edit that to a local development URL and see if that changes things.

Carlos
carlos123 replied on at Permalink Reply
Hmmm...still not working.

Here is the output from echo $image_url...

http://domain.com.local/sunrise-over-the-santa-monica-mountains...

That's changing it to my local development URL.

But having it be...

http://domain.com/sunrise-over-the-santa-monica-mountains...

Doesn't work either.

"domain" in the above is just a placeholder for the real domain name which I would rather not publish yet.

Carlos
12345j replied on at Permalink Reply
12345j
do an str replace on the image url to delete the domain.com part and only use /sunrise-over-the-santa-monica-mountains
carlos123 replied on at Permalink Reply
I just saw your reply 12345j. I'll try that.

Give me just a minute and I'll report back.

Carlos
jordanlev replied on at Permalink Reply
jordanlev
Look at 12345j's original response a bit more closely -- the format of the path you pass in to the Page::getByPath() function is important -- it must be a path within your site, not including the domain name. Basically everything in the URL after your BASE_URL and DIR_REL (as defined in your config/site.php file). And it must start with a leading slash, and not have a trailing slash.
carlos123 replied on at Permalink Reply
Voila!

That did it guys. THANK YOU!

I had to remove the domain name.

I just assumed that "/path" was a full URL. My bad. Sorry about that.

This is one of those problems that took forever to figure out but which was super simple in the solution (with your all's extra eyes on the problem).

Thanks again!

Carlos
carlos123 replied on at Permalink Reply
For anyone that is curious here is revised code that works for my purposes...

<?php
   $urlArray = parse_url($image_url);
   $path = $urlArray['path'];
   $pageName = Page::getByPath($path)->getCollectionName();
   echo $path;
   ?>


I can now assign the $path value to the alt attribute of the img tag for each new and different thumbnail added to a gallery page.

Sweet!

Carlos
carlos123 replied on at Permalink Reply 1 Attachment
I changed the code to the following (inside the blocks view.php file) and attached a new output image...

<?php
   $page = Page::getByPath($image_url);
   echo "<pre style='background-color: white;'>";
   echo "\$page = ";
   print_r($page);
   print_r("\$image_url = $image_url");
   echo "</pre>";
   ?>


The url that is inside $image_url is definitely a page at my local copy of the site and it is definitely reachable by browser address bar.

Carlos
carlos123 replied on at Permalink Reply 1 Attachment
I've attached an image of the resultant output from the above code in case that gives anyone any insight on why this isn't working.

Carlos