You are here

public function PhotosImageAddForm::save in Album Photos 6.0.x

Same name and namespace in other branches
  1. 8.5 src/Form/PhotosImageAddForm.php \Drupal\photos\Form\PhotosImageAddForm::save()

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/PhotosImageAddForm.php, line 129

Class

PhotosImageAddForm
Defines a form to edit images.

Namespace

Drupal\photos\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  // Save changes.

  /** @var \Drupal\photos\PhotosImageInterface $photosImage */
  $photosImage = $this->entity;
  $photosImage
    ->save();

  // Clear image page cache.
  Cache::invalidateTags([
    'photos:image:' . $photosImage
      ->id(),
  ]);
  if ($nid = $form_state
    ->getValue('nid')) {

    // Clear album page and node cache.
    Cache::invalidateTags([
      'photos:album:' . $nid,
      'node:' . $nid,
    ]);
  }

  // Update image statistics.
  if ($this
    ->config('photos.settings')
    ->get('photos_user_count_cron')) {
    $albumId = $photosImage
      ->getAlbumId();
    $uid = $photosImage
      ->getOwnerId();
    if ($albumId) {

      // Update album count.
      PhotosAlbum::setCount('node_album', $albumId);

      // Clear album page and node cache.
      Cache::invalidateTags([
        'photos:album:' . $albumId,
        'node:' . $albumId,
      ]);
    }
    if ($uid) {

      // Update user count.
      PhotosAlbum::setCount('user_image', $uid);
    }
  }
  $this->messenger
    ->addMessage($this
    ->t('Entity saved to album.'));
}