Commit 15b7ce16 authored by Nicole Sciacca's avatar Nicole Sciacca Committed by Shawn Duncan
Browse files

Issue #3323199 by nsciacca: Form loading error 404 WSOD in D9

parent 76d477c2
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Utility\Token;
use Drupal\formassembly\Entity\FormAssemblyEntity;
use GuzzleHttp\Client;
@@ -28,6 +29,8 @@ use Psr\Log\LoggerInterface;
 */
class ApiMarkup extends ApiBase {

  use StringTranslationTrait;

  /**
   * GuzzleHttp\Client definition.
   *
@@ -149,16 +152,20 @@ class ApiMarkup extends ApiBase {
    // Make FA rest call and return form markup.
    $url = $this->getUrl('/rest/forms/view/' . $entity->faid->value);
    $url->setOptions(['query' => $params]);
    $request = $this->httpClient->get($url->toString(TRUE)->getGeneratedUrl());

    // Guzzle throws an Exception on http 400/500 errors.
    // Ensure we have a 200.
    if ($request->getStatusCode() != 200) {
      throw new \UnexpectedValueException(
        'Http return code 200 expected.  Code ' . $request->getStatusCode() . ' received.'
      );
    }
    try {
      $request = $this->httpClient->get($url->toString(TRUE)->getGeneratedUrl());
      return $request->getBody()->getContents();
    }
    catch (\Exception $e) {
      $this->logger->error('Form :id failed to load. Http return code 200 expected. Code ' . $e->getCode() . ' received.', [
        ':id' => $entity->faid->value,
      ]);
      return '<div class="wFormContainer"><div class="wFormFailed">' . $this->t('Form failed to load.') . '</div></div>';
    }
  }

  /**
   * Get the HTML for a FormAssembly next path using returned tfa_next value.