View source
<?php
namespace Drupal\draggableviews\Plugin\views\field;
use Drupal\Core\Render\Markup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\draggableviews\DraggableViews;
use Drupal\views\Plugin\views\field\BulkForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DraggableViewsField extends BulkForm {
protected $entityManager;
protected $actionStorage;
protected $languageManager;
protected $currentUser;
public function setCurrentUser(AccountInterface $current_user) {
$this->currentUser = $current_user;
return $this;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$bulk_form = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$bulk_form
->setCurrentUser($container
->get('current_user'));
return $bulk_form;
}
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);
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']);
}
public function render_item($count, $item) {
return Markup::create('<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->');
}
public function viewsForm(&$form, FormStateInterface $form_state) {
$form[$this->options['id']] = [
'#tree' => TRUE,
];
$draggableviews = new DraggableViews($this->view);
foreach ($this->view->result as $row_index => $row) {
$form[$this->options['id']][$row_index] = [
'#tree' => TRUE,
];
$form[$this->options['id']][$row_index]['weight'] = [
'#type' => 'textfield',
'#size' => '5',
'#maxlength' => '5',
'#value' => $row->draggableviews_structure_weight,
'#attributes' => [
'class' => [
'draggableviews-weight',
],
],
];
$form[$this->options['id']][$row_index]['id'] = [
'#type' => 'hidden',
'#value' => $this
->getEntity($row)
->id(),
'#attributes' => [
'class' => [
'draggableviews-id',
],
],
];
$form[$this->options['id']][$row_index]['parent'] = [
'#type' => 'hidden',
'#default_value' => $draggableviews
->getParent($row_index),
'#attributes' => [
'class' => [
'draggableviews-parent',
],
],
];
}
if ($this->currentUser
->hasPermission('access draggableviews')) {
$options = [
'table_id' => $draggableviews
->getHtmlId(),
'action' => 'match',
'relationship' => 'group',
'group' => 'draggableviews-parent',
'subgroup' => 'draggableviews-parent',
'source' => 'draggableviews-id',
];
drupal_attach_tabledrag($form, $options);
}
}
}