Problem/Motivation

Webform REST currently does not support text_format elements. Currently it is not possible to ...

  • ... get the form definition if it contains an element of type "text_format"
  • ... submit form values containing the value for an element of type "text_format

Perform GET on elements endpoint results into the following error:

The website encountered an unexpected error. Try again later.<br><br><em class="placeholder">Symfony\Component\Serializer\Exception\NotNormalizableValueException</em>: Could not normalize object of type &quot;Drupal\Core\Url&quot;, no supporting normalizer found. in <em class="placeholder">Symfony\Component\Serializer\Serializer-&gt;normalize()</em> (line <em class="placeholder">190</em> of <em class="placeholder">/var/www/html/vendor/symfony/serializer/Serializer.php</em>). <pre class="backtrace">Symfony\Component\Serializer\Serializer-&gt;normalize(Array, &#039;json&#039;, Array) (Line: 179)
Symfony\Component\Serializer\Serializer-&gt;normalize(Array, &#039;json&#039;, Array) (Line: 179)
Symfony\Component\Serializer\Serializer-&gt;normalize(Array, &#039;json&#039;, Array) (Line: 179)
Symfony\Component\Serializer\Serializer-&gt;normalize(Array, &#039;json&#039;, Array) (Line: 179)
Symfony\Component\Serializer\Serializer-&gt;normalize(Array, &#039;json&#039;, Array) (Line: 140)
Symfony\Component\Serializer\Serializer-&gt;serialize(Array, &#039;json&#039;, Array) (Line: 159)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber-&gt;renderResponseBody(Object, Object, Object, &#039;json&#039;) (Line: 75)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber-&gt;onResponse(Object, &#039;kernel.response&#039;, Object)
call_user_func(Array, Object, &#039;kernel.response&#039;, Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher-&gt;dispatch(Object, &#039;kernel.response&#039;) (Line: 214)
Symfony\Component\HttpKernel\HttpKernel-&gt;filterResponse(Object, Object, 1) (Line: 202)
Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 53)
Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength-&gt;handle(Object, 1, 1) (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength-&gt;handle(Object, 1, 1) (Line: 201)
Drupal\page_cache\StackMiddleware\PageCache-&gt;fetch(Object, 1, 1) (Line: 138)
Drupal\page_cache\StackMiddleware\PageCache-&gt;lookup(Object, 1, 1) (Line: 87)
Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 53)
Asm89\Stack\Cors-&gt;handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState-&gt;handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 741)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)
</pre>

Perform POST on submit endpoint results in the following error (using both flat structure and object-structure) when the field is required:

{
    "message": "Submitted Data contains validation errors.",
    "error": {
        "body": "The field @name is required."
    }
}

Using this form definition:

body:
  '#type': text_format
  '#title': Inhalt
  '#required': true
  '#allowed_formats':
    webform: webform
    webform_default: webform_default
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

fabianrichter created an issue. See original summary.

fabianrichter’s picture

Issue summary: View changes
fabianrichter’s picture

Issue summary: View changes

andresalvarez made their first commit to this issue’s fork.

andres alvarez’s picture

Status: Active » Needs review
andres alvarez’s picture

Problem
The REST endpoint GET /webform_rest/{id}/elements returned a 500 error when the Webform contained text_format elements.

Error
Symfony\Component\Serializer\Exception\NotNormalizableValueException: Could not normalize object of type "Drupal\Core\Url"

Root cause
The Symfony serializer tried to normalize the Webform element definition directly. text_format elements may contain Drupal\Core\Url objects in their configuration, which cannot be automatically serialized to JSON.

Solution

Updated src/Plugin/rest/resource/WebformElementsResource.php.

Added a recursive method normalizeElements(array $elements) to traverse the Webform elements.

When a Drupal\Core\Url instance is found, it is explicitly converted to a string using toString().

The get() method now normalizes the elements with this function before returning the REST response.

Tests

Added a new functional test: tests/src/Functional/WebformRestTextFormatTest.php.

The test creates a Webform with a text_format element, performs a GET request to the endpoint, and asserts a 200 OK response with valid element data.

Result
The REST endpoint now works correctly with text_format elements, resolving the serialization error.

fabianrichter’s picture

Looks really good to me and solves the issue.
One note: I was not able to apply the patch because of the different use/import order and the missing t() wrapper function around the response (_t("Webform ID wasn't provided") and _t("Can't load webform.")).
Other than that I don't see any blocker for a merge.
Thank you!

fabianrichter’s picture

I was not able to apply the patch → merge diff as a patch ...