-2

I'm designing a form that creates a user on sign in. After that I'm displaying the data in a view for an admin to approve. I need to add custom PHP code that calls a function that performs approval of users. How can I do that?

In D7, we can add Global:views PHP field to custom php code. Likewise I need that in D8 also.

  • Any suggestions? – Max28 Sep 5 at 6:06
0

yes in d7 we had php but in d8 (for good reason) this is not the case. You can however create a controller to do the approval functionality,

then in the view create a button or link that passes the user id to it

such as in view make sure you have the user id field then make a new filed with custom text

<a href="/your-controller-path-to-approve-user/{{uid}}>Approve user</a>

then in a custom module create a controller see:

1) https://www.drupal.org/docs/8/creating-custom-modules/adding-a-basic-controller

2) https://www.drupal.org/docs/8/creating-custom-modules/add-a-routing-file

then change the route to use your path and better permission

yourmodule.content:

path: '/your-controller-path-to-approve-user/{user_id}'

....

_permission: 'Administer users'

then in your controller method example

   public function content($user_id) {
     //do the stuff to approve the user here.
   }

for bonus points you can look in to making it use ajax. eg https://api.drupal.org/comment/62725#comment-62725

and if this is still not your thing then you will need to look in to

creating custom Twig extensions but far better to go the controller route.

  • Thanks Taggart. It worked perfectly!! – Max28 Sep 6 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.