Rich Torres Posted August 18, 2017 Posted August 18, 2017 Hello there fellow PW people, I am looking to feature a different page every week on the home page. I am building this web site, but not maintaining the content. I wanted some thing like a checkbox that would turn a page into a 'featured' page. this checkbox could only be used by one page at any given time. So if another page is chosen as featured, it would have to lose this value. I am kind of at a loss where to start. I have done some searching, but perhaps my keywords are not allowing something I am looking for to pop up. Thanks in advance! Rich Torres
abdus Posted August 18, 2017 Posted August 18, 2017 How about something like this? Create a Checkbox field called featured, add it to a template Hook into Page::savedField, return if field name is not 'featured' Check if $page ($page = $event->arguments(0)) has $page->featured=1 Perform a $pages->find("featured=1, id!=$page") Iterate over those pages and set featured=0 Now one potential problem that comes to my mind is that as you're iterating over these pages, the hook will be called over and over. Setting $event->cancelHooks = true before you do the iteration should cancel subsequent hooks, but I haven't tried it. 1
Rich Torres Posted August 18, 2017 Author Posted August 18, 2017 I'm an idiot... page reference field on home page... all those duckduckgo searches... wasted abdus, thank you for such a hasty reply. I will make note of this, as I am sure this will help me further my understanding. I am sure this info will come in useful in the near future.
Peter Knight Posted August 19, 2017 Posted August 19, 2017 I always found it easier to have a page select field on my homepage. That way you have a single point for determining the featured page and don't have to trawl through a site tree to determine which page(s) are already checked. 1
szabesz Posted August 19, 2017 Posted August 19, 2017 3 hours ago, Peter Knight said: I always found it easier to have a page select field on my homepage. Me too
tpr Posted August 19, 2017 Posted August 19, 2017 Why not just get the page that has featured=1 and set to 0 on save hook, instead of iterating?
abdus Posted August 19, 2017 Posted August 19, 2017 There should be at most 1 page with featured=1, but $pages->find() returns PageArray, you'd have to iterate over it but it's just semantics. What I'm interested is how you'd get a hook to run just once and stop further hooks from getting triggered inside that hook.
tpr Posted August 20, 2017 Posted August 20, 2017 That's why I wrote "get" instead find. On a page save hook you could save the field so no additional page save would be triggered. http://cheatsheet.processwire.com/page/built-in-methods-reference/page-save-field/
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