Skip to content
Snippets Groups Projects

Add "renderedForm" property to webform that returns the HTML

1 file
+ 77
0
Compare changes
  • Side-by-side
  • Inline
<?php
namespace Drupal\graphql_webform\Plugin\GraphQL\Fields\Webform;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use GraphQL\Type\Definition\ResolveInfo;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a field handler which renders an webform into HTML.
*
* @GraphQLField(
* secure = true,
* parents = {"Webform"},
* id = "webform_rendered_form",
* name = "renderedForm",
* type = "String",
* )
*/
class WebformRenderedForm extends FieldPluginBase implements ContainerFactoryPluginInterface {
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static(
$configuration,
$pluginId,
$pluginDefinition,
$container->get('renderer')
);
}
/**
* WebformRenderedForm constructor.
*
* @param array $configuration
* The plugin configuration array.
* @param string $pluginId
* The plugin id.
* @param mixed $pluginDefinition
* The plugin definition.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(
array $configuration,
$pluginId,
$pluginDefinition,
RendererInterface $renderer,
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
$builder = \Drupal::service('entity_type.manager')->getViewBuilder('webform');
$view = $builder->view($value);
yield $this->renderer->render($view);
}
}
Loading