3

I have a configuration form that if it wasn't previousely configured should be the only thing that the user is able to see.

I would like to hijack the response for the current page and display the form, no matter what url the user is currently on. I could do a redirect but I would rather learn how to do it this way.

I presume this is done via EventSubscriber, I'm just not exactly sure how. Do I have to render the form and set is as response? Or can I just alter the controller for active route? Which would seem more DX friendly to me.

*I am not looking for contrib module to do this.

--

I was looking at the kernel events but the only viable events were

  • request - which already had the route "loaded"
  • controller - which is fine but in most cases it requires DI stuff so I cannot swap it so easily, and
  • response - too late, creating a new repsonse is not good for performance
| improve this question | | | | |
  • I would do it that way: preprocess_page condition to check if the variable_get of the form are empty, if yes return the form (drupal_get_form) ? – GwenM Jun 11 '15 at 10:36
  • Not a bad idea. I am not 100% sure that submitting the form will work though but I'll try. – user21641 Jun 12 '15 at 11:34
  • well if you call it with drupal_get_form there shouldn't be any problems. Otherwise you can drupal_retrieve_form(), or overide it with form_alter. – GwenM Jun 12 '15 at 13:44
0
+100

You could implement a RouteEnhancer that modifies the _controller entry in the route object to point to a controller which loads your form using FormBuilder::getForm().

Another (heavier) approach would be to use a RouteSubscriber to alter all routes and change the path: entry to point to a path you define. The benefit of this approach is you control the entire output (instead of just the "main content"), including the menu tabs. However, it's not dynamic since routes are cached.

| improve this answer | | | | |
1

It could be done with Rules:

The Rules module allows site administrators to define conditionally executed actions based on occurring events (known as reactive or ECA rules). It's a replacement with more features for the trigger module in core and the successor of the Drupal 5 workflow-ng module.

  • set up a field for the user, boolean and use Field Permissions to hide it from users:

The Field Permissions module allows site administrators to set field-level permissions to edit, view and create fields on any entity.

  • Rules, Event - Drupal is Initialising
  • Condition - Data field is empty, user field previously set up
  • Action - redirect to form

Then you can set up an override for the form submit to set the user data field (or use Rules to do it), so that the condition is no longer met.

| improve this answer | | | | |
  • I just noticed there's no 8 version for Field permissions - so this may not work as intended, depending on how its set up – Geoff May 11 '15 at 19:48
  • Thanks but I'm not interested in contrib modules. I want to do this on my own. I have updated the description to be more clear. – user21641 May 11 '15 at 20:45
  • d8rules module is not ready for production yet – milkovsky May 19 '15 at 20:48
  • 1
    @milkovsky neither is d8 – Geoff May 19 '15 at 20:49
0

I would alter the controller for the active route, seems much cleaner and more accurate to the intended purpose.

You may also want to conditionally alter it based on permissions. i.e. this might be the desired case for admins/site builders/etc but could be really dangerous for a general user to be presented with a config form.

| improve this answer | | | | |
  • How would you dynamically alter the route definition if routes are cached? – user21641 Jun 17 '15 at 14:25

Your Answer

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