Lance O. Posted August 9, 2017 Share Posted August 9, 2017 I'm working on a site that has both a "Blog" and a "News" section. I'd like to be able to use the same "Post" template for child pages of each of these sections. The catch is that the "Post" template needs to include a Page Reference field that should display categories specific to "Blog" posts -or- specific to "News" posts. In other words, the categories for these two sections are different. What is the best approach in displaying a "Categories" field on the "Post" template that would display the appropriate categories based on if the post is located in the "Blog" or the "News" section? Page tree looks like this: Blog Posts Blog Post 1 (Categories field should only reference blog categories) Blog Post 2 Blog Post 3 Categories Blog Category 1 Blog Category 2 Blog Category 3 News Posts News Post 1 (Categories field should only reference news categories) News Post 2 News Post 3 Categories News Category 1 News Category 2 News Category 3 Link to comment Share on other sites More sharing options...
abdus Posted August 9, 2017 Share Posted August 9, 2017 I faced with a very similar same issue (here) in the past. There's some good discussion and solutions in that post. To summarize: If you're not using "Allow new pages to be created from field" feature, you can provide a selector string or hook into "InputfieldPage::getSelectablePages" to return only the pages you want to appear. If you're using Autocomplete inputs or creating new pages from the field it wont work. There's no elegant solution to this as far as I know. But, you have several options: Duplicate the page field and add both to the post template. Hide one or the other depending on the parent. Duplicate the template as well and add a single field to a template (and distinguish it with blog-post & news-post). Then you can include one template file inside the other to reduce duplication etc. Hope this helps. 3 Link to comment Share on other sites More sharing options...
Lance O. Posted August 9, 2017 Author Share Posted August 9, 2017 Thanks for your quick response, abdus, and for providing a link to the earlier discussion. Rather than spend any more time on a solution, I think I'll just create two different templates based on your suggestion. Thank you! 1 Link to comment Share on other sites More sharing options...
maxf5 Posted August 9, 2017 Share Posted August 9, 2017 Why don't you make a "Select Options" field inside the Posts and determine the category there. Link to comment Share on other sites More sharing options...
Lance O. Posted August 9, 2017 Author Share Posted August 9, 2017 Because the client needs the ability to add or edit categories. Creating a "Select Options" field would mean that I would have to provide permissions to edit the field itself. Link to comment Share on other sites More sharing options...
Zeka Posted August 10, 2017 Share Posted August 10, 2017 Hi @Lance O. You can do it by specifying needed selector in the field options or with hook. 1 Link to comment Share on other sites More sharing options...
maxf5 Posted August 10, 2017 Share Posted August 10, 2017 alternative method: BLOG - CAT1 POST POST - CAT2 POST POST NEWS - CAT1 POST POST - CAT2 POST POST Link to comment Share on other sites More sharing options...
Robin S Posted August 10, 2017 Share Posted August 10, 2017 14 hours ago, abdus said: If you're not using "Allow new pages to be created from field" feature, you can provide a selector string or hook into "InputfieldPage::getSelectablePages" to return only the pages you want to appear. If you're using Autocomplete inputs or creating new pages from the field it wont work. With a bit of hooking it is possible to both dynamically control the selectable pages for a PageAutocomplete inputfield, and dynamically set the parent/template of new pages created from the inputfield. I have only played around with this and haven't used it in production, so use it at your own risk. Dynamic selectable pages for PageAutocomplete The idea is that in the field settings you define a broad selector that includes all of the pages you might want to select. It could be something really broad such as "has_parent!=2". Then you modify the selector in a hook according to the page being edited. In /site/ready.php... $wire->addHookAfter('Field::getInputfield', function(HookEvent $event) { $field = $event->object; $inputfield = $event->return; // Only for this one Page Reference field if($field->name !== 'test_page_field') return; // Only for ProcessPageEdit if($this->process != 'ProcessPageEdit') return; $page = $this->process->getPage(); // Get findPagesSelector (or findPagesSelect if you used that) $selector = $inputfield->findPagesSelector; /* Now append to or overwrite $selector however you like, depending on $page maybe. You can even define some $allowed PageArray like with the "Custom PHP code" option that is possible with other Page inputfields, and then use it in the selector as "id=$allowed". But note that there is a 500 character limit enforced on selectors. */ // Set the modified selector back to the property you got it from $inputfield->findPagesSelector = $selector; }); Dynamic parent and/or template for pages created from the inputfield The idea is that in the field settings you do not define a parent or template for allowed pages, but then set these properties in a couple of hooks according to the page being edited. In /site/ready.php... $wire->addHookBefore('InputfieldPage::renderAddable', null, 'dynamicParentandTemplate'); $wire->addHookBefore('InputfieldPage::processInputAddPages', null, 'dynamicParentandTemplate'); function dynamicParentandTemplate(HookEvent $event) { $inputfield = $event->object; // Only for this one Page Reference field if($inputfield->hasField->name !== 'test_page_field') return; // Only for ProcessPageEdit if(wire()->process != 'ProcessPageEdit') return; $page = wire()->process->getPage(); // Now set $parent_id and $template_id dynamically depending on $page $parent_id = 1234; // demo value $template_id = 56; // demo value $inputfield->parent_id = $parent_id; $inputfield->template_id = $template_id; } Have fun! 4 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