You are here

public static function MultiStepDisplay::saveNewOrder in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\MultiStepDisplay::saveNewOrder()

Saves new ordering of entities based on weight.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

3 calls to MultiStepDisplay::saveNewOrder()
MultiStepDisplay::executeJsCommand in src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php
Execute command generated by front-end.
MultiStepDisplay::removeItemSubmit in src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php
Submit callback for remove buttons.
MultiStepDisplay::submit in src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php
Submits form.

File

src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php, line 397

Class

MultiStepDisplay
Show current selection and delivers selected entities.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay

Code

public static function saveNewOrder(FormStateInterface $form_state) {
  $selected = $form_state
    ->getValue('selected');
  if (!empty($selected)) {
    $weights = array_column($selected, 'weight');
    $selected_entities = $form_state
      ->get([
      'entity_browser',
      'selected_entities',
    ]);

    // If we added new entities to the selection at this step we won't have
    // weights for them so we have to fake them.
    $diff_selected_size = count($selected_entities) - count($weights);
    if ($diff_selected_size > 0) {
      $max_weight = max($weights) + 1;
      for ($new_weight = $max_weight; $new_weight < $max_weight + $diff_selected_size; $new_weight++) {
        $weights[] = $new_weight;
      }
    }
    $ordered = array_combine($weights, $selected_entities);
    ksort($ordered);
    $form_state
      ->set([
      'entity_browser',
      'selected_entities',
    ], $ordered);
  }
}