You are here

function authcache_comment_form_comment_form_alter in Authenticated User Page Caching (Authcache) 7.2

Implements hook_form_FORM_ID_alter().

Replace the username in the comment-form with a placeholder.

File

modules/authcache_comment/authcache_comment.module, line 129
Provide personalization for the comment module.

Code

function authcache_comment_form_comment_form_alter(&$form, &$form_state) {
  global $user;

  // When displaying the comment form for new comments (not on edit) prepare the
  // author field such that the user name does not leak into the form cache.
  if ($user->uid && authcache_page_is_cacheable() && !$form_state['comment']->cid) {

    // Retrieve the username from settings.
    if (isset($form['author']['_author']) && !authcache_element_is_cacheable($form['author']['_author'])) {
      $replacement = array(
        '#theme' => 'html_tag',
        '#tag' => 'span',
      );
      authcache_user_attach_property($replacement, 'name');
      $form['author']['_author']['#markup'] = render($replacement);
      authcache_element_set_cacheable($form['author']['_author']);
    }

    // Remove the author name #value in order to prevent it from entering the
    // form cache. Instead add a validation callback which will restore the
    // value from the $user global upon submission.
    if (isset($form['author']['name']['#type']) && $form['author']['name']['#type'] === 'value' && !authcache_element_is_cacheable($form['author']['name'])) {
      $form['author']['name']['#value'] = '';
      $form['author']['name']['#element_validate'][] = 'authcache_comment_form_restore_author';
      authcache_element_set_cacheable($form['author']['name']);
    }
  }

  // The comment form uses value form elements to store "internal" comment
  // properties which are not editable by any user. Luckily none of them depends
  // on the currently logged in user, therefore it is safe to flag them as
  // cacheable without taking additional measures.
  $cacheable_value_elements = array(
    'is_anonymous',
    'cid',
    'pid',
    'nid',
    'language',
    'uid',
    'node_type',
  );
  foreach ($cacheable_value_elements as $key) {
    if (isset($form[$key]['#type']) && $form[$key]['#type'] === 'value' && !authcache_element_is_cacheable($form[$key])) {
      authcache_element_set_cacheable($form[$key]);
    }
  }
}