diff --git a/fillpdf.services.yml b/fillpdf.services.yml index 4cd99a3d8f72edb226586890de69c56eb9469414..42f83b1d53ed4b71e40bf4942d5582aa73496787 100644 --- a/fillpdf.services.yml +++ b/fillpdf.services.yml @@ -47,7 +47,7 @@ services: fillpdf.input_helper: class: Drupal\fillpdf\InputHelper - arguments: ["@config.factory", "@plugin.manager.fillpdf.pdf_backend"] + arguments: ["@config.factory", "@plugin.manager.fillpdf.pdf_backend", "@entity_type.manager"] fillpdf.shell_manager: class: Drupal\fillpdf\ShellManager @@ -55,4 +55,4 @@ services: fillpdf.backend_proxy: class: Drupal\fillpdf\Service\BackendProxy - arguments: ['@fillpdf.token_resolver', '@plugin.manager.fillpdf.pdf_backend', '@config.factory'] + arguments: ['@fillpdf.token_resolver', '@plugin.manager.fillpdf.pdf_backend', '@config.factory', '@entity_type.manager'] diff --git a/src/Controller/HandlePdfController.php b/src/Controller/HandlePdfController.php index 1aabbb155895c6e5788179ed1cc1ad6e2b58e0ef..f6956d8b798fa7c7e286de0c4525f7929c57b85a 100644 --- a/src/Controller/HandlePdfController.php +++ b/src/Controller/HandlePdfController.php @@ -2,10 +2,12 @@ namespace Drupal\fillpdf\Controller; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Link; use Drupal\Core\Url; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\StreamWrapper\StreamWrapperInterface; +use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface; use Drupal\fillpdf\FillPdfContextManagerInterface; use Drupal\fillpdf\FillPdfFormInterface; use Drupal\fillpdf\FillPdfLinkManipulatorInterface; @@ -72,6 +74,20 @@ class HandlePdfController extends ControllerBase { */ protected $backendProxy; + /** + * Provides a StreamWrapper manager. + * + * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface + */ + protected $streamWrapperManager; + + /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + /** * Constructs a FillPdfBackendManager object. * @@ -89,6 +105,10 @@ class HandlePdfController extends ControllerBase { * The FillPDF action manager. * @param \Drupal\fillpdf\Service\BackendProxyInterface $backend_proxy * The backend proxy. + * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager + * Provides a StreamWrapper manager. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The configuration factory. */ public function __construct( FillPdfLinkManipulatorInterface $link_manipulator, @@ -97,7 +117,9 @@ class HandlePdfController extends ControllerBase { RequestStack $request_stack, PdfBackendManager $backend_manager, FillPdfActionPluginManager $action_manager, - BackendProxyInterface $backend_proxy + BackendProxyInterface $backend_proxy, + StreamWrapperManagerInterface $stream_wrapper_manager, + ConfigFactoryInterface $config_factory ) { $this->linkManipulator = $link_manipulator; $this->contextManager = $context_manager; @@ -106,6 +128,8 @@ class HandlePdfController extends ControllerBase { $this->backendManager = $backend_manager; $this->actionManager = $action_manager; $this->backendProxy = $backend_proxy; + $this->streamWrapperManager = $stream_wrapper_manager; + $this->configFactory = $config_factory; } /** @@ -119,7 +143,9 @@ class HandlePdfController extends ControllerBase { $container->get('request_stack'), $container->get('plugin.manager.fillpdf.pdf_backend'), $container->get('plugin.manager.fillpdf_action.processor'), - $container->get('fillpdf.backend_proxy') + $container->get('fillpdf.backend_proxy'), + $container->get('stream_wrapper_manager'), + $container->get('config.factory') ); } @@ -209,8 +235,8 @@ class HandlePdfController extends ControllerBase { // Determine the appropriate action for the PDF. $scheme = $fillpdf_form->getStorageScheme(); - $is_available = array_key_exists($scheme, \Drupal::service('stream_wrapper_manager')->getWrappers(StreamWrapperInterface::WRITE_VISIBLE)); - $is_allowed = in_array($scheme, \Drupal::config('fillpdf.settings')->get('allowed_schemes') ?: []); + $is_available = array_key_exists($scheme, $this->streamWrapperManager->getWrappers(StreamWrapperInterface::WRITE_VISIBLE)); + $is_allowed = in_array($scheme, $this->configFactory->get('fillpdf.settings')->get('allowed_schemes') ?: []); if (empty($scheme)) { $action_plugin_id = 'download'; diff --git a/src/Form/FillPdfFormForm.php b/src/Form/FillPdfFormForm.php index 267e11455db2ff3c833055afddbb114335c68897..48bd90ceb8219918f414af8372c00cef69a1aa93 100644 --- a/src/Form/FillPdfFormForm.php +++ b/src/Form/FillPdfFormForm.php @@ -10,8 +10,8 @@ use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; +use Drupal\Core\Render\RendererInterface; use Drupal\Core\Url; -use Drupal\file\Entity\File; use Drupal\fillpdf\Component\Utility\FillPdf; use Drupal\fillpdf\FillPdfAdminFormHelperInterface; use Drupal\fillpdf\FillPdfLinkManipulatorInterface; @@ -72,6 +72,13 @@ class FillPdfFormForm extends ContentEntityForm { */ protected $tokenResolver; + /** + * The Renderer service. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + /** * {@inheritdoc} */ @@ -85,6 +92,7 @@ class FillPdfFormForm extends ContentEntityForm { EntityTypeManager $entity_type_manager, TokenResolverInterface $token_resolver, EntityTypeBundleInfoInterface $entity_type_bundle_info, + RendererInterface $renderer, TimeInterface $time ) { parent::__construct( @@ -98,6 +106,7 @@ class FillPdfFormForm extends ContentEntityForm { $this->serializer = $fillpdf_serializer; $this->fileSystem = $file_system; $this->entityTypeManager = $entity_type_manager; + $this->renderer = $renderer; $this->tokenResolver = $token_resolver; } @@ -115,6 +124,7 @@ class FillPdfFormForm extends ContentEntityForm { $container->get('entity_type.manager'), $container->get('fillpdf.token_resolver'), $container->get('entity_type.bundle.info'), + $container->get('renderer'), $container->get('datetime.time') ); } @@ -235,7 +245,7 @@ class FillPdfFormForm extends ContentEntityForm { $fid = $fillpdf_form->id(); /** @var \Drupal\file\FileInterface $file_entity */ - $file_entity = File::load($fillpdf_form->get('file')->first()->target_id); + $file_entity = $this->entityTypeManager->getStorage('file')->load($fillpdf_form->get('file')->first()->target_id); $pdf_info_weight = 0; $form['pdf_info'] = [ '#type' => 'fieldset', @@ -510,7 +520,7 @@ class FillPdfFormForm extends ContentEntityForm { $existing_fields = $entity->getFormFields(); /** @var \Drupal\file\FileInterface $new_file */ - $new_file = File::load($form_state->getValue('upload_pdf')['0']); + $new_file = $this->entityTypeManager->getStorage('file')->load($form_state->getValue('upload_pdf')['0']); $new_file->setPermanent(); $new_file->save(); @@ -545,7 +555,7 @@ class FillPdfFormForm extends ContentEntityForm { '#items' => array_keys($non_matching_fields), ], ]; - $this->messenger()->addWarning(\Drupal::service('renderer')->render($message)); + $this->messenger()->addWarning($this->renderer->render($message)); } $this->messenger()->addStatus($this->t('You might also want to update the <em>Filename pattern</em> field; this has not been changed.')); diff --git a/src/Form/FillPdfOverviewForm.php b/src/Form/FillPdfOverviewForm.php index 688ec8a5759dc86345abdd9331dad9620460f2d9..f2ca507ad1bfbf6ccd7744868bc45b548c24b07d 100644 --- a/src/Form/FillPdfOverviewForm.php +++ b/src/Form/FillPdfOverviewForm.php @@ -8,7 +8,7 @@ use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; -use Drupal\file\Entity\File; +use Drupal\Core\Entity\EntityTypeManager; use Drupal\fillpdf\InputHelperInterface; use Drupal\fillpdf\Component\Utility\FillPdf; use Drupal\fillpdf\Plugin\PdfBackendManager; @@ -48,6 +48,13 @@ class FillPdfOverviewForm extends FormBase { */ protected $inputHelper; + /** + * The Entity Type Manager. + * + * @var \Drupal\Core\Entity\EntityTypeManager + */ + protected $entityTypeManager; + /** * Constructs a FillPdfSettingsForm object. * @@ -59,12 +66,21 @@ class FillPdfOverviewForm extends FormBase { * The file system. * @param \Drupal\fillpdf\InputHelperInterface $input_helper * The FillPDF input helper. + * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager + * The Entity type manager service. */ - public function __construct(ModuleHandlerInterface $module_handler, PdfBackendManager $backend_manager, FileSystemInterface $file_system, InputHelperInterface $input_helper) { + public function __construct( + ModuleHandlerInterface $module_handler, + PdfBackendManager $backend_manager, + FileSystemInterface $file_system, + InputHelperInterface $input_helper, + EntityTypeManager $entity_type_manager + ) { $this->moduleHandler = $module_handler; $this->backendManager = $backend_manager; $this->fileSystem = $file_system; $this->inputHelper = $input_helper; + $this->entityTypeManager = $entity_type_manager; } /** @@ -75,7 +91,8 @@ class FillPdfOverviewForm extends FormBase { $container->get('module_handler'), $container->get('plugin.manager.fillpdf.pdf_backend'), $container->get('file_system'), - $container->get('fillpdf.input_helper') + $container->get('fillpdf.input_helper'), + $container->get('entity_type.manager') ); } @@ -156,7 +173,7 @@ class FillPdfOverviewForm extends FormBase { public function submitForm(array &$form, FormStateInterface $form_state) { if ($form_state->getValue('upload_pdf')) { /** @var \Drupal\file\FileInterface $file */ - $file = File::load($form_state->getValue('upload_pdf')['0']); + $file = $this->entityTypeManager->getStorage('file')->load($form_state->getValue('upload_pdf')['0']); $added = $this->inputHelper->attachPdfToForm($file); /** @var \Drupal\fillpdf\Entity\FillPdfForm $fillpdf_form */ diff --git a/src/Form/FillPdfSettingsForm.php b/src/Form/FillPdfSettingsForm.php index 69f67032e2996a87e3bce4486ef641bb85a3827b..04d543b645df99d4947e1bc8b09544dd1438db09 100644 --- a/src/Form/FillPdfSettingsForm.php +++ b/src/Form/FillPdfSettingsForm.php @@ -15,6 +15,7 @@ use Drupal\fillpdf\ShellManager; use GuzzleHttp\Client; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Link; +use Drupal\Core\Render\RendererInterface; use Drupal\fillpdf\Entity\FillPdfForm; /** @@ -59,6 +60,13 @@ class FillPdfSettingsForm extends ConfigFormBase { */ protected $shellManager; + /** + * The Renderer service. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + /** * Constructs a FillPdfSettingsForm object. * @@ -74,6 +82,8 @@ class FillPdfSettingsForm extends ConfigFormBase { * The FillPDF shell manager. * @param \Drupal\fillpdf\Plugin\PdfBackendManager $backend_manager * The FillPDF backend manager. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The Renderer service. */ public function __construct( ConfigFactoryInterface $config_factory, @@ -81,7 +91,8 @@ class FillPdfSettingsForm extends ConfigFormBase { FillPdfAdminFormHelper $admin_form_helper, Client $http_client, ShellManager $shell_manager, - PdfBackendManager $backend_manager + PdfBackendManager $backend_manager, + RendererInterface $renderer ) { parent::__construct($config_factory); @@ -90,6 +101,7 @@ class FillPdfSettingsForm extends ConfigFormBase { $this->httpClient = $http_client; $this->shellManager = $shell_manager; $this->definitions = $backend_manager->getDefinitions(); + $this->renderer = $renderer; } /** @@ -102,7 +114,8 @@ class FillPdfSettingsForm extends ConfigFormBase { $container->get('fillpdf.admin_form_helper'), $container->get('http_client'), $container->get('fillpdf.shell_manager'), - $container->get('plugin.manager.fillpdf.pdf_backend') + $container->get('plugin.manager.fillpdf.pdf_backend'), + $container->get('renderer') ); } @@ -184,7 +197,7 @@ class FillPdfSettingsForm extends ConfigFormBase { '#items' => $items, ], ]; - $this->messenger()->addError(\Drupal::service('renderer')->renderPlain($error_message)); + $this->messenger()->addError($this->renderer->renderPlain($error_message)); } $this->logger('fillpdf')->critical('File storage %previous_scheme is no longer available.' . $count ? " $count FillPDF forms are defunct." : '', [ diff --git a/src/InputHelper.php b/src/InputHelper.php index f0ab39b18a19f4c1f2549805829b440e96b7ce85..7918654dd96b2d7bbe4517e48106abff19b17929 100644 --- a/src/InputHelper.php +++ b/src/InputHelper.php @@ -4,7 +4,7 @@ namespace Drupal\fillpdf; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\file\FileInterface; -use Drupal\file\Entity\File; +use Drupal\Core\Entity\EntityTypeManager; use Drupal\fillpdf\Entity\FillPdfForm; use Drupal\fillpdf\Entity\FillPdfFormField; use Drupal\fillpdf\Plugin\PdfBackendManager; @@ -30,6 +30,13 @@ class InputHelper implements InputHelperInterface { */ protected $backendManager; + /** + * The Entity Type Manager. + * + * @var \Drupal\Core\Entity\EntityTypeManager + */ + protected $entityTypeManager; + /** * Constructs an InputHelper object. * @@ -37,10 +44,13 @@ class InputHelper implements InputHelperInterface { * The config factory service. * @param \Drupal\fillpdf\Plugin\PdfBackendManager $backend_manager * The FillPDF backend manager. + * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager + * The Entity type manager service. */ - public function __construct(ConfigFactoryInterface $config_factory, PdfBackendManager $backend_manager) { + public function __construct(ConfigFactoryInterface $config_factory, PdfBackendManager $backend_manager, EntityTypeManager $entity_type_manager) { $this->configManager = $config_factory; $this->backendManager = $backend_manager; + $this->entityTypeManager = $entity_type_manager; } /** @@ -89,7 +99,7 @@ class InputHelper implements InputHelperInterface { $backend = $this->backendManager->createInstance($config->get('backend'), $config->get()); // Attempt to parse the fields in the PDF. - $parsed_fields = $backend->parseFile(File::load($fillpdf_form->file->target_id)); + $parsed_fields = $backend->parseFile($this->entityTypeManager->getStorage('file')->load($fillpdf_form->file->target_id)); $unique_fields = []; foreach ((array) $parsed_fields as $field) { diff --git a/src/Service/BackendProxy.php b/src/Service/BackendProxy.php index ac850e6c0c9e9d830f49686b7a8e615ad7d5410d..d924b998256b7b8c6bf81b6d61007f2815beffe4 100644 --- a/src/Service/BackendProxy.php +++ b/src/Service/BackendProxy.php @@ -3,7 +3,7 @@ namespace Drupal\fillpdf\Service; use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\file\Entity\File; +use Drupal\Core\Entity\EntityTypeManager; use Drupal\fillpdf\Component\Helper\FillPdfMappingHelper; use Drupal\fillpdf\FieldMapping\TextFieldMapping; use Drupal\fillpdf\FillPdfFormInterface; @@ -35,6 +35,13 @@ class BackendProxy implements BackendProxyInterface { */ protected $configFactory; + /** + * The Entity Type Manager. + * + * @var \Drupal\Core\Entity\EntityTypeManager + */ + protected $entityTypeManager; + /** * Constructs a BackendProxy object. * @@ -44,11 +51,14 @@ class BackendProxy implements BackendProxyInterface { * The plugin.manager.fillpdf.pdf_backend service. * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory * The configuration factory. + * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager + * The Entity type manager service. */ - public function __construct(TokenResolverInterface $tokenResolver, PdfBackendManager $backendManager, ConfigFactoryInterface $configFactory) { + public function __construct(TokenResolverInterface $tokenResolver, PdfBackendManager $backendManager, ConfigFactoryInterface $configFactory, EntityTypeManager $entity_type_manager) { $this->tokenResolver = $tokenResolver; $this->backendManager = $backendManager; $this->configFactory = $configFactory; + $this->entityTypeManager = $entity_type_manager; } /** @@ -100,7 +110,7 @@ class BackendProxy implements BackendProxyInterface { // @todo Emit event (or call alter hook?) before populating PDF. // Rename fillpdf_merge_fields_alter() to fillpdf_populate_fields_alter(). /** @var \Drupal\file\FileInterface $templateFile */ - $templateFile = File::load($fillPdfForm->file->target_id); + $templateFile = $this->entityTypeManager->getStorage('file')->load($fillPdfForm->file->target_id); $mergedPdf = $backend->mergeFile($templateFile, $fieldMappings, $mergeOptions);