You are here

public function PhotosRearrangeController::editSortAlbumsSave in Album Photos 8.5

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

Save new album weights.

Parameters

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

int $uid: The user ID.

Return value

string A message or empty string.

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

File

src/Controller/PhotosRearrangeController.php, line 416

Class

PhotosRearrangeController
Re-arrange view controller.

Namespace

Drupal\photos\Controller

Code

public function editSortAlbumsSave(array $order = [], $uid = 0) {
  $message = '';
  if ($uid) {
    $user = $this
      ->currentUser();
    $access = FALSE;
    if ($user
      ->id() == $uid || $user
      ->id() == 1 || $user
      ->hasPermission('edit any photos content')) {
      $weight = 0;

      // Update weight for all albums in array.
      foreach ($order as $album_id) {
        $album_id = str_replace('photos_', '', $album_id);
        try {
          $node = $this->entityTypeManager
            ->getStorage('node')
            ->load($album_id);

          // 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) {
          $this->connection
            ->update('photos_album')
            ->fields([
            'weight' => $weight,
          ])
            ->condition('album_id', $album_id)
            ->execute();
          $weight++;
        }
      }
      if ($weight > 0) {
        $message = $this
          ->t('Album order saved!');
      }
    }
  }
  return $message;
}