You are here

function photos_form_alter in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 photos.module \photos_form_alter()
  2. 6.2 photos.module \photos_form_alter()
  3. 7.3 photos.module \photos_form_alter()
  4. 6.0.x photos.module \photos_form_alter()

Implements hook_form_alter().

File

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

Code

function photos_form_alter(&$form, FormStateInterface &$form_state, $form_id) {

  // Comment form(s).
  // @todo dynamic comment form id?
  if ($form_id == 'comment_node_photos_form' && \Drupal::config('photos.settings')
    ->get('photos_comment') || $form_id == 'comment_comment_form' && \Drupal::config('photos.settings')
    ->get('photos_comment')) {

    // Get path args.
    $current_path = \Drupal::service('path.current')
      ->getPath();
    $arg = explode('/', $current_path);

    // Get fid.
    $fid = FALSE;
    if ($form_state
      ->getValue('photos_fid')) {
      $fid = $form_state
        ->getValue('photos_fid');
    }
    elseif ($form_state
      ->getValue('fid')) {
      $fid = $form_state
        ->getValue('fid');
    }
    elseif (isset($arg[3]) && $arg[1] == 'photos' && is_numeric($arg[3])) {
      $fid = $arg[3];
    }
    elseif (isset($arg[6]) && $arg[2] == 'reply' && is_numeric($arg[6])) {
      $db = \Drupal::database();
      $fid = $db
        ->query('SELECT fid FROM {photos_comment} WHERE cid = :cid', [
        ':cid' => $arg[6],
      ])
        ->fetchField();
    }
    if ($fid) {

      // Form action.
      $action_url = Url::fromUri('base:photos/image/' . $fid)
        ->toString();
      $form['#action'] = $action_url;

      // Hidden fid fields.
      $form['fid'] = [
        '#type' => 'hidden',
        '#value' => $fid,
      ];
      $form['comment']['photos_fid'] = [
        '#type' => 'hidden',
        '#value' => $fid,
      ];

      // Custom submit handler.
      foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
          $form['actions'][$action]['#submit'][] = 'photos_comment_form_submit';
        }
      }
    }
  }

  // Photos node form.
  if ($form_id == 'node_photos_form') {
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'photos_form_redirect';
      }
    }
  }
}