You are here

public function AjaxController::ajaxSave in Sortableviews 8

Saves new weights.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

Return value

\Drupal\Core\Ajax\AjaxResponse An response with Ajax commands.

1 string reference to 'AjaxController::ajaxSave'
sortableviews.routing.yml in ./sortableviews.routing.yml
sortableviews.routing.yml

File

src/Controller/AjaxController.php, line 94

Class

AjaxController
Controller for sortableviews ajax calls.

Namespace

Drupal\sortableviews\Controller

Code

public function ajaxSave(Request $request) {
  $entity_type = $request
    ->get('entity_type');
  $field = $request
    ->get('weight_field');
  $current_order = $this
    ->retrieveOrderFromRequest($request);
  $entities = $this->entityManager
    ->getStorage($entity_type)
    ->loadMultiple(array_values($current_order));
  foreach ($entities as $entity) {
    $entity
      ->set($field, array_search($entity
      ->id(), $current_order));
    $entity
      ->save();
  }
  $content = [
    '#type' => 'container',
    '#attributes' => [
      'class' => 'status-messages',
    ],
    'messages' => [
      '#theme' => 'status_messages',
      '#message_list' => [
        'status' => [
          $this
            ->t('Changes have been saved.'),
        ],
      ],
    ],
  ];
  $response = new AjaxResponse();
  $response
    ->addCommand(new PrependCommand('.js-view-dom-id-' . $request
    ->get('dom_id'), $content));
  $response
    ->addCommand(new RemoveCommand('.js-view-dom-id-' . $request
    ->get('dom_id') . ' .sortableviews-ajax-trigger'));
  return $response;
}