Jump to content

Field access on runtime


elabx
 Share

Recommended Posts

Hi everyone! Does anyone know if I can tweak field access on runtime to hide fields in ProcessPageEdit depending on some other field conditions. Eg. I want users to be able to edit the data if they are asigned to said page, or they created it and if not, hide the fields. Right now I'm wiping the fields with a hook on ProcessPageEdit::buildFormContent, but for example, I'd like to leave the title field intact and some other "common" fields amongst the users.

Link to comment
Share on other sites

2 hours ago, Robin S said:

You can use my Custom Inputfield Dependencies module to do this.

Wow thanks! With the module,  is it possible to render the fields with their renderValue() ? Like the access permission does! It let's you view the inputfield, but doesn't display as an actual input but just the value. 

Thanks for your help! Will definitely take a look at your module.

Link to comment
Share on other sites

5 hours ago, elabx said:

With the module,  is it possible to render the fields with their renderValue() ?

No, it only allows for the showing or hiding of fields, not setting the render value mode.

But if you want to selectively render the value of fields rather than the full inputfield you could use hooks like below:

$wire->addHookBefore('ProcessPageEdit::buildForm', function(HookEvent $event) {
	/* @var ProcessPageEdit $ppe */
	$ppe = $event->object;
	$page = $ppe->getPage();
	// Some test on $page here, for example...
	if($page->template != 'news_item') return;
	$event->wire()->addHookBefore('InputfieldWrapper::renderInputfield', function(HookEvent $event) {
		/* @var Inputfield $inputfield */
		$inputfield = $event->arguments(0);
		// Some test on $inputfield here, or on the field object via ->hasField
		if($inputfield->name === 'text_1') {
			// Inputfield should have only its value rendered, so set render value mode argument to true
			$event->arguments(1, true);
		}
	});
});

 

  • Like 3
Link to comment
Share on other sites

7 hours ago, Robin S said:

No, it only allows for the showing or hiding of fields, not setting the render value mode.

But if you want to selectively render the value of fields rather than the full inputfield you could use hooks like below:


$wire->addHookBefore('ProcessPageEdit::buildForm', function(HookEvent $event) {
	/* @var ProcessPageEdit $ppe */
	$ppe = $event->object;
	$page = $ppe->getPage();
	// Some test on $page here, for example...
	if($page->template != 'news_item') return;
	$event->wire()->addHookBefore('InputfieldWrapper::renderInputfield', function(HookEvent $event) {
		/* @var Inputfield $inputfield */
		$inputfield = $event->arguments(0);
		// Some test on $inputfield here, or on the field object via ->hasField
		if($inputfield->name === 'text_1') {
			// Inputfield should have only its value rendered, so set render value mode argument to true
			$event->arguments(1, true);
		}
	});
});

 

Awesome! Thanks a lot for your help! Actually found this myself yesterday working through the PW source code (I really LOVE being able to do this).

  • Like 1
Link to comment
Share on other sites

Hi everyone! Just came back with another issue trying to solve this "dynamic field access" thing. 

I've implemented the previous hook @Robin S kindly gave me, but I now have the issue that if I visit the page a second time and save the page again while the conditions within are met (I visit the page a second time, and the fields show as their rendervalue) , the fields data gets deleted!

Anyone got any clues on this? I wonder if I have to put a flag somwhere when the form is submitted?

Link to comment
Share on other sites

I solved this like this:

$wire->addHookBefore('ProcessPageEdit::buildForm', function(HookEvent $event) {
	$ppe = $event->object;
	$page = $ppe->getPage();
	if($page->template != 'transaccion') return;
    $pageInputfields = $page->getInputfields()->children();

    if($event->user->hasRole('asesor') && $page->transaccion_enviar_admin){
        foreach($page->getFields() as $field){
            $event->addHookAfter("Field::editable", function($event){
                $event->return = false;
        
            });
        }
    }
    
});

BUT, I have to turn on access control on the fields and set the fields to be viewable even if not editable. (the checkboxes at the bottom)

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...