WebformFieldsResource.php in Webform REST 8.2
Same filename and directory in other branches
Namespace
Drupal\webform_rest\Plugin\rest\resourceFile
src/Plugin/rest/resource/WebformFieldsResource.phpView source
<?php
namespace Drupal\webform_rest\Plugin\rest\resource;
use Drupal\webform\Entity\Webform;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ModifiedResourceResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Creates a resource for retrieving webform elements.
*
* @RestResource(
* id = "webform_rest_fields",
* label = @Translation("Webform Fields"),
* uri_paths = {
* "canonical" = "/webform_rest/{webform_id}/fields"
* }
* )
*/
class WebformFieldsResource extends ResourceBase {
/**
* Responds to GET requests, returns webform elements.
*
* @param string $webform_id
* Webform ID.
*
* @return \Drupal\rest\ResourceResponse
* HTTP response object containing webform elements.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws HttpException in case of error.
*/
public function get($webform_id) {
if (empty($webform_id)) {
throw new BadRequestHttpException(t("Webform ID wasn't provided"));
}
// Load the webform.
$webform = Webform::load($webform_id);
// Basic check to see if something's returned.
if ($webform) {
// Return only the form elements.
return new ModifiedResourceResponse($webform
->getElementsInitialized());
}
throw new NotFoundHttpException(t("Can't load webform."));
}
}
Classes
Name | Description |
---|---|
WebformFieldsResource | Creates a resource for retrieving webform elements. |