You are here

function photos_comment_form_submit in Album Photos 8.4

Same name and namespace in other branches
  1. 6.2 photos.module \photos_comment_form_submit()
  2. 7.3 photos.module \photos_comment_form_submit()

Submit handler for image comments.

1 string reference to 'photos_comment_form_submit'
photos_form_alter in ./photos.module
Implements hook_form_alter().

File

./photos.module, line 1131
Implementation of photos.module.

Code

function photos_comment_form_submit($form, FormStateInterface &$form_state) {
  if ($fid = $form_state
    ->getValue('photos_fid')) {
    $comment = $form_state
      ->getFormObject()
      ->getEntity();
    $db = \Drupal::database();

    // Record comment in photos_comment.
    $db
      ->insert('photos_comment')
      ->fields([
      'fid' => $fid,
      'cid' => $comment
        ->id(),
    ])
      ->execute();

    // Update image comment count.
    $count = $db
      ->query("SELECT COUNT(fid) FROM {photos_comment} WHERE fid = :fid", [
      ':fid' => $fid,
    ])
      ->fetchField();
    $db
      ->update('photos_image')
      ->fields([
      'comcount' => $count,
    ])
      ->condition('fid', $fid)
      ->execute();

    // Redirect back to image page.
    $url = Url::fromUri('base:photos/image/' . $fid);
    $form_state
      ->setRedirectUrl($url);
  }
}