class CommentFormController extends EntityFormControllerNG {
public function form(array $form, array &$form_state) {
global $user;
$comment = $this->entity;
$node = $comment->nid->entity;
$form['#id'] = drupal_html_id('comment_form');
$form['#theme'] = array(
'comment_form__node_' . $node->type,
'comment_form',
);
$anonymous_contact = variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT);
$is_admin = $comment
->id() && user_access('administer comments');
if (!$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
$form['#attached']['library'][] = array(
'system',
'jquery.cookie',
);
$form['#attributes']['class'][] = 'user-info-from-cookie';
}
if (!$comment
->id() && !$comment->pid->target_id) {
$form['#action'] = url('comment/reply/' . $comment->nid->target_id);
}
if (isset($form_state['comment_preview'])) {
$form += $form_state['comment_preview'];
}
$form['author'] = array(
'#weight' => 10,
);
if ($is_admin) {
$form['author'] += array(
'#type' => 'details',
'#title' => t('Administration'),
'#collapsed' => TRUE,
);
}
if ($is_admin) {
$author = $comment->name->value;
$status = isset($comment->status->value) ? $comment->status->value : COMMENT_NOT_PUBLISHED;
$date = !empty($comment->date) ? $comment->date : new DrupalDateTime($comment->created->value);
}
else {
if ($user->uid) {
$author = $user->name;
}
else {
$author = $comment->name->value ? $comment->name->value : '';
}
$status = user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
$date = '';
}
$form['author']['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#default_value' => $author,
'#required' => !$user->uid && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT,
'#maxlength' => 60,
'#size' => 30,
);
if ($is_admin) {
$form['author']['name']['#title'] = t('Authored by');
$form['author']['name']['#description'] = t('Leave blank for %anonymous.', array(
'%anonymous' => config('user.settings')
->get('anonymous'),
));
$form['author']['name']['#autocomplete_path'] = 'user/autocomplete';
}
elseif ($user->uid) {
$form['author']['name']['#type'] = 'item';
$form['author']['name']['#value'] = $form['author']['name']['#default_value'];
$form['author']['name']['#markup'] = theme('username', array(
'account' => $user,
));
}
$form['author']['mail'] = array(
'#type' => 'email',
'#title' => t('E-mail'),
'#default_value' => $comment->mail->value,
'#required' => !$user->uid && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT,
'#maxlength' => 64,
'#size' => 30,
'#description' => t('The content of this field is kept private and will not be shown publicly.'),
'#access' => $is_admin || !$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT,
);
$form['author']['homepage'] = array(
'#type' => 'url',
'#title' => t('Homepage'),
'#default_value' => $comment->homepage->value,
'#maxlength' => 255,
'#size' => 30,
'#access' => $is_admin || !$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT,
);
$form['author']['date'] = array(
'#type' => 'datetime',
'#title' => t('Authored on'),
'#default_value' => $date,
'#size' => 20,
'#access' => $is_admin,
);
$form['author']['status'] = array(
'#type' => 'radios',
'#title' => t('Status'),
'#default_value' => $status,
'#options' => array(
COMMENT_PUBLISHED => t('Published'),
COMMENT_NOT_PUBLISHED => t('Not published'),
),
'#access' => $is_admin,
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#maxlength' => 64,
'#default_value' => $comment->subject->value,
'#access' => variable_get('comment_subject_field_' . $node->type, 1) == 1,
);
$form['is_anonymous'] = array(
'#type' => 'value',
'#value' => $comment
->id() ? !$comment->uid->target_id : !$user->uid,
);
if ($comment
->isNew()) {
$language_content = language(Language::TYPE_CONTENT);
$comment->langcode->value = $language_content->langcode;
}
foreach (array(
'cid',
'pid',
'nid',
'uid',
'node_type',
'langcode',
) as $key) {
$key_name = key($comment->{$key}
->offsetGet(0)
->getPropertyDefinitions());
$form[$key] = array(
'#type' => 'value',
'#value' => $comment->{$key}->{$key_name},
);
}
return parent::form($form, $form_state, $comment);
}
protected function actions(array $form, array &$form_state) {
$element = parent::actions($form, $form_state);
$comment = $this->entity;
$node = $comment->nid->entity;
$preview_mode = variable_get('comment_preview_' . $node->type, DRUPAL_OPTIONAL);
unset($element['delete']);
$element['submit']['#button_type'] = 'primary';
$element['submit']['#access'] = $comment
->id() && user_access('administer comments') || $preview_mode != DRUPAL_REQUIRED || isset($form_state['comment_preview']);
$element['preview'] = array(
'#type' => 'submit',
'#value' => t('Preview'),
'#access' => $preview_mode != DRUPAL_DISABLED,
'#validate' => array(
array(
$this,
'validate',
),
),
'#submit' => array(
array(
$this,
'submit',
),
array(
$this,
'preview',
),
),
);
return $element;
}
public function validate(array $form, array &$form_state) {
parent::validate($form, $form_state);
if (!empty($form_state['values']['cid'])) {
$account = user_load_by_name($form_state['values']['name']);
$form_state['values']['uid'] = $account ? $account->uid : 0;
$date = $form_state['values']['date'];
if ($date instanceof DrupalDateTime && $date
->hasErrors()) {
form_set_error('date', t('You have to specify a valid date.'));
}
if ($form_state['values']['name'] && !$form_state['values']['is_anonymous'] && !$account) {
form_set_error('name', t('You have to specify a valid author.'));
}
}
elseif ($form_state['values']['is_anonymous']) {
if ($form_state['values']['name']) {
$query = db_select('users', 'u');
$query
->addField('u', 'uid', 'uid');
$taken = $query
->condition('name', db_like($form_state['values']['name']), 'LIKE')
->countQuery()
->execute()
->fetchField();
if ($taken) {
form_set_error('name', t('The name you used belongs to a registered user.'));
}
}
}
}
public function buildEntity(array $form, array &$form_state) {
$comment = parent::buildEntity($form, $form_state);
if (!empty($form_state['values']['date']) && $form_state['values']['date'] instanceof DrupalDateTime) {
$comment->created->value = $form_state['values']['date']
->getTimestamp();
}
else {
$comment->created->value = REQUEST_TIME;
}
$comment->changed->value = REQUEST_TIME;
return $comment;
}
public function submit(array $form, array &$form_state) {
$comment = parent::submit($form, $form_state);
if (!$comment->is_anonymous && !empty($comment->name->value) && ($account = user_load_by_name($comment->name->value))) {
$comment->uid->target_id = $account->uid;
}
if ($comment->is_anonymous && (!isset($comment->name->value) || $comment->name->value === '')) {
$comment->name->value = config('user.settings')
->get('anonymous');
}
if (trim($comment->subject->value) == '') {
$comment_text = $comment->comment_body->processed;
$comment->subject = truncate_utf8(trim(decode_entities(strip_tags($comment_text))), 29, TRUE);
if ($comment->subject->value == '') {
$comment->subject->value = t('(No subject)');
}
}
return $comment;
}
public function preview(array $form, array &$form_state) {
$comment = $this->entity;
drupal_set_title(t('Preview comment'), PASS_THROUGH);
$form_state['comment_preview'] = comment_preview($comment);
$form_state['rebuild'] = TRUE;
}
public function save(array $form, array &$form_state) {
$node = node_load($form_state['values']['nid']);
$comment = $this->entity;
if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_OPEN)) {
if (user_is_anonymous()) {
user_cookie_save(array_intersect_key($form_state['values'], array_flip(array(
'name',
'mail',
'homepage',
))));
}
$comment
->save();
$form_state['values']['cid'] = $comment
->id();
watchdog('content', 'Comment posted: %subject.', array(
'%subject' => $comment->subject->value,
), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment
->id(), array(
'fragment' => 'comment-' . $comment
->id(),
)));
if ($comment->status->value == COMMENT_NOT_PUBLISHED) {
if (!user_access('administer comments')) {
drupal_set_message(t('Your comment has been queued for review by site administrators and will be published after approval.'));
}
}
else {
drupal_set_message(t('Your comment has been posted.'));
}
$query = array();
$page = comment_get_display_page($comment
->id(), $node->type);
if ($page > 0) {
$query['page'] = $page;
}
$redirect = array(
'node/' . $node->nid,
array(
'query' => $query,
'fragment' => 'comment-' . $comment
->id(),
),
);
}
else {
watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array(
'%subject' => $comment->subject->value,
), WATCHDOG_WARNING);
drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array(
'%subject' => $comment->subject->value,
)), 'error');
$redirect = 'node/' . $node->nid;
}
$form_state['redirect'] = $redirect;
cache_invalidate_tags(array(
'content' => TRUE,
));
}
}