Skip to content
Snippets Groups Projects

Support checkboxes in GraphQL 4.

Files
19
<?php
namespace Drupal\graphql_webform\Plugin\GraphQL\DataProducer;
use Drupal\Core\Render\Element;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\webform\WebformInterface;
/**
* Return the webform items.
*
* @DataProducer(
* id = "webform_items",
* name = @Translation("Webform Items"),
* description = @Translation("Returns the webforms items (elements)."),
* produces = @ContextDefinition("any",
* label = @Translation("Webform Items"),
* required = FALSE
* ),
* consumes = {
* "webform" = @ContextDefinition("entity:webform",
* label = @Translation("Webform")
* ),
* "form" = @ContextDefinition("any",
* label = @Translation("Webform Submission Form")
* )
* }
* )
*/
class WebformItems extends DataProducerPluginBase {
/**
* Resolver.
*/
public function resolve(WebformInterface $webform, array $form, FieldContext $field) {
$elements = empty($form['elements']) ? $form : $form['elements'];
$childKeys = Element::children($elements, TRUE);
$data = $webform->getElementsDecodedAndFlattened('view');
$items = [];
foreach ($childKeys as $key) {
$element = $elements[$key] ?? NULL;
if ($element) {
$element['#graphql_element_decoded'] = $data[$key] ?? NULL;
$items[] = $element;
}
}
return $items;
}
}
Loading