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.
14 class Rearrange extends ViewsFormBase {
17 * Constructs a new Rearrange object.
19 public function __construct($type = NULL) {
20 $this->setType($type);
26 public function getFormKey() {
33 public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL) {
34 $this->setType($type);
35 return parent::getForm($view, $display_id, $js);
41 public function getFormId() {
42 return 'views_ui_rearrange_form';
48 public function buildForm(array $form, FormStateInterface $form_state) {
49 $view = $form_state->get('view');
50 $display_id = $form_state->get('display_id');
51 $type = $form_state->get('type');
53 $types = ViewExecutable::getHandlerTypes();
54 $executable = $view->getExecutable();
55 if (!$executable->setDisplay($display_id)) {
56 $form['markup'] = ['#markup' => $this->t('Invalid display id @display', ['@display' => $display_id])];
59 $display = &$executable->displayHandlers->get($display_id);
60 $form['#title'] = $this->t('Rearrange @type', ['@type' => $types[$type]['ltitle']]);
61 $form['#section'] = $display_id . 'rearrange-item';
63 if ($display->defaultableSections($types[$type]['plural'])) {
64 $section = $types[$type]['plural'];
65 $form_state->set('section', $section);
66 views_ui_standard_display_dropdown($form, $form_state, $section);
71 // Get relationship labels
73 foreach ($display->getHandlers('relationship') as $id => $handler) {
74 $relationships[$id] = $handler->adminLabel();
79 '#header' => ['', $this->t('Weight'), $this->t('Remove')],
80 '#empty' => $this->t('No fields available.'),
84 'relationship' => 'sibling',
89 '#prefix' => '<div class="scroll" data-drupal-views-scroll>',
90 '#suffix' => '</div>',
93 foreach ($display->getOption($types[$type]['plural']) as $id => $field) {
94 $form['fields'][$id] = [];
96 $form['fields'][$id]['#attributes'] = ['class' => ['draggable'], 'id' => 'views-row-' . $id];
98 $handler = $display->getHandler($type, $id);
100 $name = $handler->adminLabel() . ' ' . $handler->adminSummary();
101 if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
102 $name = '(' . $relationships[$field['relationship']] . ') ' . $name;
108 $markup = $this->t('Broken field @id', ['@id' => $id]);
110 $form['fields'][$id]['name'] = ['#markup' => $markup];
112 $form['fields'][$id]['weight'] = [
113 '#type' => 'textfield',
114 '#default_value' => ++$count,
115 '#attributes' => ['class' => ['weight']],
116 '#title' => t('Weight for @title', ['@title' => $name]),
117 '#title_display' => 'invisible',
120 $form['fields'][$id]['removed'] = [
121 '#type' => 'checkbox',
122 '#title' => t('Remove @title', ['@title' => $name]),
123 '#title_display' => 'invisible',
124 '#id' => 'views-removed-' . $id,
125 '#attributes' => ['class' => ['views-remove-checkbox']],
126 '#default_value' => 0,
127 '#suffix' => \Drupal::l(SafeMarkup::format('<span>@text</span>', ['@text' => $this->t('Remove')]),
128 Url::fromRoute('<none>', [], [
130 'id' => 'views-remove-link-' . $id,
131 'class' => ['views-hidden', 'views-button-remove', 'views-remove-link'],
132 'alt' => $this->t('Remove this item'),
133 'title' => $this->t('Remove this item'),
140 $view->getStandardButtons($form, $form_state, 'views_ui_rearrange_form');
148 public function submitForm(array &$form, FormStateInterface $form_state) {
149 $view = $form_state->get('view');
150 $display_id = $form_state->get('display_id');
151 $type = $form_state->get('type');
153 $types = ViewExecutable::getHandlerTypes();
154 $display = &$view->getExecutable()->displayHandlers->get($display_id);
156 $old_fields = $display->getOption($types[$type]['plural']);
157 $new_fields = $order = [];
159 // Make an array with the weights
160 foreach ($form_state->getValue('fields') as $field => $info) {
161 // add each value that is a field with a weight to our list, but only if
162 // it has had its 'removed' checkbox checked.
163 if (is_array($info) && isset($info['weight']) && empty($info['removed'])) {
164 $order[$field] = $info['weight'];
171 // Create a new list of fields in the new order.
172 foreach (array_keys($order) as $field) {
173 $new_fields[$field] = $old_fields[$field];
175 $display->setOption($types[$type]['plural'], $new_fields);