You are here

function photos_image_edit_submit_comment in Album Photos 8.4

Image edit submit move comments to new node.

1 string reference to 'photos_image_edit_submit_comment'
photos_form_photos_image_edit_alter in ./photos.module
Implements hook_form_FORM_ID_alter().

File

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

Code

function photos_image_edit_submit_comment($form, FormStateInterface &$form_state) {
  $fid = $form_state
    ->getValue('fid');
  $pid = $form_state
    ->getValue('pid');
  $old_pid = $form_state
    ->getValue('oldpid');
  if ($pid != $old_pid) {
    $db = \Drupal::database();
    $sub_select = $db
      ->select('photos_comment', 'v')
      ->fields('v', [
      'cid',
    ])
      ->condition('v.fid', $fid)
      ->execute()
      ->fetchCol();
    if (!empty($sub_select)) {

      // Update comment entity_id.
      $db
        ->update('comment_field_data')
        ->fields([
        'entity_id' => $pid,
      ])
        ->condition('cid', $sub_select, 'IN')
        ->execute();

      // Update comment statistics.
      foreach ($sub_select as $cid) {
        $comment = \Drupal::entityTypeManager()
          ->getStorage('comment')
          ->load($cid);

        // Update old album comment statistics.
        \Drupal::service('comment.statistics')
          ->update($comment);

        // Reset cache and reload comment.
        \Drupal::entityTypeManager()
          ->getStorage('comment')
          ->resetCache([
          $cid,
        ]);
        $comment = \Drupal::entityTypeManager()
          ->getStorage('comment')
          ->load($cid);
        Cache::invalidateTags($comment
          ->getCacheTagsToInvalidate());

        // Update new album comment statistics.
        \Drupal::service('comment.statistics')
          ->update($comment);
      }
    }
  }
}