From d71a9193eda0a2936fdce1a4707df4904b196e14 Mon Sep 17 00:00:00 2001
From: Kent Parker <kent@passingphase.co.nz>
Date: Sat, 15 Jul 2017 11:27:17 +1200
Subject: [PATCH] add options details and run codesniffer

---
 CHANGELOG.md                                  | 10 ++++++-
 README.md                                     |  2 +-
 .../ViewsReferenceOptionsFieldFormatter.php   |  6 ++---
 .../ViewsReferenceOptionsWidget.php           |  5 ++--
 .../Field/FieldType/ViewsReferenceItem.php    |  3 +--
 .../ViewsReferenceSelectWidget.php            |  9 +++++++
 .../Field/FieldWidget/ViewsReferenceTrait.php | 26 +++++++++++++++++--
 .../FieldWidget/ViewsReferenceWidget.php      |  5 +++-
 viewsreference.install                        | 14 ++++++----
 viewsreference.module                         |  8 +++---
 10 files changed, 65 insertions(+), 23 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 685a568..6c0c96e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -37,4 +37,12 @@ Add multiple arguments: https://www.drupal.org/node/2846625
 Fix placeholder error: https://www.drupal.org/node/2838413
 Remove redundant code
 Remove extra settings: https://www.drupal.org/node/2851184
-Add preexecute: https://www.drupal.org/node/2851058
\ No newline at end of file
+Add preexecute: https://www.drupal.org/node/2851058
+
+8.x-1.0-beta2
+More fixes to arguments: https://www.drupal.org/node/2888158
+Clean up install code: https://www.drupal.org/node/2862022
+
+8.x-1.0-rc1
+Apply Details form type to title and argument options to prepare for advanced options module
+Run codesniffer on code
diff --git a/README.md b/README.md
index c1d519a..49c49ff 100644
--- a/README.md
+++ b/README.md
@@ -35,4 +35,4 @@ MAINTAINERS
 Current maintainers:
 
  * Kent Shelley (New Zeal) - https://www.drupal.org/u/new-zeal
- * Joe Kersey (joekers) - https://www.drupal.org/u/joekers
\ No newline at end of file
+ * Joe Kersey (joekers) - https://www.drupal.org/u/joekers
diff --git a/modules/viewsreference_options/src/Field/FieldFormatter/ViewsReferenceOptionsFieldFormatter.php b/modules/viewsreference_options/src/Field/FieldFormatter/ViewsReferenceOptionsFieldFormatter.php
index ef03fac..ecbb181 100644
--- a/modules/viewsreference_options/src/Field/FieldFormatter/ViewsReferenceOptionsFieldFormatter.php
+++ b/modules/viewsreference_options/src/Field/FieldFormatter/ViewsReferenceOptionsFieldFormatter.php
@@ -4,7 +4,6 @@ namespace Drupal\viewsreference_options\Plugin\Field\FieldFormatter;
 
 use Drupal\views\Views;
 use Drupal\Core\Field\FieldItemListInterface;
-use Drupal\Core\Field\FormatterBase;
 use Drupal\Core\Form\FormStateInterface;
 
 /**
@@ -34,8 +33,7 @@ class ViewsReferenceOptionsFieldFormatter extends ViewsReferenceFieldFormatter {
   public function settingsForm(array $form, FormStateInterface $form_state) {
     $form = parent::settingsForm($form, $form_state);
 
-    // Add new settings here
-
+    // Add new settings here.
     return $form;
   }
 
@@ -64,7 +62,7 @@ class ViewsReferenceOptionsFieldFormatter extends ViewsReferenceFieldFormatter {
     $elements = [];
 
     // We cannot use the parent function here so we need to replicate the build that occurs in viewsreference
-    // This could be avoided by having an independent function that processes the initial build
+    // This could be avoided by having an independent function that processes the initial build.
     foreach ($items as $delta => $item) {
       $view_name = $item->getValue()['target_id'];
       $display_id = $item->getValue()['display_id'];
diff --git a/modules/viewsreference_options/src/Field/FieldWidget/ViewsReferenceOptionsWidget.php b/modules/viewsreference_options/src/Field/FieldWidget/ViewsReferenceOptionsWidget.php
index a1cd808..70c5393 100644
--- a/modules/viewsreference_options/src/Field/FieldWidget/ViewsReferenceOptionsWidget.php
+++ b/modules/viewsreference_options/src/Field/FieldWidget/ViewsReferenceOptionsWidget.php
@@ -5,9 +5,8 @@ namespace Drupal\viewsreference_options\Plugin\Field\FieldWidget;
 use Drupal\Core\Field\FieldItemListInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Symfony\Component\Validator\ConstraintViolationInterface;
-use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
-//use Drupal\viewsreference\Plugin\Field\FieldWidget\ViewsReferenceTrait;
 
+// Use Drupal\viewsreference\Plugin\Field\FieldWidget\ViewsReferenceTrait;.
 /**
  * Plugin implementation of the 'entity_reference_autocomplete' widget.
  *
@@ -54,7 +53,7 @@ class ViewsReferenceOptionsWidget extends ViewsReferenceWidget {
     $test = array('filled' => TRUE);
     $element = $this->fieldElement($element, $items, $delta, $test);
 
-    // Add extra options here
+    // Add extra options here.
     return $element;
   }
 
diff --git a/src/Plugin/Field/FieldType/ViewsReferenceItem.php b/src/Plugin/Field/FieldType/ViewsReferenceItem.php
index 7b8bee7..fff6abe 100644
--- a/src/Plugin/Field/FieldType/ViewsReferenceItem.php
+++ b/src/Plugin/Field/FieldType/ViewsReferenceItem.php
@@ -90,7 +90,6 @@ class ViewsReferenceItem extends EntityReferenceItem implements
     $schema = parent::schema($field_definition);
     $target_type = $field_definition->getSetting('target_type');
     $target_type_info = \Drupal::entityTypeManager()->getDefinition($target_type);
-    $properties = static::propertyDefinitions($field_definition)['target_id'];
     $schema['columns']['display_id'] = array(
       'description' => 'The ID of the display.',
       'type' => 'varchar_ascii',
@@ -114,7 +113,7 @@ class ViewsReferenceItem extends EntityReferenceItem implements
     $schema['columns']['data'] = array(
       'description' => 'Serialized data.',
       'type' => 'text',
-      'size' => 'big'
+      'size' => 'big',
     );
 
     $schema['indexes']['display_id'] = array('display_id');
diff --git a/src/Plugin/Field/FieldWidget/ViewsReferenceSelectWidget.php b/src/Plugin/Field/FieldWidget/ViewsReferenceSelectWidget.php
index 25d930f..df08491 100644
--- a/src/Plugin/Field/FieldWidget/ViewsReferenceSelectWidget.php
+++ b/src/Plugin/Field/FieldWidget/ViewsReferenceSelectWidget.php
@@ -47,4 +47,13 @@ class ViewsReferenceSelectWidget extends OptionsSelectWidget {
     return $select_element;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
+    // Options creates an array which we need to flatten.
+    $values = $this->massageValues($values, $form, $form_state);
+    return $values;
+  }
+
 }
diff --git a/src/Plugin/Field/FieldWidget/ViewsReferenceTrait.php b/src/Plugin/Field/FieldWidget/ViewsReferenceTrait.php
index 451c65f..b18490b 100644
--- a/src/Plugin/Field/FieldWidget/ViewsReferenceTrait.php
+++ b/src/Plugin/Field/FieldWidget/ViewsReferenceTrait.php
@@ -77,7 +77,14 @@ trait ViewsReferenceTrait {
       ),
     );
 
-    $element['title'] = array(
+    $element['options'] = array(
+      '#type' => 'details',
+      '#title' => t('Options'),
+      '#weight' => 10,
+    );
+
+    // Title and argument are the original options included in this module.
+    $element['options']['title'] = array(
       '#title' => 'Include View Title',
       '#type' => 'checkbox',
       '#default_value' => isset($items[$delta]->getValue()['title']) ? $items[$delta]->getValue()['title'] : '',
@@ -89,7 +96,7 @@ trait ViewsReferenceTrait {
       ),
     );
 
-    $element['argument'] = array(
+    $element['options']['argument'] = array(
       '#title' => 'Argument',
       '#type' => 'textfield',
       '#default_value' => isset($items[$delta]->getValue()['argument']) ? $items[$delta]->getValue()['argument'] : '',
@@ -264,4 +271,19 @@ trait ViewsReferenceTrait {
     return $views_list;
   }
 
+  /**
+   * Helper function to flatten options array.
+   */
+  public function massageValues(array $values, array $form, FormStateInterface $form_state) {
+    foreach ($values as $key => $value) {
+      if (is_array($value['options'])) {
+        foreach ($value['options'] as $ind => $option) {
+          $values[$key][$ind] = $option;
+        }
+        unset($value['options']);
+      }
+    }
+    return $values;
+  }
+
 }
diff --git a/src/Plugin/Field/FieldWidget/ViewsReferenceWidget.php b/src/Plugin/Field/FieldWidget/ViewsReferenceWidget.php
index 9576035..ed8b02f 100644
--- a/src/Plugin/Field/FieldWidget/ViewsReferenceWidget.php
+++ b/src/Plugin/Field/FieldWidget/ViewsReferenceWidget.php
@@ -67,7 +67,10 @@ class ViewsReferenceWidget extends EntityReferenceAutocompleteWidget {
    * {@inheritdoc}
    */
   public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
-    return parent::massageFormValues($values, $form, $form_state);
+    $values = parent::massageFormValues($values, $form, $form_state);
+    // Options creates an array which we need to flatten.
+    $values = $this->massageValues($values, $form, $form_state);
+    return $values;
   }
 
 }
diff --git a/viewsreference.install b/viewsreference.install
index 69a59d0..e4e5673 100644
--- a/viewsreference.install
+++ b/viewsreference.install
@@ -5,6 +5,9 @@
  * Viewsreference install functions.
  */
 
+use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
+
+
 /**
  * Update node fields already created with argument field.
  */
@@ -19,22 +22,23 @@ function viewsreference_update_8101() {
   viewsreference_update_viewsreference_fields('title');
 }
 
-
 /**
  * Update node fields already created with data field.
  */
 function viewsreference_update_8102() {
   viewsreference_update_viewsreference_fields('data');
 }
+
 /**
  * Update all instances using viewsreference field type with new field. This is
  * called from hook_update_N() when new fields are added to the schema.
  *
- * @param string $new_field_name The name of the new field to be added.
+ * @param string $new_field_name
+ *   The name of the new field to be added.
  */
 function viewsreference_update_viewsreference_fields($new_field_name) {
 
-  // Caches have to be cleared first to ensure new fields are detected in the code
+  // Caches have to be cleared first to ensure new fields are detected in the code.
   drupal_flush_all_caches();
 
   // Retrieve list of all viewsreference fields mapped by entity type.
@@ -46,7 +50,7 @@ function viewsreference_update_viewsreference_fields($new_field_name) {
     foreach (array_keys($fields) as $field_name) {
       $field_storage_definition = $manager->getFieldStorageDefinition($field_name, $entity_type_id);
       $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
-      if ($storage instanceof \Drupal\Core\Entity\Sql\SqlContentEntityStorage) {
+      if ($storage instanceof SqlContentEntityStorage) {
         $table_mapping = $storage->getTableMapping([
           $field_name => $field_storage_definition,
         ]);
@@ -67,4 +71,4 @@ function viewsreference_update_viewsreference_fields($new_field_name) {
       $manager->updateFieldStorageDefinition($field_storage_definition);
     }
   }
-}
\ No newline at end of file
+}
diff --git a/viewsreference.module b/viewsreference.module
index cd057ce..9c4553d 100644
--- a/viewsreference.module
+++ b/viewsreference.module
@@ -1,6 +1,6 @@
 <?php
 /**
- * @file module functions for viewsreference
+ * @file Module functions for viewsreference.
  */
 
 /**
@@ -11,15 +11,15 @@ function viewsreference_theme($existing, $type, $theme, $path) {
     'viewsreference__view_title' => array(
       'template' => 'viewsreference--view-title',
       'render element' => 'variables',
-    )
+    ),
   );
 }
 
 /**
  * Implements HOOK_preprocess_HOOK().
- * 
+ *
  * @param $variables
  */
 function viewsreference_preprocess_viewsreference__view_title(&$variables) {
   $variables['title'] = $variables['variables']['#title'];
-}
\ No newline at end of file
+}
-- 
GitLab