From e85853ee9b9a18d4f7c4565cf61999fac826d07e Mon Sep 17 00:00:00 2001
From: Kevin Kaland <kevin@wizonesolutions.com>
Date: Wed, 4 Nov 2015 12:32:33 +0100
Subject: [PATCH] Issue #2359213: Migrate entity_reference fields to file
 fields.

This gets us file.usage tracking for free.
---
 fillpdf.install                   | 72 +++++++++++++++----------------
 src/Entity/FillPdfFileContext.php |  6 +--
 src/Entity/FillPdfForm.php        |  5 +--
 3 files changed, 38 insertions(+), 45 deletions(-)

diff --git a/fillpdf.install b/fillpdf.install
index 538bb22..f80e6e2 100644
--- a/fillpdf.install
+++ b/fillpdf.install
@@ -1,4 +1,5 @@
 <?php
+use Drupal\Core\Entity\ContentEntityInterface;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\fillpdf\Service\FillPdfAdminFormHelper;
 
@@ -29,68 +30,63 @@ function fillpdf_update_8102() {
 
 /**
  * Use file fields instead of entity_reference fields for referring to files.
- *
- * Never actually ran this, but leaving it in to refer to in the future.
  */
 function fillpdf_update_8103() {
   $edum = \Drupal::entityDefinitionUpdateManager();
   $em = \Drupal::entityManager();
+  $db = \Drupal::database();
 
-  $form_file = BaseFieldDefinition::create('file')
+  $form_file_def = BaseFieldDefinition::create('file')
     ->setLabel(t('The associated managed file.'))
     ->setDescription(t('The associated managed file.'))
     ->setName('file')
     ->setProvider('fillpdf_form')
-    ->setTargetBundle(NULL);
+    ->setTargetBundle(NULL)
+    ->setTargetEntityTypeId('fillpdf_form');
 
-  $fc_file = BaseFieldDefinition::create('file')
+  $fc_file_def = BaseFieldDefinition::create('file')
     ->setLabel(t('The associated managed file.'))
     ->setDescription(t('The associated managed file.'))
     ->setName('file')
     ->setProvider('fillpdf_file_context')
-    ->setTargetBundle(NULL);
+    ->setTargetBundle(NULL)
+    ->setTargetEntityTypeId('fillpdf_file_context');
 
-  // Save existing fillpdf_form data.
-  $form_files = [];
-  $forms = $em->getStorage('fillpdf_form')->loadMultiple();
-  foreach ($forms as $form) {
-    $form_files[$form->id()] = $form->file;
-    $form->file = NULL;
-    $form->save();
-  }
+  // Save existing data.
+  $form_files = $db->select('fillpdf_forms', 'ff')
+    ->fields('ff', ['fid', 'file'])
+    ->execute()
+    ->fetchAllKeyed();
 
-  // Save existing fillpdf_file_context data.
-  $fc_files = [];
-  $fcs = $em->getStorage('fillpdf_file_context')->loadMultiple();
-  foreach ($fcs as $fc) {
-    $fc_files[$fc->id()] = $fc->file;
-    $fc->file = NULL;
-    $fc->save();
-  }
+  $fc_files = $db->select('fillpdf_file_context', 'fc')
+    ->fields('fc', ['id', 'file'])
+    ->execute()
+    ->fetchAllKeyed();
+
+  // Remove data from the storage.
+  $db->update('fillpdf_forms')
+    ->fields(['file' => NULL])
+    ->execute();
+
+  $db->update('fillpdf_file_context')
+    ->fields(['file' => NULL])
+    ->execute();
 
   // Now install the new field definitions.
-  $edum->updateFieldStorageDefinition($form_file);
-  $edum->updateFieldStorageDefinition($fc_file);
+  $edum->updateFieldStorageDefinition($form_file_def);
+  $edum->updateFieldStorageDefinition($fc_file_def);
 
-  foreach ($form_files as $entity_id => $form_file) {
+  foreach ($form_files as $entity_id => $fillpdf_form_file) {
+    /** @var ContentEntityInterface $entity */
     $entity = $em->getStorage('fillpdf_form')->load($entity_id);
-    $entity->file = $form_file;
+    $entity->file->target_id = $fillpdf_form_file;
     $entity->save();
-
-    // These files don't have proper file usage information because they were
-    // created as entity_reference fields. Manually run postSave as if they
-    // were new entities.
-    $entity->postSave($em->getStorage('fillpdf_form'), FALSE);
   }
 
-  foreach ($fc_files as $entity_id => $fc_file) {
+  foreach ($fc_files as $entity_id => $ffcf) {
+    /** @var ContentEntityInterface $entity */
     $entity = $em->getStorage('fillpdf_file_context')->load($entity_id);
-    $entity->file = $fc_file;
+    $entity->file->target_id = $ffcf;
     $entity->save();
-
-    // These files don't have proper file usage information because they were
-    // created as entity_reference fields. Manually run postSave as if they
-    // were new entities.
-    $entity->postSave($em->getStorage('fillpdf_file_context'), FALSE);
   }
 }
diff --git a/src/Entity/FillPdfFileContext.php b/src/Entity/FillPdfFileContext.php
index 28606ff..31427e2 100644
--- a/src/Entity/FillPdfFileContext.php
+++ b/src/Entity/FillPdfFileContext.php
@@ -27,7 +27,6 @@ use Drupal\user\UserInterface;
  *   label = @Translation("FillPDF file context"),
  *   handlers = {
  *     "views_data" = "Drupal\fillpdf\Entity\FillPdfFileContextViewsData",
- *
  *     "access" = "Drupal\fillpdf\FillPdfFileContextAccessControlHandler",
  *   },
  *   base_table = "fillpdf_file_context",
@@ -63,10 +62,9 @@ class FillPdfFileContext extends ContentEntityBase implements FillPdfFileContext
       ->setDescription(t('The UUID of the FillPDF file context entity.'))
       ->setReadOnly(TRUE);
 
-    $fields['file'] = BaseFieldDefinition::create('entity_reference')
+    $fields['file'] = BaseFieldDefinition::create('file')
       ->setLabel(t('The associated managed file.'))
-      ->setDescription(t('The associated managed file.'))
-      ->setSetting('target_type', 'file');
+      ->setDescription(t('The associated managed file.'));
 
     $fields['context'] = BaseFieldDefinition::create('string_long')
       ->setLabel(t('Generation context'))
diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php
index 709fe94..4812586 100644
--- a/src/Entity/FillPdfForm.php
+++ b/src/Entity/FillPdfForm.php
@@ -58,10 +58,9 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface {
       ->setDescription(t('The UUID of the FillPdfForm entity.'))
       ->setReadOnly(TRUE);
 
-    $fields['file'] = BaseFieldDefinition::create('entity_reference')
+    $fields['file'] = BaseFieldDefinition::create('file')
       ->setLabel(t('The associated managed file.'))
-      ->setDescription(t('The associated managed file.'))
-      ->setSetting('target_type', 'file');
+      ->setDescription(t('The associated managed file.'));
 
     // @todo: Figure out how to do this the right way...I get a router rebuild error if I use $url_generator->generateFromRoute()
     $overview_url = Url::fromUri('base://admin/structure/fillpdf')->toString();
-- 
GitLab