You are here

public function PhotosRearrangeController::editSortSave in Album Photos 8.5

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

Save new order.

Parameters

array $order: An array of photos_image IDs in order of appearance.

int $nid: The album node ID.

string $type: The type (currently only images are supported).

Return value

string A message or empty string.

1 call to PhotosRearrangeController::editSortSave()
PhotosRearrangeController::ajaxRearrange in src/Controller/PhotosRearrangeController.php
Ajax callback to save new image order.

File

src/Controller/PhotosRearrangeController.php, line 363

Class

PhotosRearrangeController
Re-arrange view controller.

Namespace

Drupal\photos\Controller

Code

public function editSortSave(array $order = [], $nid = 0, $type = 'images') {
  $message = '';
  if ($nid) {
    $access = FALSE;
    if ($nid) {
      try {
        $node = $this->entityTypeManager
          ->getStorage('node')
          ->load($nid);

        // Check for node_access.
        $access = $node
          ->getType() == 'photos' && $node
          ->access('update');
      } catch (InvalidPluginDefinitionException $e) {
        watchdog_exception('photos', $e);
      } catch (PluginNotFoundException $e) {
        watchdog_exception('photos', $e);
      }
    }
    if ($access) {
      $weight = 0;

      // Update weight for all images in array / album.
      foreach ($order as $image_id) {
        $id = str_replace('photos_', '', $image_id);
        if ($type == 'images') {

          // Save sort order for images in album.
          $this->connection
            ->update('photos_image_field_data')
            ->fields([
            'weight' => $weight,
          ])
            ->condition('id', $id)
            ->condition('album_id', $nid)
            ->execute();
        }
        $weight++;
      }
      if ($weight > 0) {
        $message = $this
          ->t('Image order saved!');
      }
    }
  }
  return $message;
}