prestoav Posted yesterday at 02:20 PM Share Posted yesterday at 02:20 PM Hi folks, Is there any way to setup a checkbox field so that is checked by default? I can't see that in the field admin and where. Thanks! Link to comment Share on other sites More sharing options...
bernhard Posted yesterday at 04:41 PM Share Posted yesterday at 04:41 PM The problem with default yes is that it would not affect already existing items. So if you had 100 pages already, then you add a checkbox with default=1 and you add another 200 pages and then you use a selector "mycheckbox=1" then it would find 100 pages, not 200. Or you'd have to update all 100 pages that already existed before you added the checkbox. Ryan recommended once (if I remember correctly) to use what I'd call the reverse-label-pattern. So instead of showing a checkbox "send email after save" that is default on, you'd add a checkbox that is default off and shows "do NOT send an email after save". Or "no mail after send" or whatever. Or you add a hook on page create that populates the checkbox for you. Or you add a toggle field, that has the option to set default yes/no etc. I think I'd probably use a nice, non-reversed label together with a hook to auto-populate when the page is created. Link to comment Share on other sites More sharing options...
prestoav Posted 12 hours ago Author Share Posted 12 hours ago Hi @bernhard and thanks for taking time to answer my question. I must confess, I had not considered the 'old pages' problem but what you say does makes sense. I'm not keep on the 'double negative' approach to the field instruction as, while it solves the technical issue, it doesn't feel like good UX for the admins, at least in my application. However, the idea of a hook to set that field on page creation does seem like a way forward. Thanks again. Link to comment Share on other sites More sharing options...
bernhard Posted 11 hours ago Share Posted 11 hours ago Agreed. When using Custom Page Classes + MagicPages these kind of things are super easy to do: <?php public function onCreate() { $this->mycheckbox = 1; } But a hook in /site/ready.php would also not be much more work. Just less beautiful in my opinion 🙂 <?php wire()->addHookAfter('Pages::saveReady', function($event) { $p = $event->arguments(0); if($p->id) return; if($p->template != 'whatever') return; $p->mycheckbox = 1; }); Link to comment Share on other sites More sharing options...
prestoav Posted 6 hours ago Author Share Posted 6 hours ago Hi @bernhard, This is super useful. Thank you and have a great weekend! Geoff. 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