You are here

public function WebformElementsResource::get in Webform REST 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/rest/resource/WebformElementsResource.php \Drupal\webform_rest\Plugin\rest\resource\WebformElementsResource::get()
  2. 8.2 src/Plugin/rest/resource/WebformElementsResource.php \Drupal\webform_rest\Plugin\rest\resource\WebformElementsResource::get()
  3. 4.x src/Plugin/rest/resource/WebformElementsResource.php \Drupal\webform_rest\Plugin\rest\resource\WebformElementsResource::get()

Responds to GET requests, returns webform elements.

Parameters

string $webform_id: Webform ID.

Return value

\Drupal\rest\ResourceResponse HTTP response object containing webform elements.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException Throws HttpException in case of error.

File

src/Plugin/rest/resource/WebformElementsResource.php, line 35

Class

WebformElementsResource
Creates a resource for retrieving webform elements.

Namespace

Drupal\webform_rest\Plugin\rest\resource

Code

public function get($webform_id) {
  if (empty($webform_id)) {
    throw new HttpException(t("Webform ID wasn't provided"));
  }

  // Load the webform.
  $webform = Webform::load($webform_id);

  // Basic check to see if something's returned.
  if ($webform) {

    // Grab the form in its entirety.
    $form = $webform
      ->getSubmissionForm();

    // Return only the form elements.
    return new ModifiedResourceResponse($form['elements']);
  }
  throw new HttpException(t("Can't load webform."));
}