From 7ff6ff3302118a52e8cb01bdc60f4e9250ab74aa Mon Sep 17 00:00:00 2001
From: Kevin Kaland <kevin@wizonesolutions.com>
Date: Mon, 13 Apr 2015 15:30:47 +0200
Subject: [PATCH] Flesh out FillPdfForm entity forms more.

---
 fillpdf.routing.yml                | 20 ++++++++++++++++++++
 src/Component/Utility/FillPdf.php  | 26 +++++++++++++++++++++++++-
 src/Entity/FillPdfForm.php         |  9 ++++-----
 src/Form/FillPdfFormDeleteForm.php | 21 +++++++++++++++++++--
 src/Form/FillPdfFormForm.php       | 11 +++++++++--
 5 files changed, 77 insertions(+), 10 deletions(-)

diff --git a/fillpdf.routing.yml b/fillpdf.routing.yml
index 7ab0a85..ec34d9b 100644
--- a/fillpdf.routing.yml
+++ b/fillpdf.routing.yml
@@ -34,3 +34,23 @@ entity.fillpdf_form.edit_form:
     _permission: 'administer pdfs' # todo: do we have an administer own pdfs perm?
   options:
     _admin_route: TRUE
+
+entity.fillpdf_form.delete_form:
+  path: '/admin/structure/fillpdf/{fillpdf_form}/delete'
+  defaults:
+    _entity_form: fillpdf_form.delete
+    _title: 'Delete FillPDF form'
+  requirements:
+    _permission: 'administer pdfs' # todo: do we have an administer own pdfs perm?
+  options:
+    _admin_route: TRUE
+
+entity.fillpdf_form_field.edit_form:
+  path: '/admin/structure/fillpdf/{fillpdf_form}/{fillpdf_form_field}'
+  defaults:
+    _entity_form: fillpdf_form_field.edit
+    _title: 'Edit FillPDF form field'
+  requirements:
+    _permission: 'administer pdfs' # todo: do we have an administer own pdfs perm?
+  options:
+    _admin_route: TRUE
diff --git a/src/Component/Utility/FillPdf.php b/src/Component/Utility/FillPdf.php
index bf7d588..1e7fd3c 100644
--- a/src/Component/Utility/FillPdf.php
+++ b/src/Component/Utility/FillPdf.php
@@ -4,6 +4,7 @@
  * Contains \Drupal\fillpdf\Component\Utility\Fillpdf.
  */
 namespace Drupal\fillpdf\Component\Utility;
+use Drupal\views\Views;
 use \Symfony\Component\Process\Process;
 
 class FillPdf {
@@ -21,4 +22,27 @@ class FillPdf {
     }
     return TRUE;
   }
-}
\ No newline at end of file
+
+  /**
+   * Correctly embed a View with arguments. views_embed_view() does not
+   * zero-index.
+   *
+   * @param $view_name
+   * @param string $display_id
+   */
+  public static function embedView($name, $display_id = 'default') {
+    $args = func_get_args();
+    // Remove $name and $display_id from the arguments.
+    unset($args[0], $args[1]);
+
+    $args = array_values($args);
+
+    $view = Views::getView($name);
+    if (!$view || !$view->access($display_id)) {
+      return;
+    }
+
+    return $view->preview($display_id, $args);
+  }
+
+}
diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php
index 3cd5fb7..00a6f24 100644
--- a/src/Entity/FillPdfForm.php
+++ b/src/Entity/FillPdfForm.php
@@ -29,7 +29,8 @@ use Drupal\fillpdf\FillPdfFormInterface;
  *   data_table = "fillpdf_forms_field_data",
  *   entity_keys = {
  *     "id" = "fid",
- *     "uuid" = "uuid"
+ *     "label" = "title",
+ *     "uuid" = "uuid",
  *   },
  * )
  */
@@ -57,8 +58,6 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface {
       ->setDescription(t('The associated managed file.'))
       ->setSetting('target_type', 'file');
 
-//    /** @var UrlGeneratorInterface $url_generator */
-//    $url_generator = \Drupal::service('url_generator');
     // @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();
     // @todo: what is wrong with the below?
@@ -100,7 +99,7 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface {
       Note that, in both cases, you are responsible for ensuring that the user under which PHP is running can write to this path. Do not include a trailing slash.</p>"))
       ->setDisplayOptions('form', array(
         'type' => 'string',
-        'weight' => 3,
+        'weight' => 2,
       ));
 
     $fields['destination_redirect'] = BaseFieldDefinition::create('boolean')
@@ -108,7 +107,7 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface {
       ->setDescription(t("<strong>This setting is applicable only if <em>Where to save generated PDFs</em> is set.</strong> Instead of redirecting your visitors to the front page, it will redirect them directly to the PDF. However, if you pass Drupal's <em>destination</em> query string parameter, that will override this setting."))
       ->setDisplayOptions('form', array(
         'type' => 'boolean_checkbox',
-        'weight' => 4,
+        'weight' => 3,
         'settings' => array(
           'display_label' => TRUE,
         ),
diff --git a/src/Form/FillPdfFormDeleteForm.php b/src/Form/FillPdfFormDeleteForm.php
index 71b362f..7aaf688 100644
--- a/src/Form/FillPdfFormDeleteForm.php
+++ b/src/Form/FillPdfFormDeleteForm.php
@@ -7,21 +7,38 @@
 namespace Drupal\fillpdf\Form;
 
 use Drupal\Core\Entity\ContentEntityConfirmFormBase;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\Core\Url;
 
 class FillPdfFormDeleteForm extends ContentEntityConfirmFormBase {
 
+  use StringTranslationTrait;
+
   /**
    * {@inheritdoc}
    */
   public function getQuestion() {
-    // TODO: Implement getQuestion() method.
+    return $this->t('Are you sure you want to delete %name?', array('%name' => $this->entity->label()));
   }
 
   /**
    * {@inheritdoc}
    */
   public function getCancelUrl() {
-    // TODO: Implement getCancelUrl() method.
+    return new Url('fillpdf.forms_admin');
+  }
+
+  public function getConfirmText() {
+    return $this->t('Delete');
+  }
+
+  public function submitForm(array &$form, FormStateInterface $form_state) {
+    $this->getEntity()->delete();
+
+    drupal_set_message($this->t('FillPDF form deleted.'));
+
+    $form_state->setRedirect('fillpdf.forms_admin');
   }
 
 }
diff --git a/src/Form/FillPdfFormForm.php b/src/Form/FillPdfFormForm.php
index cb9e6e4..2578176 100644
--- a/src/Form/FillPdfFormForm.php
+++ b/src/Form/FillPdfFormForm.php
@@ -7,17 +7,24 @@ namespace Drupal\fillpdf\Form;
 
 use Drupal\Core\Entity\ContentEntityForm;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\fillpdf\Component\Utility\FillPdf;
 
 class FillPdfFormForm extends ContentEntityForm {
   /**
    * {@inheritdoc}
    */
-  public function buildForm(array $form, FormStateInterface $form_state) {
-    $form = parent::buildForm($form, $form_state);
+  public function form(array $form, FormStateInterface $form_state) {
+    $form = parent::form($form, $form_state);
 
     /** @var FillPdfForm $entity */
     $entity = $this->entity;
 
+    $form['fillpdf_fields'] = FillPdf::embedView('fillpdf_form_fields',
+      'block_1',
+      $entity->id());
+
+    $form['fillpdf_fields']['#weight'] = 100;
+
     return $form;
   }
 
-- 
GitLab