class ReorderDisplays in Drupal 8
Same name and namespace in other branches
- 9 core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php \Drupal\views_ui\Form\Ajax\ReorderDisplays
 
Displays the display reorder form.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\views_ui\Form\Ajax\ViewsFormBase implements ViewsFormInterface
- class \Drupal\views_ui\Form\Ajax\ReorderDisplays
 
 
 - class \Drupal\views_ui\Form\Ajax\ViewsFormBase implements ViewsFormInterface
 
Expanded class hierarchy of ReorderDisplays
File
- core/
modules/ views_ui/ src/ Form/ Ajax/ ReorderDisplays.php, line 14  
Namespace
Drupal\views_ui\Form\AjaxView source
class ReorderDisplays extends ViewsFormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormKey() {
    return 'reorder-displays';
  }
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'views_ui_reorder_displays_form';
  }
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    /** @var $view \Drupal\views\ViewEntityInterface */
    $view = $form_state
      ->get('view');
    $display_id = $form_state
      ->get('display_id');
    $form['#title'] = $this
      ->t('Reorder displays');
    $form['#section'] = 'reorder';
    $form['#action'] = Url::fromRoute('views_ui.form_reorder_displays', [
      'js' => 'nojs',
      'view' => $view
        ->id(),
      'display_id' => $display_id,
    ])
      ->toString();
    $form['view'] = [
      '#type' => 'value',
      '#value' => $view,
    ];
    $displays = $view
      ->get('display');
    $count = count($displays);
    // Sort the displays.
    uasort($displays, function ($display1, $display2) {
      if ($display1['position'] != $display2['position']) {
        return $display1['position'] < $display2['position'] ? -1 : 1;
      }
      return 0;
    });
    $form['displays'] = [
      '#type' => 'table',
      '#id' => 'reorder-displays',
      '#header' => [
        $this
          ->t('Display'),
        $this
          ->t('Weight'),
        $this
          ->t('Remove'),
      ],
      '#empty' => $this
        ->t('No displays available.'),
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'weight',
        ],
      ],
      '#tree' => TRUE,
      '#prefix' => '<div class="scroll" data-drupal-views-scroll>',
      '#suffix' => '</div>',
    ];
    foreach ($displays as $id => $display) {
      $form['displays'][$id] = [
        '#display' => $display,
        '#attributes' => [
          'id' => 'display-row-' . $id,
        ],
        '#weight' => $display['position'],
      ];
      // Only make row draggable if it's not the default display.
      if ($id !== 'default') {
        $form['displays'][$id]['#attributes']['class'][] = 'draggable';
      }
      $form['displays'][$id]['title'] = [
        '#markup' => $display['display_title'],
      ];
      $form['displays'][$id]['weight'] = [
        '#type' => 'weight',
        '#value' => $display['position'],
        '#delta' => $count,
        '#title' => $this
          ->t('Weight for @display', [
          '@display' => $display['display_title'],
        ]),
        '#title_display' => 'invisible',
        '#attributes' => [
          'class' => [
            'weight',
          ],
        ],
      ];
      $form['displays'][$id]['removed'] = [
        'checkbox' => [
          '#title' => t('Remove @id', [
            '@id' => $id,
          ]),
          '#title_display' => 'invisible',
          '#type' => 'checkbox',
          '#id' => 'display-removed-' . $id,
          '#attributes' => [
            'class' => [
              'views-remove-checkbox',
            ],
          ],
          '#default_value' => !empty($display['deleted']),
        ],
        'link' => [
          '#type' => 'link',
          '#title' => new FormattableMarkup('<span>@text</span>', [
            '@text' => $this
              ->t('Remove'),
          ]),
          '#url' => Url::fromRoute('<none>'),
          '#attributes' => [
            'id' => 'display-remove-link-' . $id,
            'class' => [
              'views-button-remove',
              'display-remove-link',
            ],
            'alt' => $this
              ->t('Remove this display'),
            'title' => $this
              ->t('Remove this display'),
          ],
        ],
        '#access' => $id !== 'default',
      ];
      if (!empty($display['deleted'])) {
        $form['displays'][$id]['deleted'] = [
          '#type' => 'value',
          '#value' => TRUE,
        ];
        $form['displays'][$id]['#attributes']['class'][] = 'hidden';
      }
    }
    $view
      ->getStandardButtons($form, $form_state, 'views_ui_reorder_displays_form');
    return $form;
  }
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    /** @var $view \Drupal\views_ui\ViewUI */
    $view = $form_state
      ->get('view');
    $order = [];
    $user_input = $form_state
      ->getUserInput();
    foreach ($user_input['displays'] as $display => $info) {
      // Add each value that is a field with a weight to our list, but only if
      // it has had its 'removed' checkbox checked.
      if (is_array($info) && isset($info['weight']) && empty($info['removed']['checkbox'])) {
        $order[$display] = $info['weight'];
      }
    }
    // Sort the order array.
    asort($order);
    // Remove the default display from ordering.
    unset($order['default']);
    // Increment up positions.
    $position = 1;
    foreach (array_keys($order) as $display) {
      $order[$display] = $position++;
    }
    // Setting up position and removing deleted displays.
    $displays = $view
      ->get('display');
    foreach ($displays as $display_id => &$display) {
      // Don't touch the default.
      if ($display_id === 'default') {
        $display['position'] = 0;
        continue;
      }
      if (isset($order[$display_id])) {
        $display['position'] = $order[$display_id];
      }
      else {
        $display['deleted'] = TRUE;
      }
    }
    $view
      ->set('display', $displays);
    // Store in cache.
    $view
      ->cacheSet();
    $url = $view
      ->toUrl('edit-form')
      ->setOption('fragment', 'views-tab-default');
    $form_state
      ->setRedirectUrl($url);
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            DependencySerializationTrait:: | 
                  protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| 
            DependencySerializationTrait:: | 
                  protected | property | An array of service IDs keyed by property name used for serialization. | |
| 
            DependencySerializationTrait:: | 
                  public | function | 1 | |
| 
            DependencySerializationTrait:: | 
                  public | function | 2 | |
| 
            FormBase:: | 
                  protected | property | The config factory. | 1 | 
| 
            FormBase:: | 
                  protected | property | The request stack. | 1 | 
| 
            FormBase:: | 
                  protected | property | The route match. | |
| 
            FormBase:: | 
                  protected | function | Retrieves a configuration object. | |
| 
            FormBase:: | 
                  protected | function | Gets the config factory for this form. | 1 | 
| 
            FormBase:: | 
                  private | function | Returns the service container. | |
| 
            FormBase:: | 
                  public static | function | 
            Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: | 
                  87 | 
| 
            FormBase:: | 
                  protected | function | Gets the current user. | |
| 
            FormBase:: | 
                  protected | function | Gets the request object. | |
| 
            FormBase:: | 
                  protected | function | Gets the route match. | |
| 
            FormBase:: | 
                  protected | function | Gets the logger for a specific channel. | |
| 
            FormBase:: | 
                  protected | function | 
            Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: | 
                  |
| 
            FormBase:: | 
                  public | function | Resets the configuration factory. | |
| 
            FormBase:: | 
                  public | function | Sets the config factory for this form. | |
| 
            FormBase:: | 
                  public | function | Sets the request stack object to use. | |
| 
            LinkGeneratorTrait:: | 
                  protected | property | The link generator. | 1 | 
| 
            LinkGeneratorTrait:: | 
                  protected | function | Returns the link generator. | |
| 
            LinkGeneratorTrait:: | 
                  protected | function | Renders a link to a route given a route name and its parameters. | |
| 
            LinkGeneratorTrait:: | 
                  public | function | Sets the link generator service. | |
| 
            LoggerChannelTrait:: | 
                  protected | property | The logger channel factory service. | |
| 
            LoggerChannelTrait:: | 
                  protected | function | Gets the logger for a specific channel. | |
| 
            LoggerChannelTrait:: | 
                  public | function | Injects the logger channel factory. | |
| 
            MessengerTrait:: | 
                  protected | property | The messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Gets the messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Sets the messenger. | |
| 
            RedirectDestinationTrait:: | 
                  protected | property | The redirect destination service. | 1 | 
| 
            RedirectDestinationTrait:: | 
                  protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| 
            RedirectDestinationTrait:: | 
                  protected | function | Returns the redirect destination service. | |
| 
            RedirectDestinationTrait:: | 
                  public | function | Sets the redirect destination service. | |
| 
            ReorderDisplays:: | 
                  public | function | 
            Form constructor. Overrides FormInterface:: | 
                  |
| 
            ReorderDisplays:: | 
                  public | function | 
            Returns a unique string identifying the form. Overrides FormInterface:: | 
                  |
| 
            ReorderDisplays:: | 
                  public | function | 
            Returns the key that represents this form. Overrides ViewsFormInterface:: | 
                  |
| 
            ReorderDisplays:: | 
                  public | function | 
            Form submission handler. Overrides ViewsFormBase:: | 
                  |
| 
            StringTranslationTrait:: | 
                  protected | property | The string translation service. | 1 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Formats a string containing a count of items. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Returns the number of plurals supported by a given language. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Gets the string translation service. | |
| 
            StringTranslationTrait:: | 
                  public | function | Sets the string translation service to use. | 2 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Translates a string to the current language or to a given language. | |
| 
            UrlGeneratorTrait:: | 
                  protected | property | The url generator. | |
| 
            UrlGeneratorTrait:: | 
                  protected | function | Returns the URL generator service. | |
| 
            UrlGeneratorTrait:: | 
                  public | function | Sets the URL generator service. | |
| 
            UrlGeneratorTrait:: | 
                  protected | function | Generates a URL or path for a specific route based on the given parameters. | |
| 
            ViewsFormBase:: | 
                  protected | property | The ID of the item this form is manipulating. | |
| 
            ViewsFormBase:: | 
                  protected | property | The type of item this form is manipulating. | |
| 
            ViewsFormBase:: | 
                  protected | function | Wrapper for handling AJAX forms. | |
| 
            ViewsFormBase:: | 
                  public | function | 
            Creates a new instance of this form. Overrides ViewsFormInterface:: | 
                  6 | 
| 
            ViewsFormBase:: | 
                  public | function | 
            Gets the form state for this form. Overrides ViewsFormInterface:: | 
                  1 | 
| 
            ViewsFormBase:: | 
                  protected | function | Sets the ID for this form. | |
| 
            ViewsFormBase:: | 
                  protected | function | Sets the type for this form. | |
| 
            ViewsFormBase:: | 
                  public | function | 
            Form validation handler. Overrides FormBase:: | 
                  3 |