Skip to content
Snippets Groups Projects
Commit af38bcaf authored by Bernd Oliver Suenderhauf's avatar Bernd Oliver Suenderhauf
Browse files

Issue #3059342 by Pancho: Add plugin inspection to backend plugins

parent 5c30f29e
No related branches found
No related tags found
No related merge requests found
......@@ -2,10 +2,10 @@
namespace Drupal\fillpdf\Plugin\FillPdfBackend;
use Drupal\Core\Plugin\PluginBase;
use Drupal\file\Entity\File;
use Drupal\fillpdf\FillPdfBackendPluginInterface;
use Drupal\fillpdf\FillPdfFormInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* @Plugin(
......@@ -20,34 +20,7 @@ use Drupal\Core\StringTranslation\StringTranslationTrait;
* weight = -10
* )
*/
class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface {
use StringTranslationTrait;
/**
* The FillPDF service endpoint.
*
* @var string
*/
protected $fillPdfServiceEndpoint;
/**
* The plugin's configuration.
*
* @var array
*/
protected $config;
/**
* Constructs a FillPdfServiceFillPdfBackend plugin object.
*
* @param array $config
* A configuration array containing information about the plugin instance.
*/
public function __construct(array $config) {
$this->config = $config;
$this->fillPdfServiceEndpoint = "{$this->config['remote_protocol']}://{$this->config['remote_endpoint']}";
}
class FillPdfServiceFillPdfBackend extends PluginBase implements FillPdfBackendPluginInterface {
/**
* {@inheritdoc}
......@@ -77,7 +50,7 @@ class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface {
* @return \stdClass
*/
protected function xmlRpcRequest($method /* $args */) {
$url = $this->fillPdfServiceEndpoint;
$url = $this->configuration['remote_protocol'] . '://' . $this->configuration['remote_endpoint'];
$args = func_get_args();
// Fix up the array for Drupal 7 xmlrpc() function style.
......@@ -87,13 +60,13 @@ class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface {
$ret = new \stdClass();
if (isset($result['error'])) {
\Drupal::messenger()->addError($result['error']);
$this->messenger()->addError($result['error']);
$ret->error = TRUE;
}
elseif ($result == FALSE || xmlrpc_error()) {
$error = xmlrpc_error();
$ret->error = TRUE;
\Drupal::messenger()->addError($this->t('There was a problem contacting the FillPDF service.
$this->messenger()->addError($this->t('There was a problem contacting the FillPDF service.
It may be down, or you may not have internet access. [ERROR @code: @message]',
['@code' => $error->code, '@message' => $error->message]));
}
......@@ -111,7 +84,7 @@ class FillPdfServiceFillPdfBackend implements FillPdfBackendPluginInterface {
/** @var \Drupal\file\FileInterface $original_file */
$original_file = File::load($fillpdf_form->file->target_id);
$original_pdf = file_get_contents($original_file->getFileUri());
$api_key = $this->config['fillpdf_service_api_key'];
$api_key = $this->configuration['fillpdf_service_api_key'];
// Anonymize image data from the fields array; we should not send the real
// filename to FillPDF Service. We do this in the specific backend because
......
......@@ -4,6 +4,7 @@ namespace Drupal\fillpdf\Plugin\FillPdfBackend;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\file\Entity\File;
use Drupal\fillpdf\FillPdfBackendPluginInterface;
use Drupal\fillpdf\FillPdfFormInterface;
......@@ -17,14 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* weight = 10
* )
*/
class LocalFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFactoryPluginInterface {
/**
* The plugin's configuration.
*
* @var array
*/
protected $configuration;
class LocalFillPdfBackend extends PluginBase implements FillPdfBackendPluginInterface, ContainerFactoryPluginInterface {
/**
* The file system.
......@@ -36,18 +30,18 @@ class LocalFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
/**
* Constructs a LocalFillPdfBackend plugin object.
*
* @param \Drupal\Core\File\FileSystem $file_system
* The file system.
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\File\FileSystem $file_system
* The file system.
*/
public function __construct(FileSystem $file_system, array $configuration, $plugin_id, $plugin_definition) {
public function __construct(array $configuration, $plugin_id, array $plugin_definition, FileSystem $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->fileSystem = $file_system;
$this->configuration = $configuration;
}
/**
......@@ -55,10 +49,10 @@ class LocalFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$container->get('file_system'),
$configuration,
$plugin_id,
$plugin_definition
$plugin_definition,
$container->get('file_system')
);
}
......@@ -77,7 +71,7 @@ class LocalFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
$fields = java_values($fillpdf->parse());
}
catch (\JavaException $e) {
\Drupal::messenger()->addError(java_truncate((string) $e));
$this->messenger()->addError(java_truncate((string) $e));
}
return $fields;
......@@ -109,7 +103,7 @@ class LocalFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
}
}
catch (\JavaException $e) {
\Drupal::messenger()->addError(java_truncate((string) $e));
$this->messenger()->addError(java_truncate((string) $e));
return NULL;
}
try {
......@@ -121,7 +115,7 @@ class LocalFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
}
}
catch (\JavaException $e) {
\Drupal::messenger()->addError(java_truncate((string) $e));
$this->messenger()->addError(java_truncate((string) $e));
return NULL;
}
......
......@@ -4,13 +4,13 @@ namespace Drupal\fillpdf\Plugin\FillPdfBackend;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\file\Entity\File;
use Drupal\fillpdf\FieldMapping\ImageFieldMapping;
use Drupal\fillpdf\FieldMapping\TextFieldMapping;
use Drupal\fillpdf\FillPdfBackendPluginInterface;
use Drupal\fillpdf\FillPdfFormInterface;
use Drupal\fillpdf\Plugin\BackendServiceManager;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -21,21 +21,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* weight = 5
* )
*/
class LocalService implements FillPdfBackendPluginInterface, ContainerFactoryPluginInterface {
/**
* The plugin's configuration.
*
* @var array
*/
protected $configuration;
/**
* The plugin ID.
*
* @var string
*/
protected $pluginId;
class LocalService extends PluginBase implements FillPdfBackendPluginInterface, ContainerFactoryPluginInterface {
/**
* The file system.
......@@ -51,35 +37,24 @@ class LocalService implements FillPdfBackendPluginInterface, ContainerFactoryPlu
*/
protected $backendServiceManager;
/**
* The Guzzle http client.
*
* @var \GuzzleHttp\Client
*/
private $httpClient;
/**
* Constructs a LocalService plugin object.
*
* @param \Drupal\Core\File\FileSystem $file_system
* The file system.
* @param \GuzzleHttp\Client $http_client
* The Guzzle http client.
* @param \Drupal\fillpdf\Plugin\BackendServiceManager $backend_service_manager
* The backend service manager.
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\fillpdf\Plugin\BackendServiceManager $backend_service_manager
* The backend service manager.
* @param \Drupal\Core\File\FileSystem $file_system
* The file system.
*/
public function __construct(FileSystem $file_system, Client $http_client, BackendServiceManager $backend_service_manager, array $configuration, $plugin_id, $plugin_definition) {
$this->fileSystem = $file_system;
$this->httpClient = $http_client;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, BackendServiceManager $backend_service_manager, FileSystem $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->backendServiceManager = $backend_service_manager;
$this->configuration = $configuration;
$this->pluginId = $plugin_id;
$this->fileSystem = $file_system;
}
/**
......@@ -87,12 +62,11 @@ class LocalService implements FillPdfBackendPluginInterface, ContainerFactoryPlu
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$container->get('file_system'),
$container->get('http_client'),
$container->get('plugin.manager.fillpdf_backend_service'),
$configuration,
$plugin_id,
$plugin_definition
$plugin_definition,
$container->get('plugin.manager.fillpdf_backend_service'),
$container->get('file_system')
);
}
......
......@@ -4,7 +4,7 @@ namespace Drupal\fillpdf\Plugin\FillPdfBackend;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Plugin\PluginBase;
use Drupal\file\Entity\File;
use Drupal\fillpdf\Component\Utility\FillPdf;
use Drupal\fillpdf\FillPdfAdminFormHelperInterface;
......@@ -26,16 +26,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* weight = -5
* )
*/
class PdftkFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;
/**
* The plugin's configuration.
*
* @var array
*/
protected $configuration;
class PdftkFillPdfBackend extends PluginBase implements FillPdfBackendPluginInterface, ContainerFactoryPluginInterface {
/**
* The file system.
......@@ -61,24 +52,24 @@ class PdftkFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
/**
* Constructs a PdftkFillPdfBackend plugin object.
*
* @param \Drupal\Core\File\FileSystem $file_system
* The file system.
* @param \Drupal\fillpdf\ShellManager $shell_manager
* The FillPDF shell manager.
* @param \Drupal\fillpdf\FillPdfAdminFormHelperInterface $admin_form_helper
* The FillPDF admin form helper.
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\File\FileSystem $file_system
* The file system.
* @param \Drupal\fillpdf\ShellManager $shell_manager
* The FillPDF shell manager.
* @param \Drupal\fillpdf\FillPdfAdminFormHelperInterface $admin_form_helper
* The FillPDF admin form helper.
*/
public function __construct(FileSystem $file_system, ShellManager $shell_manager, FillPdfAdminFormHelperInterface $admin_form_helper, array $configuration, $plugin_id, $plugin_definition) {
public function __construct(array $configuration, $plugin_id, array $plugin_definition, FileSystem $file_system, ShellManager $shell_manager, FillPdfAdminFormHelperInterface $admin_form_helper) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->fileSystem = $file_system;
$this->shellManager = $shell_manager;
$this->adminFormHelper = $admin_form_helper;
$this->configuration = $configuration;
}
/**
......@@ -86,12 +77,12 @@ class PdftkFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$container->get('file_system'),
$container->get('fillpdf.shell_manager'),
$container->get('fillpdf.admin_form_helper'),
$configuration,
$plugin_id,
$plugin_definition
$plugin_definition,
$container->get('file_system'),
$container->get('fillpdf.shell_manager'),
$container->get('fillpdf.admin_form_helper')
);
}
......@@ -106,7 +97,7 @@ class PdftkFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
$pdftk_path = $this->getPdftkPath();
$status = FillPdf::checkPdftkPath($pdftk_path);
if ($status === FALSE) {
\Drupal::messenger()->addError($this->t('pdftk not properly installed.'));
$this->messenger()->addError($this->t('pdftk not properly installed.'));
return [];
}
......@@ -119,7 +110,7 @@ class PdftkFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
$output = [];
exec("{$pdftk_path} {$template_path} dump_data_fields_utf8", $output, $status);
if (count($output) === 0) {
\Drupal::messenger()->addWarning($this->t('PDF does not contain fillable fields.'));
$this->messenger()->addWarning($this->t('PDF does not contain fillable fields.'));
return [];
}
......@@ -186,7 +177,7 @@ class PdftkFillPdfBackend implements FillPdfBackendPluginInterface, ContainerFac
passthru("{$pdftk_path} {$template_path} fill_form {$xfdf_path} output - " . ($context['flatten'] ? 'flatten ' : '') . 'drop_xfa');
$data = ob_get_clean();
if ($data === FALSE) {
\Drupal::messenger()->addError($this->t('pdftk not properly installed. No PDF generated.'));
$this->messenger()->addError($this->t('pdftk not properly installed. No PDF generated.'));
}
$xfdf_file->delete();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment