Martijn Geerts Posted January 31, 2013 Share Posted January 31, 2013 Hai, I'm trying to get the current "$page" in a Inputfield.module or a Fieldtype.module. So far I ended up with: $pageId = (int) wire('input')->get('id'); $page = wire('pages')->get($pageId); This solution works, but it's quite hacky. There must be a better way. Link to comment Share on other sites More sharing options...
apeisa Posted January 31, 2013 Share Posted January 31, 2013 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 More sharing options...
Martijn Geerts Posted January 31, 2013 Author Share Posted January 31, 2013 You're right about the admin context. Building a Inputfield and a Fieldtype & I don't know if the $page variable is reachable at that spot inside the admin. (a normal way) The inputfield should react on the state of the page ( viewable ) etc. Link to comment Share on other sites More sharing options...
Soma Posted January 31, 2013 Share Posted January 31, 2013 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()){...} 2 Link to comment Share on other sites More sharing options...
doolak Posted February 1, 2013 Share Posted February 1, 2013 Exactly what i was searching for... Link to comment Share on other sites More sharing options...
ryan Posted February 2, 2013 Share Posted February 2, 2013 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. 4 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted February 2, 2013 Author Share Posted February 2, 2013 (edited) 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 February 3, 2013 by Martijn Geerts 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now