Jump to content

Get page id from images object


gebeer
 Share

Recommended Posts

Hi all,

I'm working on a function that outputs markup for a bootstrap 3 carousel.

I'm passing the $page->images object to the function argument $images.

To have an individual id for each carousel on a page with multiple carousels, I want to use the image field id and the page id of the page were the image field sits.

I can get the image field id with:

$images->field->id

But I can't seem to get the page id although it's there in the var_dump of the $images object:

object(Pageimages)[203]
  protected 'page' => 
    object(Page)[206]
      protected 'template' =>
      ... 
      protected 'settings' => 
        array (size=10)
          'id' => int 1013 //this is the id I need
          ...
  protected 'field' => 
    object(Field)[72]
      protected 'settings' => 
        array (size=5)
          'id' => int 100 //this one I can get with $images->field->id
...

When I try

$images->page->settings->id

I get null.

When I try

$images->page->id

I get 1012 instead of 1013. 1012 is nowhere to be found in the $images object. It is the id of the page from which I'm calling my function. But I'm not passing the $page object to my function.

So where does it come from and how can I get the correct page id?

Thanks for reading through this!

Link to comment
Share on other sites

"protected" means it's well "protected"

I never use or used the var_dump to inspect PW vars or objects. It is just too messy and often leads to more confusion or wrong assumptions and ways to retrieve something that there may already a API method for that. Looking at a var dump doesn't tell me that. What I do is think of what I'm dealing with and look in the classes according to see what public methods there are I could use.

Pageimages are special objects, containing your image(s) as Pageimage which extends Pagefile. Pagefile can be images or files.

I look at wire/core/Pagefile.php

Pagefile contains a get() method that allows to get the Page the (single) image object is from. 

https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/Pagefile.php#L140

So this gets me the page

$p = $img->get("page");

equals to without get() 

$p = $img->page;

This doesn't mean there's a object->page, but rather a magic get method mapping the call to a get("page")

You can also get the Pageimages from a single Pagefile

$pagefiles = $img->pagefiles;

That gives all the other images/files from the same field (in case there's any)

Pagefiles also has a method to get the page with a public method for example 

$p = $img->pagefiles->getPage();

https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/Pagefiles.php#L81

So tons of ways and option, and none of it you'll get to know when doing a var_dump.

  • Like 5
Link to comment
Share on other sites

@Soma

thank you very much for taking your time to post this very informative lesson on how to do things in PW!

Obviously, I'm still pretty new to PW and a self-taught PHP fiddler who is just emerging from the rookie stage. From this forum, I've already gained a lot of new insights.

Love PW :-)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...