1

What are the requirements for submitting the contact form using JSON:API?

I enabled the core Contacts module and set up a custom site-wide contact form. I gave permission to anon + auth users to submit the form.

Next, I enabled JSON:API. I can see the contact form at /jsonapi/contact_form/contact_form:

"data":[
   {
      "type":"contact_form--contact_form",
      "id":"d97e73f1-7d06-4bf0-b05b-1c654a659dfc",
      "links":{
         "self":{
            "href":"https://example.lndo.site/jsonapi/contact_form/contact_form/d97e73f1-7d06-4bf0-b05b-1c654a659dfc"
         }
      },
      "langcode":"ja",
      "status":true,
      "dependencies":[
         
      ],
      "drupal_internal__id":"example_support",
      "label":"Example Support",
      "recipients":[
         "[email protected]"
      ],
      "reply":"",
      "weight":0,
      "message":"Thank you for submitting.",
      "redirect":""
   }
],

But, what is the endpoint I need to POST to in order to submit this form?

I found this issue about emails not getting sent when a contact form is submitted via REST.

That issue suggests that the /entity/contact_message REST endpoint must be enabled to submit the contact form-- is this also true for JSON:API?

I'm confused because using JSON:API, I can submit nodes without enabling any REST endpoints by POSTing to /jsonapi/node/content_type.

1
  • json api is not dependent on rest endpoint, for almost every situations making a post request to Entity_type/Bundle will cause creation of new entity of that bundle, so If that does not work then maybe it is related to json api module but I think enabling a rest endpoint shouldn't help in this situation Commented Jan 29, 2022 at 5:56

1 Answer 1

2

It seems a REST endpoint is required in this case because although contact forms are entities, contact form submissions are not entities unless you use the Contact Storage module.

However, given the core issue with notifications not being sent and the large number of RTBC and "Needs review" issues in the Contact Storage issue queue as of January 30, 2022, I decided to take a different approach. If a user is upset and reaches out for support, it's especially terrible to have some bug cause their submission to go missing or the response to be late.

So I solved this using the Webform REST module:

  1. Install the module.
  2. Enable the Webform submit REST resource using REST UI module.
  3. Create the webform.
  4. Add code to the frontend app to submit the form.

I got this working in about an hour, and the only additional change I have had to make so far is to patch Webform REST so that I can uninstall REST UI after enabling the endpoint.

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.