0

I have a custom form:

\Drupal\module\Form\CustomForm

I want to be able to embed this form in a basic page by adding a shortcode/variable to the basic page such as:

[form][/form]

or

form

This is very easy to achieve in WordPress but really difficult in Drupal.

How can I achieve the above?

3
  • 2
    Thank goodness there are no "short codes" in Drupal. The closest thing to short codes are tokens in Drupal, but they are usually used for other purposes. I would advise to take a look at the Webforms module instead.
    – Hudri
    Commented Jul 19, 2018 at 15:39
  • 1
    The Webforms module allows you to create a form. I have a form created already, I just need to be able to provide the user with a way to embed the form very easily. Commented Jul 19, 2018 at 15:51
  • And beyond creating forms it has means to easily show, embed and manage forms. If Webforms is not on the table, take a look at the Twig tweak module, it has a {{ drupal_form }} template operator, but this is for coders/designers, not for users/editors like Webforms.
    – Hudri
    Commented Jul 19, 2018 at 16:05

1 Answer 1

0

As also WordPress is now moving towards modular content (codename Gutenberg) I'd advise you to do the same in Drupal with Paragraphs.

Then you can provide different paragraph types (image, text, CTA etc.), one of them could be a simple block reference (I don't remember if core already offers block reference fields or if you need Block field for that) with which you let editors choose blocks to be placed wherever they want. (Paragraphs also are resortable.)

So, you already have your form, cool. Next level is to let your custom module provide a block that outputs this form. Next, In your theme add a new region "Disabled" (that never gets printed anywhere) and place your block there. Now this block will be available to choose as block reference. (I guess it's desired behavior that only blocks that are assigned to a region can be referenced.)

Bonus would be a little bit of back-end JS to only display certain blocks in the block reference select list. But that's just bonus.

6
  • Thanks, this helps a lot. Do you have any information on how to programatically create paragraphs? i want the user to be able to add these on any basic page, not a pre-defined one. This step, "Next level is to let your custom module provide a block that outputs this form", how do I achieve that? I'm new to Drupal. Commented Jul 19, 2018 at 21:16
  • @SergiuTripon - You are on D8, create the paragraph types you want, export config, git push, git pull on Prod, import config, voila. (Codename: configuration management.) On how to programmatically create blocks you'll find a lot of good tuts out there. In principle it's not much different from creating custom forms.
    – leymannx
    Commented Jul 19, 2018 at 21:22
  • I'm extremely new to Drupal so I'll need a bit more than this. Paragraphs can be added in the config? Commented Jul 19, 2018 at 21:30
  • @SergiuTripon - "I need more than that"? Sorry my friend I'm not a consultant and Drupal Answers is narrow Q&A. Everything else is getting too broad. I gave you enough to kickstart on your own. It's your turn now to find out how you get there.
    – leymannx
    Commented Jul 19, 2018 at 21:42
  • Understood. I'm stuck on "Next, In your theme add a new region "Disabled" (that never gets printed anywhere) and place your block there" now, I'm not sure the syntax to set a region as disabled as reading on the internet, some say this isn't possible anymore. Thanks. Commented Jul 20, 2018 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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