thijs Posted August 9, 2012 Share Posted August 9, 2012 Hi all, I’m implemeting a feature where a client should be able to select a page to be shown on the frontpage, call it a ‘featured page’ checkbox. Thing is; only one page should be able to be featured, and by default a checkbox can ofcourse be selected on more than one of the pages. What would be the best approach here? To create a new field module that has some sort of hook that sets the checkbox values on all other pages to 0/false, when it is selected on the page that is being saved? There are ofcourse other ways to implement this, such as a select box on the parent page populated with it’s child pages, but it would be nice to use a checkbox on the page itself. Curious to hear your thoughts! Best, Thijs Link to comment Share on other sites More sharing options...
ryan Posted August 9, 2012 Share Posted August 9, 2012 The simplest solution would be to just put the 'featured' page reference on your homepage, and select the featured page from there (via a PageListSelect input). But if you need to do it from a featured checkbox on the actual page being featured, then you might want to add another component to finding the featured page: $featured = $pages->get("featured=1, sort=-modified"); That would pull the page that was most recently modified and featured. It would also be relatively simple to un-feature pages via a module or in your template, etc. $allFeatured = $pages->find("featured=1, sort=modified"); $featured = $allFeatured->pop(); foreach($allFeatured as $p) { $p->of(false); $p->featured = 0; $p->save(); } 1 Link to comment Share on other sites More sharing options...
teppo Posted August 9, 2012 Share Posted August 9, 2012 (edited) I do realize that you're suggesting that having the checkbox on the selectable page itself would be better UX wise, but I'd still suggest using a Page field on homepage. This way you can easily allow the user to select only one page AND all required features would already exist -- no need to reinvent the wheel Anyway, if you really want to have that checkbox on each individual page then the module you're suggesting might be the only solution. I'd probably just create a new module and add a hook before Pages save ($this->pages->addHookBefore('save', $this, 'checkFeatured') or something like that) and there (after checking if the value of this checkbox has changed for current page / if current page is a new page and has that checkbox checked etc.) make sure that no other page has the checkbox checked. Edit: Ryan beat me with his answer and pretty much posted the precise code you'd need in the hook I mentioned.. Another option would naturally be allowing multiple featured pages, with some kind of Javascript slideshow enabled to only show one at a time. Edited August 9, 2012 by teppo 1 Link to comment Share on other sites More sharing options...
thijs Posted August 9, 2012 Author Share Posted August 9, 2012 Hey guys, Thanks so much for your replies – I only see them hours later – forgot to ‘follow’ the topic, let me see if there’s a setting in the forum to ‘auto follow’ any topic I start, would that be the ‘Auto follow topics I reply to. ’, I’ll give that a try anyway. The sort=-modified was actually my temporary solution A good quickfix. I’ll give the PageListSelect a try, havent used that field yet. I’m actually using ProcessWire now on a couple of different projects I’m working on, and it’s incredibly smooth so far. Even redid a project that was halfway, simply a matter of setting up the structure and moving some templates, still amazed. Link to comment Share on other sites More sharing options...
apeisa Posted August 9, 2012 Share Posted August 9, 2012 My mind also likes to put "featured" checkbox to actual items... but editors seem to be more comfortable to edit the homepage and choose the featured stuff from there. So now I usually go with the page reference on homepage. 1 Link to comment Share on other sites More sharing options...
thijs Posted August 9, 2012 Author Share Posted August 9, 2012 It does actually make more sense to have it as a page reference on the homepage. But perhaps our minds like checkboxes? 1 Link to comment Share on other sites More sharing options...
apeisa Posted August 9, 2012 Share Posted August 9, 2012 Checkboxes are always nice One reason might be that as a programmers we do think it as a property of a object, or a "feature of that page" etc... But the editor is looking the frontpage and wants to change something there. Also if there can be more than one featured items, then using page reference allows choosing their position etc. And removing items is much simpler also. 1 Link to comment Share on other sites More sharing options...
thijs Posted August 9, 2012 Author Share Posted August 9, 2012 It’s a great feature for sure, and thanks again for your replies! 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