You are here

public function AjaxCommentsForm::buildForm in AJAX Comments 8

Form constructor.

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

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/AjaxCommentsForm.php, line 111

Class

AjaxCommentsForm
Provides ajax enhancements to core default Comment form.

Namespace

Drupal\ajax_comments\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $request = $this->requestStack
    ->getCurrentRequest();
  $is_ajax = Utility::isAjaxRequest($request, $form_state
    ->getUserInput());
  $route_name = $this->currentRouteMatch
    ->getRouteName();

  // If this is an ajax request, ensure that id attributes are generated
  // as unique.
  if ($is_ajax) {
    Html::setIsAjax(TRUE);
  }

  // Ajax replies to other comments should happen on the canonical entity page
  // (note this functionality has not been ported to D8, yet).
  // If the user is on the standalone comment reply page or comment edit page,
  // it means JavaScript is disabled or the ajax functionality is not working.
  // Do not proceed with the form alter.
  if (in_array($this->currentRouteMatch
    ->getRouteName(), [
    'comment.reply',
    'entity.comment.edit_form',
  ])) {
    return $form;
  }

  /** @var \Drupal\comment\CommentInterface $comment */
  $comment = $form_state
    ->getFormObject()
    ->getEntity();

  // Check to see if this comment field uses ajax comments.
  $comment_formatter = $this->fieldSettingsHelper
    ->getFieldFormatterFromComment($comment, 'full');
  if (empty($comment_formatter) || !$this->fieldSettingsHelper
    ->isEnabled($comment_formatter)) {

    // If not using Ajax Comments, return the unmodified form.
    return $form;
  }

  /** @var \Drupal\Core\Entity\ContentEntityInterface $commented_entity */
  $commented_entity = $comment
    ->getCommentedEntity();
  $field_name = $comment
    ->getFieldName();
  $cid = $comment
    ->id() ? $comment
    ->id() : 0;
  $pid = $comment
    ->get('pid')->target_id ? $comment
    ->get('pid')->target_id : 0;
  $id = 'ajax-comments-reply-form-' . $commented_entity
    ->getEntityTypeId() . '-' . $commented_entity
    ->id() . '-' . $field_name . '-' . $pid . '-' . $cid;
  $form['#attributes']['id'] = Html::getUniqueId($id);

  // Add the form's id as a hidden input so we can
  // access it in the controller.
  $form['form_html_id'] = [
    '#type' => 'hidden',
    '#value' => $form['#attributes']['id'],
  ];

  // If this is an instance of the form that was submitted (not a child
  // form on a comment field attached to this comment), then
  // update the temp store values while rebuilding the form, if necessary.
  $this->tempStore
    ->processForm($request, $form, $form_state);
  if ($is_ajax && in_array($route_name, [
    'ajax_comments.edit',
    'ajax_comments.reply',
  ])) {
    $wrapper_html_id = $this->tempStore
      ->getSelectorValue($request, 'wrapper_html_id');
  }
  else {
    $wrapper_html_id = Utility::getWrapperIdFromEntity($commented_entity, $field_name);
  }

  // Add the wrapping fields's HTML id as a hidden input
  // so we can access it in the controller.
  // NOTE: This field needs to be declared here, and the only the #value
  // property overridden in $this->setWrapperId(). Otherwise, if the field
  // were NOT declared here but rather only in $this->setWrapperId(), then
  // the hidden input element in the markup will not be generated correctly
  // when $this->setWrapperId() is called from $this->validateForm().
  $form['wrapper_html_id'] = [
    '#type' => 'hidden',
    '#value' => $wrapper_html_id,
  ];

  // Add the wrapping fields's HTML id.
  $this
    ->setWrapperId($form, $wrapper_html_id);
  return $form;
}