Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / draggableviews / src / Plugin / views / field / DraggableViewsField.php
index 4361e398a18ab8fe95b8f1ee3cbeac2eea85fe24..8f1e6fff0f7fec0384d2d72a8ce40c6dddb17027 100755 (executable)
@@ -1,16 +1,15 @@
 <?php
 
-/**
- * @file
- * Contains \Drupal\draggableviews\Plugin\views\field\DraggableViewsField.
- */
-
 namespace Drupal\draggableviews\Plugin\views\field;
 
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\draggableviews\DraggableViews;
 use Drupal\system\Plugin\views\field\BulkForm;
 use Drupal\Core\Render\Markup;
+use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Language\LanguageManagerInterface;
+use Drupal\Core\Session\AccountInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Defines a draggableviews form element.
@@ -19,19 +18,79 @@ use Drupal\Core\Render\Markup;
  */
 class DraggableViewsField extends BulkForm {
 
+  /**
+   * The entity manager.
+   *
+   * @var \Drupal\Core\Entity\EntityManagerInterface
+   */
+  protected $entityManager;
+
+  /**
+   * The action storage.
+   *
+   * @var \Drupal\Core\Entity\EntityStorageInterface
+   */
+  protected $actionStorage;
+
+  /**
+   * The language manager.
+   *
+   * @var \Drupal\Core\Language\LanguageManagerInterface
+   */
+  protected $languageManager;
+  /**
+   * The Current user.
+   *
+   * @var \Drupal\Core\Session\AccountInterface
+   */
+  protected $currentUser;
+
+  /**
+   * Sets the current_user service.
+   *
+   * @param \Drupal\Core\Session\AccountInterface $current_user
+   *   Current user.
+   *
+   * @return $this
+   */
+  public function setCurrentUser(AccountInterface $current_user) {
+    $this->currentUser = $current_user;
+    return $this;
+  }
+
   /**
    * {@inheritdoc}
    */
-  protected function defineOptions() {
-    $options = parent::defineOptions();
-    return $options;
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    /** @var static $datasource */
+    $bulk_form = parent::create($container, $configuration, $plugin_id, $plugin_definition);
+    $bulk_form->setCurrentUser($container->get('current_user'));
+    return $bulk_form;
   }
 
   /**
    * {@inheritdoc}
    */
   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
+    $form['draggableview_help'] = [
+      '#markup' => $this->t("A draggable element will be added to the first table column. You do not have to set this field as the first column in your View."),
+    ];
     parent::buildOptionsForm($form, $form_state);
+    // Remove all the fields that would break this or are completely ignored
+    // when rendering the drag interface.
+    unset($form['custom_label']);
+    unset($form['label']);
+    unset($form['element_label_colon']);
+    unset($form['action_title']);
+    unset($form['include_exclude']);
+    unset($form['selected_actions']);
+    unset($form['exclude']);
+    unset($form['alter']);
+    unset($form['empty_field_behavior']);
+    unset($form['empty']);
+    unset($form['empty_zero']);
+    unset($form['hide_empty']);
+    unset($form['hide_alter_empty']);
   }
 
   /**
@@ -55,33 +114,33 @@ class DraggableViewsField extends BulkForm {
     $draggableviews = new DraggableViews($this->view);
 
     foreach ($this->view->result as $row_index => $row) {
-      $form[$this->options['id']][$row_index] = array(
+      $form[$this->options['id']][$row_index] = [
         '#tree' => TRUE,
-      );
+      ];
 
       // Item to keep id of the entity.
-      $form[$this->options['id']][$row_index]['id'] = array(
+      $form[$this->options['id']][$row_index]['id'] = [
         '#type' => 'hidden',
-        '#value' => $row->{$this->definition['entity field']},
-        '#attributes' => array('class' => array('draggableviews-id')),
-      );
+        '#value' => $this->getEntity($row)->id(),
+        '#attributes' => ['class' => ['draggableviews-id']],
+      ];
 
       // Add parent.
-      $form[$this->options['id']][$row_index]['parent'] = array(
+      $form[$this->options['id']][$row_index]['parent'] = [
         '#type' => 'hidden',
         '#default_value' => $draggableviews->getParent($row_index),
-        '#attributes' => array('class' => array('draggableviews-parent')),
-      );
+        '#attributes' => ['class' => ['draggableviews-parent']],
+      ];
     }
 
-    if (\Drupal::currentUser()->hasPermission('access draggableviews')) {
+    if ($this->currentUser->hasPermission('access draggableviews')) {
       $options = [
         'table_id' => $draggableviews->getHtmlId(),
         'action' => 'match',
-        'relationship' => 'parent',
+        'relationship' => 'group',
         'group' => 'draggableviews-parent',
         'subgroup' => 'draggableviews-parent',
-        'source' => 'draggableviews-id'
+        'source' => 'draggableviews-id',
       ];
       drupal_attach_tabledrag($form, $options);
     }