Jump to content

getting current $page in a Inputfield or Fieldtype module


Recommended Posts

Does this work: $page = wire('page')

You need it in admin context?

Edit: now that I think of it, wire('page') should return the page edit admin page. I think your method is fine. Of course you could check that the current page is admin with ProcessPageEdit process defined, if you want to play it safe. 

Link to comment
Share on other sites

This is a perfect way and I think the only one, even ProcessPageEdit uses it. If you output in your module $this->page you'll get the id of the admin page and not the edited one. So the GET id is there to know what page. I think there's other way to get to the page but it will end in the same call somewhere anyway.

To check process your on in admin try $this->process and you'll get ProcessPageEdit as it's the process module running.

So get the page and set it to the inpufield module with

$this->page = $this->pages->get($this->input->get->id); 

Is simple enough and will always work in this context.

Then use it as usual

if(!$this->page->editable()){...}
  • Like 2
Link to comment
Share on other sites

Nearly all built-in Fieldtype functions are passed a copy of the $page and $field being edited. So you pretty much always have a copy in your Fieldtype. For example, every Fieldtype has this function: 

/**
 * Return a new Inputfield, ready to be used
 *
 * @param Page $page Page being edited
 * @param Field $field Field that needs an Inputfield
 *
 */
public function getInputfield(Page $page, Field $field) {
   // $page is the Page you want
}

Since there is only 1 copy of any given Fieldtype in memory at once, you don't want to keep any copies of $page outside of the individual functions because the same Fieldtype instance might get called several times for different pages within the same request. 

Inputfields are different:

  • They aren't supposed to know anything about the page being edited, because Inputfields are used for far more than just pages.
  • There will be multiple instances of the same Inputfield in memory at once. Basically, there is a new instance of it for every single input that's needed. Unlike Fieldtypes, Inputfields don't get reused for multiple fields of the same type. 

Since Inputfields aren't supposed to have a $page context, they don't already have a copy of it anywhere. However, if you need one, you can pass it a copy from your Fieldtype's getInputfield method, shown above:

/**
 * Return a new Inputfield, ready to be used
 *
 * @param Page $page Page being edited
 * @param Field $field Field that needs an Inputfield
 *
 */
public function getInputfield(Page $page, Field $field) {
   $inputfield = wire('modules')->get('InputfieldSomething'); 
   $inputfield->set('editPage', $page); // give it a copy of $page being edited
   return $inputfield; 
}

Now InputfieldSomething can access the $page from $this->editPage. 

This is what FieldtypeRepeater does for InputfieldRepeater, btw. 

  • Like 4
Link to comment
Share on other sites

This was the information I was looking for. Love to see some insights I never thought of before. Always great to see how you explane things & take your time to answer these questions!

Thank You ryan.

note: I did use your code 1 on 1.

( Hope to release MailChimpCampaign next weekend ) 

Edited by Martijn Geerts
  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...