From 0f1845fc48776660481b2859ef38525579017d8e Mon Sep 17 00:00:00 2001
From: Bernd Oliver Suenderhauf <bos@suenderhauf.de>
Date: Sun, 17 Mar 2019 00:40:59 +0100
Subject: [PATCH] Issue #3022485 by Anas_maw, Pancho: Undefined entity label
 impedes identifying FillPdfForm

---
 src/Entity/FillPdfForm.php           |  7 ++-
 src/Form/FillPdfFormForm.php         | 17 +++++--
 tests/src/Functional/AdminIdTest.php | 69 ++++++++++++++++++++++++++--
 3 files changed, 81 insertions(+), 12 deletions(-)

diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php
index 03fb002..d2e962d 100644
--- a/src/Entity/FillPdfForm.php
+++ b/src/Entity/FillPdfForm.php
@@ -2,8 +2,6 @@
 
 namespace Drupal\fillpdf\Entity;
 
-use Drupal\Core\Annotation\Translation;
-use Drupal\Core\Entity\Annotation\ContentEntityType;
 use Drupal\Core\Entity\ContentEntityBase;
 use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Field\BaseFieldDefinition;
@@ -32,6 +30,7 @@ use Drupal\fillpdf\Service\FillPdfAdminFormHelper;
  *   base_table = "fillpdf_forms",
  *   entity_keys = {
  *     "id" = "fid",
+ *     "label" = "admin_title",
  *     "uuid" = "uuid",
  *   },
  *   links = {
@@ -68,8 +67,8 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface {
 
     $overview_url = Url::fromUri('base://admin/structure/fillpdf')->toString();
     $fields['admin_title'] = BaseFieldDefinition::create('string')
-      ->setLabel(t('Administrative description'))
-      ->setDescription(t('Enter the name of the form here, and it will be shown on the <a href="@overview_url">form overview page</a>. It has no effect on functionality, but it can help you identify which form configuration you want to edit.', ['@overview_url' => $overview_url]))
+      ->setLabel(t('Administrative title'))
+      ->setDescription(t('Enter an administrative title to help identifying this FillPDF Form on the <a href="@overview_url">form overview page</a> and in some other places.', ['@overview_url' => $overview_url]))
       ->setDisplayOptions('form', [
         'type' => 'string',
         'weight' => 0,
diff --git a/src/Form/FillPdfFormForm.php b/src/Form/FillPdfFormForm.php
index e44fcad..68ed02b 100644
--- a/src/Form/FillPdfFormForm.php
+++ b/src/Form/FillPdfFormForm.php
@@ -275,6 +275,9 @@ class FillPdfFormForm extends ContentEntityForm {
     /** @var \Drupal\file\FileInterface $file */
     $file = $form_state->getValue('upload_pdf');
 
+    $message = [];
+    $message[] = $this->t('FillPDF Form %link has been updated.', ['%link' => $entity->toLink(NULL, 'edit-form')->toString()]);
+
     if ($file) {
       $existing_fields = $this->entityHelper->getFormFields($entity);
 
@@ -288,20 +291,24 @@ class FillPdfFormForm extends ContentEntityForm {
 
       $form_fields = $added['fields'];
 
+      $message[] = $this->t('Your previous field mappings have been transferred to the new PDF template you uploaded.');
+
       // Import previous form field values over new fields.
-      $non_matching_msg = '';
       $non_matching_fields = $this->serializer->importFormFieldsByKey($existing_fields, $form_fields);
       if (count($non_matching_fields)) {
-        $non_matching_msg = $this->t(" These keys couldn't be found in the new PDF");
+        $message[] = $this->t("These keys couldn't be found in the new PDF:");
       }
 
-      drupal_set_message($this->t("Your previous field mappings have been transferred to the new PDF template you uploaded.") . $non_matching_msg);
+      $this->messenger()->addStatus(implode(' ', $message));
 
       foreach ($non_matching_fields as $non_matching_field) {
-        drupal_set_message($non_matching_field, 'warning');
+        $this->messenger()->addWarning($non_matching_field);
       }
 
-      drupal_set_message($this->t('You might also want to update the <em>Filename pattern</em> field; this has not been changed.'));
+      $this->messenger()->addStatus($this->t('You might also want to update the <em>Filename pattern</em> field; this has not been changed.'));
+    }
+    else {
+      $this->messenger()->addStatus(reset($message));
     }
 
     $entity->set('default_entity_type', $form_state->getValue('default_entity_type'));
diff --git a/tests/src/Functional/AdminIdTest.php b/tests/src/Functional/AdminIdTest.php
index 089b978..df809d3 100644
--- a/tests/src/Functional/AdminIdTest.php
+++ b/tests/src/Functional/AdminIdTest.php
@@ -2,7 +2,11 @@
 
 namespace Drupal\Tests\fillpdf\Functional;
 
+use Drupal\field\Entity\FieldConfig;
+use Drupal\field\Entity\FieldStorageConfig;
 use Drupal\fillpdf\Entity\FillPdfForm;
+use Drupal\simpletest\ContentTypeCreationTrait;
+use Drupal\user\Entity\Role;
 use Drupal\Tests\BrowserTestBase;
 use Drupal\Tests\TestFileCreationTrait;
 use Drupal\Tests\fillpdf\Traits\TestFillPdfTrait;
@@ -14,6 +18,7 @@ use Drupal\Tests\fillpdf\Traits\TestFillPdfTrait;
  */
 class AdminIdTest extends BrowserTestBase {
 
+  use ContentTypeCreationTrait;
   use TestFileCreationTrait;
   use TestFillPdfTrait;
 
@@ -62,15 +67,16 @@ class AdminIdTest extends BrowserTestBase {
   public function testOverviewFormLinks() {
     $this->uploadTestPdf('fillpdf_test_v3.pdf');
 
-    // Set the administrative title.
+    // Set the administrative title and check if it has been successfully set.
     $admin_title = 'Example form';
     $this->drupalPostForm(NULL, ['admin_title[0][value]' => $admin_title], 'Save');
-    $this->assertSession()->statusCodeEquals(200);
+    $this->assertSession()->pageTextContains("FillPDF Form $admin_title has been updated.");
+    $this->assertSession()->fieldValueEquals('edit-admin-title-0-value', $admin_title);
 
     // Go back to the overview page.
     $this->drupalGet('admin/structure/fillpdf');
 
-    // Check if the administrative title has been set and appears in the view.
+    // Check if the administrative title appears in the view.
     $this->assertSession()->pageTextContains($admin_title);
 
     // Check hook_entity_operation_alter(). Only the altered link should exist.
@@ -105,4 +111,61 @@ class AdminIdTest extends BrowserTestBase {
     $this->assertSession()->pageTextContains('Only PDF files are supported, and they must end in .pdf.');
   }
 
+  /**
+   * Tests an entity reference to a FillPdfForm.
+   */
+  public function testEntityReference() {
+    // Create new FillPdfForm.
+    $this->uploadTestPdf('fillpdf_test_v3.pdf');
+    $fid = $this->getLatestFillPdfForm();
+
+    // Set the administrative title.
+    $admin_title = 'Example form';
+    $this->drupalPostForm("admin/structure/fillpdf/{$fid}", ['admin_title[0][value]' => $admin_title], 'Save');
+    $this->assertSession()->statusCodeEquals(200);
+
+    // Create host content type.
+    $bundle = $this->createContentType();
+    $bundle_id = $bundle->id();
+
+    // Create an entity reference to our FillPdfForm.
+    $storage = FieldStorageConfig::create([
+      'field_name' => 'field_fillpdf_form',
+      'entity_type' => 'node',
+      'type' => 'entity_reference',
+      'settings' => [
+        'target_type' => 'fillpdf_form',
+      ],
+    ]);
+    $storage->save();
+    $field = FieldConfig::create([
+      'label' => 'FillPDF form',
+      'field_storage' => $storage,
+      'entity_type' => 'node',
+      'bundle' => $bundle_id,
+      'settings' => [
+        'handler' => 'default:fillpdf_form',
+      ],
+    ]);
+    $field->save();
+    \Drupal::entityTypeManager()
+      ->getStorage('entity_form_display')
+      ->load("node.{$bundle_id}.default")
+      ->setComponent('field_fillpdf_form', [
+        'type' => 'options_select',
+      ])
+      ->save();
+
+    // Grant additional permission to the logged in user.
+    $existing_user_roles = $this->loggedInUser->getRoles(TRUE);
+    $role_to_modify = Role::load(end($existing_user_roles));
+    $this->grantPermissions($role_to_modify, ["create $bundle_id content"]);
+
+    // On a new node, check if the select contains an option with the
+    // administrative title we have set.
+    $this->drupalGet("/node/add/{$bundle_id}");
+    $this->assertSession()->statusCodeEquals(200);
+    $this->assertSession()->optionExists('edit-field-fillpdf-form', $admin_title);
+  }
+
 }
-- 
GitLab