diff --git a/draggableviews.module b/draggableviews.module index e2a08e1..51149cc 100644 --- a/draggableviews.module +++ b/draggableviews.module @@ -17,7 +17,7 @@ function draggableviews_views_data_alter(&$data) { 'id' => 'numeric', ), 'sort' => array( - 'id' => 'standard', + 'id' => 'draggableviews_sort', ), 'filter' => array( 'help' => t('Filter by the draggableviews weight value (Native handler only).'), diff --git a/src/Plugin/views/sort/DraggableViewsSort.php b/src/Plugin/views/sort/DraggableViewsSort.php new file mode 100644 index 0000000..0f49ad2 --- /dev/null +++ b/src/Plugin/views/sort/DraggableViewsSort.php @@ -0,0 +1,71 @@ +view->id()); + $options = parent::defineOptions(); + $options['view_name'] = ['default' => $this->view->id()]; + $options['view_display'] = ['default' => '']; + return $options; + } + + /** + * Shortcut to display the value form. + */ + protected function showSortForm(&$form, FormStateInterface $form_state) { + parent::showSortForm($form, $form_state); + + $views = Views::getEnabledViews(); + $draggableviews = []; + foreach ($views as $view) { + $config = \Drupal::config('views.view.' . $view->id()); + $rawData = $config->getRawData(); + //dpm($rawData); + foreach ($rawData['display'] as $display_key => $display) { + $draggableviews[$this->view->id()] = $this->t('This view'); + if (array_key_exists('draggableviews', $display['display_options']['fields'])) { + $draggableviews[$view->id()] = $view->label(); + } + } + } + + $form['view_name'] = array( + '#type' => 'select', + '#title' => $this->t('View name'), + '#options' => $draggableviews, + '#default_value' => $this->options['view_name'], + ); + + $form['view_display'] = array( + '#type' => 'textfield', + '#title' => $this->t('View display'), + '#default_value' => $this->options['view_display'], + '#description' => $this->t('If want to use the draggableviews information a specific display of the selected view, type it here. Otherwise the whole view will be used.'), + ); + } + + public function query() { + parent::query(); + $table = &$this->query->getTableInfo($this->tableAlias); + if ($table) { + $table['join']->extra = [['field' => 'view_name', 'value' => $this->options['view_name']]]; + if (!empty($this->options['view_display'])) { + $table['join']->extra[] = ['field' => 'view_display', 'value' => $this->options['view_display']]; + } + } + } +}