class ApiMarkup in FormAssembly 8
Service class for FormAssembly API: Fetches form markup.
@author Shawn P. Duncan <code@sd.shawnduncan.org>
Copyright 2018 by Shawn P. Duncan. This code is released under the GNU General Public License. Which means that it is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. http://www.gnu.org/licenses/gpl.html @package Drupal\formassembly
Hierarchy
Expanded class hierarchy of ApiMarkup
1 file declares its use of ApiMarkup
- FormAssemblyEntityForm.php in src/
Form/ FormAssemblyEntityForm.php
1 string reference to 'ApiMarkup'
1 service uses ApiMarkup
File
- src/
ApiMarkup.php, line 29
Namespace
Drupal\formassemblyView source
class ApiMarkup extends ApiBase {
/**
* GuzzleHttp\Client definition.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* Injected authorization service.
*
* @var \Drupal\formassembly\ApiAuthorize
*/
protected $authorize;
/**
* Injected service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Injected service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $tokens;
/**
* Injected Service.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Injected Service.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $userProxy;
/**
* ApiSync constructor.
*
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* Injected config service.
* @param \Psr\Log\LoggerInterface $loggerChannel
* Injected service.
* @param \GuzzleHttp\Client $http_client
* Injected Guzzle client.
* @param \Drupal\formassembly\ApiAuthorize $authorize
* Injected api service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* Injected service.
* @param \Drupal\Core\Utility\Token $tokenService
* Injected service.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* Injected service.
* @param \Drupal\Core\Session\AccountProxyInterface $userProxy
* Injected service.
*/
public function __construct(ConfigFactory $config_factory, LoggerInterface $loggerChannel, Client $http_client, ApiAuthorize $authorize, ModuleHandlerInterface $moduleHandler, Token $tokenService, RouteMatchInterface $routeMatch, AccountProxyInterface $userProxy) {
parent::__construct($config_factory, $loggerChannel);
$this->httpClient = $http_client;
$this->authorize = $authorize;
$this->moduleHandler = $moduleHandler;
$this->tokens = $tokenService;
$this->routeMatch = $routeMatch;
$this->userProxy = $userProxy;
}
/**
* Retrieve the HTML for a FormAssembly form.
*
* The FA API recognizes query parameters passed on the rest URL and will
* use them to pre-fill fields in the returned form markup. Here we fold
* in parameters configured via formassembly_form() and expose the hook
* hook_formassembly_form_params_alter(&$params) to allow modules to modify
* the passed parameter list.
*
* @param \Drupal\formassembly\Entity\FormAssemblyEntity $entity
* Entity form object.
*
* @return string
* HTML representation of the form.
*/
public function getFormMarkup(FormAssemblyEntity $entity) {
$params = [];
if (!$entity->query_params
->isEmpty()) {
$params = $entity->query_params->value;
}
// Expose hook_formassembly_form_params_alter()
$this->moduleHandler
->alter('formassembly_form_params', $params);
// Replace any tokens found in the parameter pair values.
$data = [
'user' => $this->userProxy
->getAccount(),
];
foreach ($this->routeMatch
->getParameters() as $parameter) {
if ($parameter instanceof ContentEntityInterface) {
$data[$parameter
->getEntityTypeId()] = $parameter;
}
}
foreach ($params as $key => $value) {
$params[$key] = $this->tokens
->replace($value, $data, [
'clear' => TRUE,
]);
}
$params = array_filter($params, function ($value) {
return !is_null($value) && $value !== '';
});
// 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.');
}
return $request
->getBody()
->getContents();
}
/**
* Get the HTML for a FormAssembly next path using returned tfa_next value.
*
* @param string $tfa_next
* The urlencoded parameter from FormAssembly.
*
* @return string
* HTML returned by the query
*/
public function getNextForm($tfa_next) {
$queryPath = urldecode($tfa_next);
// Make FA rest call and return form markup.
$url = $this
->getUrl('/rest/' . $queryPath);
$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.');
}
return $request
->getBody()
->getContents();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ApiBase:: |
protected | property | Injected config service. | |
ApiBase:: |
protected | property | An array of url objects to the formassembly api endpoints. | |
ApiBase:: |
protected | property | Flag reflecting formassembly.api.oauth.admin_index. | |
ApiBase:: |
protected | property | Default logger channel. | |
ApiBase:: |
constant | The path for admin api requests. | ||
ApiBase:: |
constant | The path for standard api requests. | ||
ApiBase:: |
public | function | Utility method to prepare a getUrl object. | |
ApiMarkup:: |
protected | property | Injected authorization service. | |
ApiMarkup:: |
protected | property | GuzzleHttp\Client definition. | |
ApiMarkup:: |
protected | property | Injected service. | |
ApiMarkup:: |
protected | property | Injected Service. | |
ApiMarkup:: |
protected | property | Injected service. | |
ApiMarkup:: |
protected | property | Injected Service. | |
ApiMarkup:: |
public | function | Retrieve the HTML for a FormAssembly form. | |
ApiMarkup:: |
public | function | Get the HTML for a FormAssembly next path using returned tfa_next value. | |
ApiMarkup:: |
public | function |
ApiSync constructor. Overrides ApiBase:: |