Skip to content

Commit a07ded8

Browse files
committed
fix(drupal): Fix PHP 8.2 serialization issue in PdfConversionForm
The readonly constructor-promoted property $conversionService was not surviving Drupal's AJAX form rebuilds due to PHP 8.2 serialization behavior. Changed to nullable protected property with lazy loading via getConversionService() method. This fixes the "Typed property must not be accessed before initialization" error when uploading PDF files.
1 parent 5d9b775 commit a07ded8

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

  • cloudformation/scenarios/localgov-drupal/drupal/web/modules/custom/ndx_aws_ai/src/Form

cloudformation/scenarios/localgov-drupal/drupal/web/modules/custom/ndx_aws_ai/src/Form/PdfConversionForm.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@ class PdfConversionForm extends FormBase {
2525
*/
2626
private const MAX_UPLOAD_SIZE_MB = 5;
2727

28+
/**
29+
* The PDF conversion service.
30+
*
31+
* Note: Not using readonly constructor promotion to avoid PHP 8.2
32+
* serialization issues during Drupal AJAX form rebuilds.
33+
*
34+
* @var \Drupal\ndx_aws_ai\Service\PdfConversionServiceInterface|null
35+
*/
36+
protected ?PdfConversionServiceInterface $conversionService = NULL;
37+
2838
/**
2939
* Constructs a PdfConversionForm.
3040
*
31-
* @param \Drupal\ndx_aws_ai\Service\PdfConversionServiceInterface $conversionService
41+
* @param \Drupal\ndx_aws_ai\Service\PdfConversionServiceInterface|null $conversionService
3242
* The PDF conversion service.
3343
*/
34-
public function __construct(
35-
private readonly PdfConversionServiceInterface $conversionService,
36-
) {}
44+
public function __construct(?PdfConversionServiceInterface $conversionService = NULL) {
45+
$this->conversionService = $conversionService;
46+
}
3747

3848
/**
3949
* {@inheritdoc}
@@ -44,6 +54,21 @@ public static function create(ContainerInterface $container): static {
4454
);
4555
}
4656

57+
/**
58+
* Gets the PDF conversion service.
59+
*
60+
* Handles re-initialization after form unserialization during AJAX rebuilds.
61+
*
62+
* @return \Drupal\ndx_aws_ai\Service\PdfConversionServiceInterface
63+
* The conversion service.
64+
*/
65+
protected function getConversionService(): PdfConversionServiceInterface {
66+
if ($this->conversionService === NULL) {
67+
$this->conversionService = \Drupal::service('ndx_aws_ai.pdf_conversion');
68+
}
69+
return $this->conversionService;
70+
}
71+
4772
/**
4873
* {@inheritdoc}
4974
*/
@@ -56,7 +81,7 @@ public function getFormId(): string {
5681
*/
5782
public function buildForm(array $form, FormStateInterface $form_state): array {
5883
// Check service availability.
59-
if (!$this->conversionService->isAvailable()) {
84+
if (!$this->getConversionService()->isAvailable()) {
6085
$form['warning'] = [
6186
'#type' => 'container',
6287
'#attributes' => ['class' => ['messages', 'messages--warning']],
@@ -229,7 +254,7 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
229254
$fileId = reset($fileIds);
230255

231256
try {
232-
$jobId = $this->conversionService->startConversion((int) $fileId);
257+
$jobId = $this->getConversionService()->startConversion((int) $fileId);
233258

234259
// Store job ID in session for status checking.
235260
$this->messenger()->addStatus(

0 commit comments

Comments
 (0)