Jump to content

Second Admin / Roles


ShadowByte
 Share

Recommended Posts

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:


498689587_Screenshot2023-09-15at16-08-22Rolesadminfgfc820_de.thumb.png.0c1b914479714ca4ac62a6032d14f236.pngsignal-2023-09-13-182103_002.thumb.png.29ead8e32fd95c7fe024f40c6b17576c.png

Link to comment
Share on other sites

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;
});

 

  • Like 3
Link to comment
Share on other sites

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

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 ?

  • Like 1
Link to comment
Share on other sites

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:

mud2zza.png

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 ? 

  • Like 1
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...