3 namespace Drupal\views_ui\Form\Ajax;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\views\ViewEntityInterface;
9 use Drupal\views\ViewExecutable;
12 * Provides a rearrange form for Views handlers.
16 class Rearrange extends ViewsFormBase {
19 * Constructs a new Rearrange object.
21 public function __construct($type = NULL) {
22 $this->setType($type);
28 public function getFormKey() {
35 public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL) {
36 $this->setType($type);
37 return parent::getForm($view, $display_id, $js);
43 public function getFormId() {
44 return 'views_ui_rearrange_form';
50 public function buildForm(array $form, FormStateInterface $form_state) {
51 $view = $form_state->get('view');
52 $display_id = $form_state->get('display_id');
53 $type = $form_state->get('type');
55 $types = ViewExecutable::getHandlerTypes();
56 $executable = $view->getExecutable();
57 if (!$executable->setDisplay($display_id)) {
58 $form['markup'] = ['#markup' => $this->t('Invalid display id @display', ['@display' => $display_id])];
61 $display = &$executable->displayHandlers->get($display_id);
62 $form['#title'] = $this->t('Rearrange @type', ['@type' => $types[$type]['ltitle']]);
63 $form['#section'] = $display_id . 'rearrange-item';
65 if ($display->defaultableSections($types[$type]['plural'])) {
66 $section = $types[$type]['plural'];
67 $form_state->set('section', $section);
68 views_ui_standard_display_dropdown($form, $form_state, $section);
73 // Get relationship labels
75 foreach ($display->getHandlers('relationship') as $id => $handler) {
76 $relationships[$id] = $handler->adminLabel();
81 '#header' => ['', $this->t('Weight'), $this->t('Remove')],
82 '#empty' => $this->t('No fields available.'),
86 'relationship' => 'sibling',
91 '#prefix' => '<div class="scroll" data-drupal-views-scroll>',
92 '#suffix' => '</div>',
95 foreach ($display->getOption($types[$type]['plural']) as $id => $field) {
96 $form['fields'][$id] = [];
98 $form['fields'][$id]['#attributes'] = ['class' => ['draggable'], 'id' => 'views-row-' . $id];
100 $handler = $display->getHandler($type, $id);
102 $name = $handler->adminLabel() . ' ' . $handler->adminSummary();
103 if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
104 $name = '(' . $relationships[$field['relationship']] . ') ' . $name;
110 $markup = $this->t('Broken field @id', ['@id' => $id]);
112 $form['fields'][$id]['name'] = ['#markup' => $markup];
114 $form['fields'][$id]['weight'] = [
115 '#type' => 'textfield',
116 '#default_value' => ++$count,
117 '#attributes' => ['class' => ['weight']],
118 '#title' => t('Weight for @title', ['@title' => $name]),
119 '#title_display' => 'invisible',
122 $form['fields'][$id]['removed'] = [
123 '#type' => 'checkbox',
124 '#title' => t('Remove @title', ['@title' => $name]),
125 '#title_display' => 'invisible',
126 '#id' => 'views-removed-' . $id,
127 '#attributes' => ['class' => ['views-remove-checkbox']],
128 '#default_value' => 0,
129 '#suffix' => \Drupal::l(SafeMarkup::format('<span>@text</span>', ['@text' => $this->t('Remove')]),
130 Url::fromRoute('<none>', [], [
132 'id' => 'views-remove-link-' . $id,
133 'class' => ['views-hidden', 'views-button-remove', 'views-remove-link'],
134 'alt' => $this->t('Remove this item'),
135 'title' => $this->t('Remove this item'),
142 $view->getStandardButtons($form, $form_state, 'views_ui_rearrange_form');
150 public function submitForm(array &$form, FormStateInterface $form_state) {
151 $view = $form_state->get('view');
152 $display_id = $form_state->get('display_id');
153 $type = $form_state->get('type');
155 $types = ViewExecutable::getHandlerTypes();
156 $display = &$view->getExecutable()->displayHandlers->get($display_id);
158 $old_fields = $display->getOption($types[$type]['plural']);
159 $new_fields = $order = [];
161 // Make an array with the weights
162 foreach ($form_state->getValue('fields') as $field => $info) {
163 // add each value that is a field with a weight to our list, but only if
164 // it has had its 'removed' checkbox checked.
165 if (is_array($info) && isset($info['weight']) && empty($info['removed'])) {
166 $order[$field] = $info['weight'];
173 // Create a new list of fields in the new order.
174 foreach (array_keys($order) as $field) {
175 $new_fields[$field] = $old_fields[$field];
177 $display->setOption($types[$type]['plural'], $new_fields);