Get page URL knowing block id.

Permalink 1 user found helpful
Hi folks.

I've developed a custom block. I can get the block id from the database table (bID).
Is any way to know the url of the page in which that block resides?

In other words, is there any way to know the url of a page knowing the id of an internal block inside it?

Thx!

 
rcrow replied on at Permalink Reply
I've found the solution, i'll share it for anyone who has the same problem.

Knowing the Block ID, to get the Collection ID you can query the CollectionVersionBlocks table:

$query = "SELECT cID FROM CollectionVersionBlocks WHERE bID=?";
$params = array($blockID);
$res = $db->getOne($query,$params);


Then, you can get the page URL using:

$page = Page::getByID($cID); 
$nh = Loader::helper(navigation);
$url = $nh->getCollectionURL($page);


Hope it helps!
JohntheFish replied on at Permalink Reply
JohntheFish
When a block is on a page, you can do:
$page_object = Page::getCurrentPage();
$page_id = $page->getCollectionID();


In many contexts, the global $c (for collection) is already the page object. So you can do:
$page_id = $c->getCollectionID();


If you want to find out where a block is used, ie, you are not trying to find this out from within the block actually on a page, it gets more complicated because a block can be used in more than one place.

For that, you should have a look at the code inside the "Where's My Block" addon.
http://www.concrete5.org/marketplace/addons/where-is-my-block/...

That finds out where all uses of a block type are, so you only want the part from block id onwards.
rcrow replied on at Permalink Reply
Thanks for your reply JohntheFish.

There is only one instance of each custom block and each one is not in the actual page. So, as you said, it is more complicated to find out.

In other words, i am on page X and i want to know the URL of page Y who has inside a block W. W exists only in Y.

Thx for the addon.

Best regards