You are here

public function PhotosRearrangeController::ajaxRearrange in Album Photos 8.5

Same name and namespace in other branches
  1. 8.4 src/Controller/PhotosRearrangeController.php \Drupal\photos\Controller\PhotosRearrangeController::ajaxRearrange()
  2. 6.0.x src/Controller/PhotosRearrangeController.php \Drupal\photos\Controller\PhotosRearrangeController::ajaxRearrange()

Ajax callback to save new image order.

1 string reference to 'PhotosRearrangeController::ajaxRearrange'
photos.routing.yml in ./photos.routing.yml
photos.routing.yml

File

src/Controller/PhotosRearrangeController.php, line 310

Class

PhotosRearrangeController
Re-arrange view controller.

Namespace

Drupal\photos\Controller

Code

public function ajaxRearrange() {
  $cache_tags = [
    'photos_image_list',
  ];

  // @todo convert to CommandInterface class?
  $post_nid = $this->requestStack
    ->getCurrentRequest()->request
    ->get('album_id');
  $post_uid = $this->requestStack
    ->getCurrentRequest()->request
    ->get('uid');
  $post_type = $this->requestStack
    ->getCurrentRequest()->request
    ->get('type');
  $post_order = $this->requestStack
    ->getCurrentRequest()->request
    ->get('order');
  $nid = $post_nid ?: 0;
  $uid = $post_uid ?: 0;
  $type = $post_type ?: 0;
  $new_order = $post_order ?: [];
  $message = '';
  if (!empty($new_order) && is_array($new_order)) {
    if ($type == 'images') {
      if ($nid) {
        $message = $this
          ->editSortSave($new_order, $nid, $type);
      }
    }
    elseif ($type == 'albums') {
      if ($uid) {
        $cache_tags[] = 'node_list';

        // Save sort order for albums.
        $message = $this
          ->editSortAlbumsSave($new_order, $uid);
      }
    }
  }
  if ($nid) {

    // Clear album page cache.
    $cache_tags = array_merge($cache_tags, [
      'node:' . $nid,
      'photos:album:' . $nid,
    ]);
  }

  // Invalidate cache tags.
  Cache::invalidateTags($cache_tags);

  // Build plain text response.
  $response = new Response();
  $response->headers
    ->set('Content-Type', 'text/plain');
  $response
    ->setContent($message);
  return $response;
}