Skip to content
Snippets Groups Projects
Commit 9294eb31 authored by Ide Braakman's avatar Ide Braakman Committed by Jonathan Sacksick
Browse files

Issue #3260367 by idebr, jsacksick: Implement file usage to allow unused files...

Issue #3260367 by idebr, jsacksick: Implement file usage to allow unused files to be automatically deleted
parent a97bda76
No related branches found
No related tags found
No related merge requests found
......@@ -78,3 +78,52 @@ function commerce_invoice_update_8202() {
$definition_manager = \Drupal::entityDefinitionUpdateManager();
$definition_manager->installFieldStorageDefinition('invoice_file', 'commerce_invoice', 'commerce_invoice', $storage_definition);
}
/**
* Convert the invoice_file from entity_reference to file field.
*/
function commerce_invoice_update_8203() {
$entity_type_manager = \Drupal::entityTypeManager();
$bundle_of = 'commerce_invoice';
$storage = $entity_type_manager->getStorage($bundle_of);
$bundle_definition = $entity_type_manager->getDefinition($bundle_of);
// Sometimes the primary key isn't 'id'. e.g. 'eid' or 'item_id'.
$id_key = $bundle_definition->getKey('id');
// If there is no data table defined then use the base table.
$table_name = $storage->getDataTable() ?: $storage->getBaseTable();
$database = \Drupal::database();
$definition_manager = \Drupal::entityDefinitionUpdateManager();
// Store the existing values.
$file_values = $database->select($table_name)
->fields($table_name, [$id_key, 'invoice_file'])
->execute()
->fetchAllKeyed();
// Clear out the values.
$database->update($table_name)
->fields(['invoice_file' => NULL])
->execute();
// Uninstall the field.
$field_storage_definition = $definition_manager->getFieldStorageDefinition('invoice_file', $bundle_of);
$definition_manager->uninstallFieldStorageDefinition($field_storage_definition);
// Create a new field definition.
$new_invoice_file = BaseFieldDefinition::create('file')
->setLabel(t('Invoice PDF'))
->setDescription(t('The invoice PDF file.'))
->setDisplayConfigurable('view', TRUE);
// Install the new definition.
$definition_manager->installFieldStorageDefinition('invoice_file', $bundle_of, $bundle_of, $new_invoice_file);
// Restore the values.
foreach ($file_values as $id => $value) {
$database->update($table_name)
->fields(['invoice_file__target_id' => $value])
->condition($id_key, $id)
->execute();
}
}
......@@ -857,10 +857,9 @@ class Invoice extends CommerceContentEntityBase implements InvoiceInterface {
])
->setDisplayConfigurable('view', TRUE);
$fields['invoice_file'] = BaseFieldDefinition::create('entity_reference')
$fields['invoice_file'] = BaseFieldDefinition::create('file')
->setLabel(t('Invoice PDF'))
->setDescription(t('The invoice PDF file.'))
->setSetting('target_type', 'file')
->setDisplayConfigurable('view', TRUE);
return $fields;
......
......@@ -164,7 +164,7 @@ class InvoiceType extends CommerceBundleEntityBase implements InvoiceTypeInterfa
*/
public function getLogoUrl() {
if ($image = $this->getLogoFile()) {
return file_create_url($image->getFileUri());
return $image->createFileUrl(FALSE);
}
return NULL;
......
......@@ -118,7 +118,7 @@ class InvoiceTypeForm extends CommerceBundleEntityFormBase {
$form['emails']['sendConfirmation'] = [
'#type' => 'checkbox',
'#title' => $this->t('Email the customer a confirmation when an invoice is "confirmed" or paid'),
'#default_value' => ($invoice_type->isNew()) ? TRUE : $invoice_type->shouldSendConfirmation(),
'#default_value' => $invoice_type->isNew() || $invoice_type->shouldSendConfirmation(),
];
$form['emails']['confirmationBcc'] = [
'#type' => 'textfield',
......
......@@ -37,6 +37,13 @@ class InvoiceFileManagerTest extends InvoiceKernelTestBase {
*/
protected $user;
/**
* The file usage service.
*
* @var \Drupal\file\FileUsage\FileUsageInterface
*/
protected $fileUsage;
/**
* {@inheritdoc}
*/
......@@ -61,6 +68,7 @@ class InvoiceFileManagerTest extends InvoiceKernelTestBase {
$profile->save();
$this->profile = $this->reloadEntity($profile);
$this->fileManager = $this->container->get('commerce_invoice.invoice_file_manager');
$this->fileUsage = $this->container->get('file.usage');
}
/**
......@@ -95,29 +103,34 @@ class InvoiceFileManagerTest extends InvoiceKernelTestBase {
'uid' => $this->user->id(),
]);
$invoice->save();
/** @var \Drupal\commerce_invoice\Entity\InvoiceInterface $invoice */
$invoice = $this->reloadEntity($invoice);
$file = $this->fileManager->getInvoiceFile($invoice);
$this->assertNotEmpty($file);
$this->assertRegExp('#private://(.*)\.pdf#', $file->getFileUri());
$this->assertEquals('10-en-draft.pdf', $file->getFilename());
$this->assertEquals('application/pdf', $file->getMimeType());
$file_draft = $this->fileManager->getInvoiceFile($invoice);
$this->assertSame(['file' => ['commerce_invoice' => [$invoice->id() => $invoice->id()]]], $this->fileUsage->listUsage($file_draft));
$this->assertNotEmpty($file_draft);
$this->assertRegExp('#private://(.*)\.pdf#', $file_draft->getFileUri());
$this->assertEquals('10-en-draft.pdf', $file_draft->getFilename());
$this->assertEquals('application/pdf', $file_draft->getMimeType());
/** @var \Drupal\commerce_invoice\Entity\InvoiceInterface $invoice */
$invoice = $this->reloadEntity($invoice);
$invoice_file = $invoice->getFile();
$this->assertNotEmpty($invoice_file);
$this->assertEquals($file->id(), $invoice_file->id());
$this->assertEquals($file_draft->id(), $invoice_file->id());
// Assert that updating the invoice state clears the invoice file
// reference.
$invoice->getState()->applyTransitionById('confirm');
$invoice->save();
$file = $this->fileManager->getInvoiceFile($invoice);
$this->assertNotEmpty($file);
$this->assertRegExp('#private://(.*)\.pdf#', $file->getFileUri());
$this->assertEquals('10-en-pending.pdf', $file->getFilename());
$this->assertEquals('application/pdf', $file->getMimeType());
$this->assertEquals($file->id(), $invoice->getFile()->id());
$this->assertSame([], $this->fileUsage->listUsage($file_draft));
$file_pending = $this->fileManager->getInvoiceFile($invoice);
$this->assertSame(['file' => ['commerce_invoice' => [$invoice->id() => $invoice->id()]]], $this->fileUsage->listUsage($file_pending));
$this->assertNotEmpty($file_pending);
$this->assertRegExp('#private://(.*)\.pdf#', $file_pending->getFileUri());
$this->assertEquals('10-en-pending.pdf', $file_pending->getFilename());
$this->assertEquals('application/pdf', $file_pending->getMimeType());
$this->assertEquals($file_pending->id(), $invoice->getFile()->id());
}
}
......@@ -31,6 +31,7 @@ abstract class InvoiceKernelTestBase extends OrderKernelTestBase {
protected function setUp(): void {
parent::setUp();
$this->installSchema('file', ['file_usage']);
$this->installEntitySchema('commerce_invoice_item');
$this->installEntitySchema('commerce_invoice');
$this->installEntitySchema('file');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment