ShadowByte Posted September 15, 2023 Share Posted September 15, 2023 Hello, I hope you can help me. I have a second admin with the role 'admin'. I have set the permissions so far. Then, to test a page (in this case imprint), I gave the admin the rights to edit it. Everything works so far. BUT..... The “normal” admin should only be able to edit content. So the “Settings” tab should be hidden and he shouldn’t be able to change the title either. Is that possible somehow? See the screenshots: Link to comment Share on other sites More sharing options...
bernhard Posted September 15, 2023 Share Posted September 15, 2023 There seems to be a module that does what you need: https://processwire.com/modules/restrict-tab-view/ (at least for the tabs! I haven't used it though). But you can do both quite easily with two hooks in /site/ready.php - unfortunately you can't do everything in one hook because you need to hook "before" and "after" the buildForm(Content): <?php // hook to make the title field non-editable $wire->addHookAfter("ProcessPageEdit::buildFormContent", function ($event) { // get the page being edited $page = $event->process->getPage(); // check if the page has the desired template if ($page->template != 'your-template') return; // check if the user has the desired role if (!$this->wire->user->hasRole('admin')) return; // get the form that was built in buildFormContent() method $form = $event->return; // make the title field non-editable $f = $form->get('title'); $f->collapsed = Inputfield::collapsedNoLocked; }); // hook hide the settings tab $wire->addHookBefore("ProcessPageEdit::buildForm", function ($event) { // get the page being edited $page = $event->process->getPage(); // check if the page has the desired template if ($page->template != 'your-template') return; // check if the user has the desired role if (!$this->wire->user->hasRole('admin')) return; // set noSettings property dynamically $page->template->noSettings = 1; }); 3 Link to comment Share on other sites More sharing options...
ShadowByte Posted September 15, 2023 Author Share Posted September 15, 2023 Thank you very much. I tryed the module "RestrictTabView" and everything works fine for me. But I will keep your hook in my mind, for further pages. Just one question about the first hook for the title: <?php $wire->addHookAfter("ProcessPageEdit::buildFormContent", function ($event) { // get the page being edited $page = $event->process->getPage(); // check if the page has the desired template if ($page->template != 'your-template') return; // check if the user has the desired role if (!$this->wire->user->hasRole('admin')) return; // get the form that was built in buildForm() method $form = $event->return; // make the title field non-editable $f = $form->get('title'); $f->collapsed = Inputfield::collapsedNoLocked; }); If this should be for every page / template, I don't need this part, right? <?php // check if the page has the desired template if ($page->template != 'your-template') return; Greetings Link to comment Share on other sites More sharing options...
bernhard Posted September 16, 2023 Share Posted September 16, 2023 14 hours ago, ShadowByte said: If this should be for every page / template, I don't need this part, right? <?php // check if the page has the desired template if ($page->template != 'your-template') return; correct ? 1 Link to comment Share on other sites More sharing options...
ShadowByte Posted September 17, 2023 Author Share Posted September 17, 2023 Just for thoose who also need a hook to make the title non-editable. This line: <?php // check if the page has the desired template if ($page->template != 'your-template') return; Must be: <?php // check if the page has the desired template if ($page->template->name != 'your-template') return; So the whole hook is: <?php // hook to make the title field non-editable $wire->addHookAfter("ProcessPageEdit::buildFormContent", function ($event) { // get the page being edited $page = $event->process->getPage(); // check if the page has the desired template if ($page->template->name != 'your-template') return; // check if the user has the desired role if (!$this->wire->user->hasRole('admin')) return; // get the form that was built in buildFormContent() method $form = $event->return; // make the title field non-editable $f = $form->get('title'); $f->collapsed = Inputfield::collapsedNoLocked; }); Thank you very much for your help, @bernhard ? 1 Link to comment Share on other sites More sharing options...
bernhard Posted September 17, 2023 Share Posted September 17, 2023 8 hours ago, ShadowByte said: This line: <?php // check if the page has the desired template if ($page->template != 'your-template') return; Must be: <?php // check if the page has the desired template if ($page->template->name != 'your-template') return; Glad you got a working solution, but what you said is not 100% correct ? I used $page->template != 'something' and this will work. But it will only work if you use the "not equal" (!=) operator. It will NOT work, if you use the "not identical" (!==) operator! Check this out: So if you use the two letter comparison (== or !=) then PHP will try to convert your $page->template into a string and ProcessWire will be smart enough to use the template's name. So you'll end with a string == string comparison and you should get the desired result. If you use a three letter comparison (=== or !==) then PHP will not convert $page->template and object === string will always be FALSE and object !== string will always be TRUE. Hope that makes sense ? 1 Link to comment Share on other sites More sharing options...
ShadowByte Posted September 18, 2023 Author Share Posted September 18, 2023 This line: <?php if ($page->template != 'your-template') didn't work. $page->template is an object and in this case it will be always != something. I made a print_r() on $page->template and so I found out that I need $page->template->name to check it. <?php if ($page->template->name != 'your-template') return; works pretty fine. Link to comment Share on other sites More sharing options...
ShadowByte Posted September 23, 2023 Author Share Posted September 23, 2023 Hello, maybe you can help me again. I would like to hide the "Default" option in the admin profile under Admin Theme. Is that somehow possible? There will be 1-2 other themes added later, so I just want to hide “Default”. Thank you in advance 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