You are here

public function WebformSubmissionResource::get in Webform REST 8.2

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

Retrieve submission data.

Parameters

string $webform_id: Webform ID.

int $sid: Submission ID.

Return value

\Drupal\rest\ModifiedResourceResponse HTTP response object containing webform submission.

Throws

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

File

src/Plugin/rest/resource/WebformSubmissionResource.php, line 55

Class

WebformSubmissionResource
Creates a resource for retrieving webform submission data.

Namespace

Drupal\webform_rest\Plugin\rest\resource

Code

public function get($webform_id, $sid) {
  if (empty($webform_id) || empty($sid)) {
    $errors = [
      'error' => [
        'message' => 'Both webform ID and submission ID are required.',
      ],
    ];
    return new ModifiedResourceResponse($errors);
  }

  // Load the webform submission.
  $webform_submission = WebformSubmission::load($sid);

  // Check for a submission.
  if (!empty($webform_submission)) {
    $submission_webform_id = $webform_submission
      ->get('webform_id')
      ->getString();

    // Check webform_id.
    if ($submission_webform_id == $webform_id) {

      // Grab submission data.
      $data = $webform_submission
        ->getData();
      $response = [
        'entity' => $webform_submission,
        'data' => $data,
      ];

      // Return the submission.
      return new ModifiedResourceResponse($response);
    }
  }
  throw new NotFoundHttpException(t("Can't load webform submission."));
}