You are here

function social_comment_form_comment_form_alter in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  2. 8 modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  3. 8.2 modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  4. 8.3 modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  5. 8.4 modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  6. 8.5 modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  7. 8.6 modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  8. 8.8 modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  9. 10.3.x modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  10. 10.0.x modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  11. 10.1.x modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()
  12. 10.2.x modules/social_features/social_comment/social_comment.module \social_comment_form_comment_form_alter()

Implements hook_form_FORM_ID_alter().

Enhance the comment form.

File

modules/social_features/social_comment/social_comment.module, line 28
The Social comment module.

Code

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

  // Change value of 'Save' button.
  $form['actions']['submit']['#value'] = t('Comment', [], [
    'context' => 'Comment Button',
  ]);

  // Hide title of 'Comment' field.
  if (!empty($form['field_comment_body']['widget'][0]['value']['#title_display'])) {
    $form['field_comment_body']['widget'][0]['value']['#title_display'] = 'invisible';
  }

  // Validate replies to comments deeper than 1.
  $form['#validate'][] = 'social_comment_form_reply_validate';
  $route_name = \Drupal::routeMatch()
    ->getRouteName();
  switch ($route_name) {

    // Comment reply form.
    case 'comment.reply':

      // Check if we should remove the author fields and make the status field
      // consistent.
      social_comment_remove_author_field($form, FALSE);
      $form['actions']['submit']['#attributes']['value'] = t('Reply');
      $form['actions']['submit']['#value'] = t('Reply');
      $form['field_comment_body']['widget'][0]['#placeholder'] = t('Add a reply');
      break;

    // Comment edit form.
    case 'entity.comment.edit_form':

      // Check if we should remove the author fields and make the status field
      // consistent.
      social_comment_remove_author_field($form);
      $form['actions']['submit']['#attributes']['value'] = t('Submit');
      $form['actions']['submit']['#value'] = t('Submit');
      $form['actions']['submit']['#submit'][] = 'social_comment_edit_form_submit';
      break;
    default:

      // Redirect to the current url. We should not use this change for posts.
      // See social_post_form_comment_post_comment_form_alter for posts.
      if ($form_id !== 'comment_post_comment_form') {
        $form['#action'] = \Drupal::request()
          ->getRequestUri();
        $form['field_comment_body']['widget'][0]['#placeholder'] = t('Add a comment');
      }
      break;
  }
  $form['#attached']['library'][] = 'social_post/keycode-submit';
}