1

I'm "cutting my teeth" in Drupal 8 on the Webform module so these are going to be rather naive questions.

I need to programmatically fill a URL field in the form with an XML file that I generate from the other data in the form and do it just before the submit so that the form is complete. To do so I figured using a webform handler is the way to go and I have been using webform_example_handler as my template.

Now for my ridiculous naivety, just a few basic questions:

In webform_example_handler.schema.yml the first line is: "webform.handler.example". Is this a standard Drupal construct of <webform>.<handler>.<my_module>, or something specific that must be followed set by the webform module or am I completely off base on how this is derived?

I would guess that for my module the line would be: webform.handler.dis_doc_gen?

The "webform.webform.webform_example_handler.yml" file: could someone explain the parts? In my module would it be: webform.webform.dis_doc_gen.yml ?

Last: I have built my webform through the interface so I assume that I don't need the file "webform.webform.webform_example_handler.yml" as that would only be for creating a webform from a yml file upon installing the module as far as I can tell. However, is there something in my webform that I need to set so that it knows about my ".../dis_doc_gen/src/Plugin/WebformHandler/DISDocGenWebformHandler.php" handler?

Could someone please tell me what key thing(s) I need to do to make that connection or is that all automatic as long as you clear the Plugin cache? (I just discovered that cache through the "Admin Toolbar" module).

I realize this could be done through one of the webform hooks but I would rather use a handler as part of getting familiar with Drupal 8 in general.

0

Could someone please tell me what key thing(s) I need to do to make that connection

There are 4 ways that you can tell Drupal that your plugin exists.

You can read about the other 3 at D8 Plugin discovery

In this case Webform uses:

AnnotatedClassDiscovery

The AnnotatedClassDiscovery class uses name of the annotations that contains the plugin definition, e.g., @Plugin, @EntityType, in plugin docblocks to discover plugins, minimizing memory usage during the discovery phase. The AnnotatedClassDiscovery class takes an argument in its constructor, $subdir, which specifies the sub-directory/sub-namespace for this plugin type. The AnnotatedClassDiscovery class scans PSR-4 classes inside those sub-directories of Plugin folders to find plugins (see a "Annotations-based plugins").

Side Note: Entities and blocks also use this "annotation" method.

This "annotation" is seen in webform_example_handler/src/Plugin/WebformHandler/ExampleWebformHandler.php

enter image description here

Yes, this info is inside a comment block, weird, but that's just how it works.


In webform_example_handler.schema.yml the first line is: webform.handler.example. Is this a standard Drupal construct of <webform>.<handler>.<my_module>, or something specific that must be followed set by the webform module or am I completely off base on how this is derived?

Wrong, otherwise it would've been named webform.handler.webform_example_handler.

It's actually webform.handler.<WebformHandler_id> (The ID you use in the annotation).

This is because your module can have multiple plugins, so it makes no sense to use the module's name here.


The webform.webform.webform_example_handler.yml file: could someone explain the parts? In my module would it be: webform.webform.dis_doc_gen.yml ?

Assuming your module is named dis_doc_gen, correct.


Last: I have built my webform through the interface so I assume that I don't need the file webform.webform.webform_example_handler.yml as that would only be for creating a webform from a yml file upon installing the module as far as I can tell

Correct, anything inside the /install folder, runs on install and that file is creating a form. Since you created one via the UI, you don't need this in your use case.

  • I've created the class DisDocGenWebformHandler but since it's already under the namespace Drupal\dis_doc_gen\Plugin\WebformHandler probably DisDocGen or DisDocumentGenerator would be clearer and avoid redundancy. Is there a convention for this or is this a matter of preference? – user5429087 Apr 16 at 8:28
  • @user5429087 yes there is a convention. 10. Classes and interfaces should have names that stand alone to tell what they do without having to refer to the namespace, read well, and are as short as possible without losing functionality information or leading to ambiguity. For more info read Declaring Classes: Naming Conventions – No Sssweat Apr 16 at 8:48
  • Thanks! For anyone else who sees this there is one more step to make it all work, although if you are familiar with D8 you will probably already know this, which is, once you set all this up you must also connect your handler to your form and you do so (in my case since I created the form through the UI) by using Webforms:Email/Handlers:Add Handler where your newly created handler should now show up. You may need to clear the Plugin cache as well. – user5429087 Apr 17 at 8:17

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.