Skip to content
Snippets Groups Projects
Commit 036fd9bd authored by Pieter Frenssen's avatar Pieter Frenssen
Browse files

Issue #3474986: Add support for the ProcessedText element

parent f1840678
No related branches found
No related tags found
2 merge requests!513506037: Add help elements.,!40Add support for the ProcessedText webform element.
Pipeline #286108 passed with warnings
<?php
declare(strict_types=1);
namespace Drupal\graphql_webform\Plugin\GraphQL\DataProducer;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Renders a Webform element.
*
* @DataProducer(
* id = "webform_render_element",
* name = @Translation("Webform render element"),
* description = @Translation("Renders the Webform element."),
* produces = @ContextDefinition("any",
* label = @Translation("Rendered Webform element"),
* ),
* consumes = {
* "element" = @ContextDefinition("any",
* label = @Translation("Webform element as a render array"),
* )
* }
* )
*/
class WebformRenderElement extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
/**
* Constructs a new WebformRenderElement data producer.
*
* @param array $configuration
* The plugin configuration.
* @param string $pluginId
* The plugin ID.
* @param mixed $pluginDefinition
* The plugin definition.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
*/
public function __construct(
array $configuration,
$pluginId,
$pluginDefinition,
protected RendererInterface $renderer,
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('renderer')
);
}
/**
* Renders the Webform element.
*
* @param array $element
* The Webform element as a render array.
*
* @return string
* The rendered Webform element.
*/
public function resolve(array $element): string {
// The Webform module is wrapping its elements in the 'form_element' theme
// wrapper since it is designed to show its forms in the Drupal theming
// layer. Remove the wrapper to render the element as a standalone element.
if (!empty($element['#theme_wrappers'])) {
$element['#theme_wrappers'] = array_diff($element['#theme_wrappers'], ['form_element']);
}
return (string) $this->renderer->renderPlain($element);
}
}
......@@ -206,6 +206,12 @@ class WebformExtension extends SdlSchemaExtensionPluginBase {
fn (array $value) => (WebformElementDisplayOn::tryFrom($value['#display_on'] ?? '') ?? WebformElementDisplayOn::DISPLAY_ON_FORM)->name
));
// WebformElementProcessedText markup.
$registry->addFieldResolver('WebformElementProcessedText', 'markup', $builder
->produce('webform_render_element')
->map('element', $builder->fromParent())
);
// Expose information about elements that accept multiple values.
$registry->addFieldResolver('WebformElementMultipleValuesBase', 'multipleValues', $builder
->produce('webform_element_multiple_values')
......
......@@ -126,6 +126,11 @@ elements: |-
'#multiple__min_items': 2
'#multiple__add_more_button_label': 'I want more'
'#multiple__add_more_items': 2
processed_text:
'#type': processed_text
'#display_on': both
'#text': '<p><strong><em>Processed text</em></strong></p>'
'#format': basic_html
actions:
'#type': webform_actions
'#title': 'Submit button(s)'
......
fragment webformElementMarkup on WebformElementWebformMarkup {
fragment webformElementMarkup on WebformElementMarkupBase {
__typename
key
markup
displayOn
}
......
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\Tests\graphql_webform\Kernel\Element;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\graphql_webform\Kernel\GraphQLWebformKernelTestBase;
/**
......@@ -13,6 +14,37 @@ use Drupal\Tests\graphql_webform\Kernel\GraphQLWebformKernelTestBase;
*/
class MarkupTest extends GraphQLWebformKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'filter',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create a filter format for testing the processed text. We only allow the
// '<strong>' and '<p>' tags. The markup in the test Webform also contains
// the <em> tag, which should be stripped out in the query result.
FilterFormat::create([
'format' => 'basic_html',
'name' => 'Basic HTML',
'weight' => 1,
'filters' => [
'filter_html' => [
'status' => 1,
'settings' => [
'allowed_html' => '<strong><p>',
],
],
],
])->save();
}
/**
* Tests custom markup.
*/
......@@ -24,10 +56,15 @@ class MarkupTest extends GraphQLWebformKernelTestBase {
'elements' => [
5 => [
'__typename' => 'WebformElementWebformMarkup',
'key' => 'markup',
'markup' => '<strong>Markup</strong>',
'displayOn' => 'DISPLAY_ON_FORM',
],
14 => [
'__typename' => 'WebformElementProcessedText',
// The '<em>' tag should be stripped out.
'markup' => '<p><strong>Processed text</strong></p>',
'displayOn' => 'DISPLAY_ON_BOTH',
],
],
],
], $this->defaultCacheMetaData());
......
......@@ -62,6 +62,7 @@ abstract class GraphQLWebformKernelTestBase extends GraphQLTestBase {
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('path_alias');
$this->installSchema('webform', ['webform']);
$this->installConfig(['webform', 'graphql_webform_test']);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment