Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example Webform usage #86

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
03a00a2
GET
harumijang Jul 26, 2022
65892b5
added submit method with POST but client not working
harumijang Jul 26, 2022
2427a9e
working submit and removed markdown
harumijang Jul 27, 2022
756c24c
added start of dynamic form element rendering
harumijang Jul 27, 2022
19b5993
support multiple forms on one node
harumijang Jul 28, 2022
62d5a25
dynamically pass in webform_id
harumijang Jul 28, 2022
1c512f4
dynamically build body request
harumijang Jul 28, 2022
d508023
started checkboxes/radio for POST
harumijang Jul 28, 2022
45b1c00
Merge branch 'main' into webforms
harumijang Jul 29, 2022
8188daf
Merge branch 'main' into webforms
harumijang Aug 1, 2022
42ef37e
added POST support for checkboxes, radio buttons, single checkbox
harumijang Aug 1, 2022
83399c0
Merge branch 'main' into webforms
harumijang Aug 1, 2022
74f4cf6
Moved webform related functions to lib directory
harumijang Aug 1, 2022
4203766
custom API GET works, moved POST into api/webform
harumijang Aug 2, 2022
b5d23e0
added super basic styling
harumijang Aug 2, 2022
db7fda1
Move webform rendering to /webform, pass in additionalContent to all …
harumijang Aug 2, 2022
c9c684e
Created Webform component and created types.ts file
harumijang Aug 3, 2022
f26db83
Use new types
harumijang Aug 3, 2022
46e2521
Started on custom components
harumijang Aug 3, 2022
be9f34a
Added example of working example of custom component
harumijang Aug 4, 2022
3486357
Merge branch 'main' into webforms
harumijang Aug 4, 2022
809164e
Merge branch 'main' into webforms
harumijang Aug 8, 2022
cabaadc
Added component wrapper for radio buttons and print validation msg
harumijang Aug 8, 2022
44134a4
Render validation error msg for one webform element
harumijang Aug 8, 2022
c9e0cf3
Added more wrapper components for webform elements
harumijang Aug 9, 2022
45bcb59
added wrapper for custom component and checkbox
harumijang Aug 9, 2022
07f85c5
prettier
harumijang Aug 9, 2022
1673ff2
add webform debug fallback component and some css for my sanity
harumijang Aug 10, 2022
87c025e
Added error and success form message
harumijang Aug 10, 2022
17451e4
Merge branch 'main' into webforms
harumijang Aug 10, 2022
8b11f33
Moved webforms to different pkg and added README
harumijang Aug 11, 2022
bda6dfb
add back line
harumijang Aug 11, 2022
1f1ac35
Updated readme
harumijang Aug 12, 2022
8e8cf49
Removed client from next webform
harumijang Aug 12, 2022
f031328
Use backend value for name and add comment about type in custom compo…
harumijang Aug 12, 2022
4191cb7
readme
harumijang Aug 12, 2022
26e6c44
removed webform
harumijang Aug 17, 2022
69d9b0a
Use webform from exports
lauriii Aug 17, 2022
db03401
Add second arg to webform method and add condition to node-article
harumijang Aug 17, 2022
cfe7e4b
uncomment custom component
harumijang Aug 18, 2022
e045044
Pass in client info
harumijang Aug 19, 2022
77ddec6
added submit api to starter and package
harumijang Aug 22, 2022
6ed31cd
Pass url into submit instead of client
harumijang Aug 23, 2022
6dde63c
added get api
harumijang Aug 25, 2022
374d84d
Merge branch 'main' into webforms
harumijang Sep 19, 2022
47664dc
Merge branch 'main' into webforms
harumijang Sep 23, 2022
752c3e8
Remove client as arg from getWebformFields
harumijang Aug 29, 2022
7bddac9
Change custom webform component
harumijang Sep 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions WEBFORMS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
How to set up Webform REST on ACMS

1.Download the Webform and Webform REST modules

2.Enable REST resources "Webform Submit", "Webform Elements", "Webform Submission" on `/admin/config/services/rest`

3.Set permissions `/admin/people/permissions#module-rest` for RESTful Web Services and check Anonymous User for the Webform related permissions to avoid dealing with auth/headers for now.

4.Create a new webform if you have not already `/admin/structure/webform`

5.Code is currently hardcoded to render the basic email contact webform on the page which I think should already be pre-made as an example
on `/admin/structure/types/manage/article/fields`.
In case it's not, create a webform where
- webform_id should be 'contact'
- there should be a name field, email field, subject field, and message field.

6.Add a webform to one of the article nodes.

7.Visit the article node on the headless site.

8.Now you should be able to make requests to the API too. ex) GET`/webform_rest/{webform_id}/fields?_format=json`

70 changes: 69 additions & 1 deletion starters/basic-starter/components/node--article.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,81 @@ import Link from 'next/link';
import { formatDate } from 'lib/format-date';
import { MediaImage } from 'components/media--image';
import { FormattedText } from 'components/formatted-text';
import React, { useState } from 'react';
import { drupal } from '../lib/drupal';

export function NodeArticle({ node, ...props }) {
const initialValues = {
name: '',
email: '',
subject: '',
message: '',
};

export function NodeArticle({ node, additionalContent, ...props }) {
const [values, setValues] = useState(initialValues);

function handleInputChange(
e:
| React.ChangeEvent<HTMLInputElement>
| React.ChangeEvent<HTMLTextAreaElement>,
) {
const { name, value } = e.target;
setValues({
...values,
[name]: value,
});
console.log(e.target);
}

// Commenting this function out will make page render
async function submitForm() {
const baseUrl = await drupal.baseUrl;
harumijang marked this conversation as resolved.
Show resolved Hide resolved
const path = '/webform_rest/submit?_format=json';
const body = {
webform_id: 'contact',
name: values['name'],
email: values['email'],
subject: values['subject'],
message: values['message'],
};
const response = await drupal.fetch(`${baseUrl}${path}`, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
},
});
console.log(response.json());
}
return (
<article className="max-w-2xl px-6 py-10 mx-auto" {...props}>
<h1 className="mb-4 text-3xl font-black leading-tight md:text-4xl">
{node.title}
</h1>
<h2>Contact</h2>
<input
placeholder={additionalContent.webform.name['#title']}
name={additionalContent.webform.name['#webform_key']}
onChange={handleInputChange}
/>
<input
placeholder={additionalContent.webform.email['#title']}
name={additionalContent.webform.email['#webform_key']}
onChange={handleInputChange}
/>
<input
placeholder={additionalContent.webform.subject['#title']}
name={additionalContent.webform.subject['#webform_key']}
onChange={handleInputChange}
/>
<textarea
placeholder={additionalContent.webform.message['#title']}
name={additionalContent.webform.message['#webform_key']}
onChange={handleInputChange}
/>
<button type="submit" onClick={() => submitForm()}>
{additionalContent.webform.actions['#submit__label']}
</button>
<p className="mb-4 text-gray-600">
{node.field_display_author?.title ? (
<span>
Expand Down
19 changes: 17 additions & 2 deletions starters/basic-starter/pages/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// It is the entry point for handling entity routes from Drupal.
import * as React from 'react';
import { GetStaticPathsResult, GetStaticPropsResult } from 'next';
import { DrupalNode, DrupalTaxonomyTerm } from 'next-drupal';
import { DrupalClient, DrupalNode, DrupalTaxonomyTerm } from 'next-drupal';
import { DrupalJsonApiParams } from 'drupal-jsonapi-params';

import { getMenus } from 'lib/get-menus';
Expand Down Expand Up @@ -49,7 +49,10 @@ export default function EntityPage({
<NodeBasicPage node={entity as DrupalNode} />
)}
{entity.type === 'node--article' && (
<NodeArticle node={entity as DrupalNode} />
<NodeArticle
node={entity as DrupalNode}
additionalContent={additionalContent as { webform: any }}
/>
)}
{entity.type === 'node--event' && (
<NodeEvent node={entity as DrupalNode} />
Expand Down Expand Up @@ -102,6 +105,7 @@ export async function getStaticPaths(): Promise<GetStaticPathsResult> {
export async function getStaticProps(
context,
): Promise<GetStaticPropsResult<EntityPageProps>> {
// console.log(await getForm());
// Find a matching path from Drupal from context.
const path = await drupal.translatePathFromContext(context);

Expand Down Expand Up @@ -142,7 +146,9 @@ export async function getStaticProps(
'field_article_media.image',
'field_article_image.image',
'field_display_author',
'field_webform',
]);
additionalContent['webform'] = await getForm();
}

if (type === 'node--event') {
Expand Down Expand Up @@ -245,3 +251,12 @@ export async function getStaticProps(
revalidate: 60,
};
}

async function getForm() {
const baseUrl = drupal.baseUrl;
const path = '/webform_rest/contact/fields?_format=json';
const response = await drupal.fetch(`${baseUrl}${path}`, {
method: 'GET',
});
return response.json();
}