"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.9/core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php" (18 Nov 2020, 5606 Bytes) of package /linux/www/drupal-8.9.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ReorderDisplays.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\views_ui\Form\Ajax;
    4 
    5 use Drupal\Component\Render\FormattableMarkup;
    6 use Drupal\Core\Form\FormStateInterface;
    7 use Drupal\Core\Url;
    8 
    9 /**
   10  * Displays the display reorder form.
   11  *
   12  * @internal
   13  */
   14 class ReorderDisplays extends ViewsFormBase {
   15 
   16   /**
   17    * {@inheritdoc}
   18    */
   19   public function getFormKey() {
   20     return 'reorder-displays';
   21   }
   22 
   23   /**
   24    * {@inheritdoc}
   25    */
   26   public function getFormId() {
   27     return 'views_ui_reorder_displays_form';
   28   }
   29 
   30   /**
   31    * {@inheritdoc}
   32    */
   33   public function buildForm(array $form, FormStateInterface $form_state) {
   34     /** @var $view \Drupal\views\ViewEntityInterface */
   35     $view = $form_state->get('view');
   36     $display_id = $form_state->get('display_id');
   37 
   38     $form['#title'] = $this->t('Reorder displays');
   39     $form['#section'] = 'reorder';
   40     $form['#action'] = Url::fromRoute('views_ui.form_reorder_displays', [
   41       'js' => 'nojs',
   42       'view' => $view->id(),
   43       'display_id' => $display_id,
   44     ])->toString();
   45     $form['view'] = [
   46       '#type' => 'value',
   47       '#value' => $view,
   48     ];
   49 
   50     $displays = $view->get('display');
   51     $count = count($displays);
   52 
   53     // Sort the displays.
   54     uasort($displays, function ($display1, $display2) {
   55       if ($display1['position'] != $display2['position']) {
   56         return $display1['position'] < $display2['position'] ? -1 : 1;
   57       }
   58       return 0;
   59     });
   60 
   61     $form['displays'] = [
   62       '#type' => 'table',
   63       '#id' => 'reorder-displays',
   64       '#header' => [$this->t('Display'), $this->t('Weight'), $this->t('Remove')],
   65       '#empty' => $this->t('No displays available.'),
   66       '#tabledrag' => [
   67         [
   68           'action' => 'order',
   69           'relationship' => 'sibling',
   70           'group' => 'weight',
   71         ],
   72       ],
   73       '#tree' => TRUE,
   74       '#prefix' => '<div class="scroll" data-drupal-views-scroll>',
   75       '#suffix' => '</div>',
   76     ];
   77 
   78     foreach ($displays as $id => $display) {
   79       $form['displays'][$id] = [
   80         '#display' => $display,
   81         '#attributes' => [
   82           'id' => 'display-row-' . $id,
   83         ],
   84         '#weight' => $display['position'],
   85       ];
   86 
   87       // Only make row draggable if it's not the default display.
   88       if ($id !== 'default') {
   89         $form['displays'][$id]['#attributes']['class'][] = 'draggable';
   90       }
   91 
   92       $form['displays'][$id]['title'] = [
   93         '#markup' => $display['display_title'],
   94       ];
   95 
   96       $form['displays'][$id]['weight'] = [
   97         '#type' => 'weight',
   98         '#value' => $display['position'],
   99         '#delta' => $count,
  100         '#title' => $this->t('Weight for @display', ['@display' => $display['display_title']]),
  101         '#title_display' => 'invisible',
  102         '#attributes' => [
  103           'class' => ['weight'],
  104         ],
  105       ];
  106 
  107       $form['displays'][$id]['removed'] = [
  108         'checkbox' => [
  109           '#title' => t('Remove @id', ['@id' => $id]),
  110           '#title_display' => 'invisible',
  111           '#type' => 'checkbox',
  112           '#id' => 'display-removed-' . $id,
  113           '#attributes' => [
  114             'class' => ['views-remove-checkbox'],
  115           ],
  116           '#default_value' => !empty($display['deleted']),
  117         ],
  118         'link' => [
  119           '#type' => 'link',
  120           '#title' => new FormattableMarkup('<span>@text</span>', ['@text' => $this->t('Remove')]),
  121           '#url' => Url::fromRoute('<none>'),
  122           '#attributes' => [
  123             'id' => 'display-remove-link-' . $id,
  124             'class' => ['views-button-remove', 'display-remove-link'],
  125             'alt' => $this->t('Remove this display'),
  126             'title' => $this->t('Remove this display'),
  127           ],
  128         ],
  129         '#access' => ($id !== 'default'),
  130       ];
  131 
  132       if (!empty($display['deleted'])) {
  133         $form['displays'][$id]['deleted'] = [
  134           '#type' => 'value',
  135           '#value' => TRUE,
  136         ];
  137 
  138         $form['displays'][$id]['#attributes']['class'][] = 'hidden';
  139       }
  140 
  141     }
  142 
  143     $view->getStandardButtons($form, $form_state, 'views_ui_reorder_displays_form');
  144 
  145     return $form;
  146   }
  147 
  148   /**
  149    * {@inheritdoc}
  150    */
  151   public function submitForm(array &$form, FormStateInterface $form_state) {
  152     /** @var $view \Drupal\views_ui\ViewUI */
  153     $view = $form_state->get('view');
  154     $order = [];
  155 
  156     $user_input = $form_state->getUserInput();
  157     foreach ($user_input['displays'] as $display => $info) {
  158       // Add each value that is a field with a weight to our list, but only if
  159       // it has had its 'removed' checkbox checked.
  160       if (is_array($info) && isset($info['weight']) && empty($info['removed']['checkbox'])) {
  161         $order[$display] = $info['weight'];
  162       }
  163     }
  164 
  165     // Sort the order array.
  166     asort($order);
  167 
  168     // Remove the default display from ordering.
  169     unset($order['default']);
  170     // Increment up positions.
  171     $position = 1;
  172 
  173     foreach (array_keys($order) as $display) {
  174       $order[$display] = $position++;
  175     }
  176 
  177     // Setting up position and removing deleted displays.
  178     $displays = $view->get('display');
  179     foreach ($displays as $display_id => &$display) {
  180       // Don't touch the default.
  181       if ($display_id === 'default') {
  182         $display['position'] = 0;
  183         continue;
  184       }
  185       if (isset($order[$display_id])) {
  186         $display['position'] = $order[$display_id];
  187       }
  188       else {
  189         $display['deleted'] = TRUE;
  190       }
  191     }
  192     $view->set('display', $displays);
  193 
  194     // Store in cache.
  195     $view->cacheSet();
  196     $url = $view->toUrl('edit-form')
  197       ->setOption('fragment', 'views-tab-default');
  198     $form_state->setRedirectUrl($url);
  199   }
  200 
  201 }