Score:1

How to send a contact form via JSON:API?

cn flag

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.

Alireza Tabatabaeian avatar
cn flag
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
Score:1
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.