comment_form(&$form_state, $edit, $title = NULL)
drupal/modules/comment/comment.module, line 1320
Generate the basic commenting form, for appending to a node or display on a separate page.
@see comment_form_validate() @see comment_form_submit()
$title Not used.
| Name | Description |
|---|---|
| Form builder functions | Functions that build an abstract representation of a HTML form. |
function comment_form(&$form_state, $edit, $title = NULL) {
global $user;
$op = isset($_POST['op']) ? $_POST['op'] : '';
$node = node_load($edit['nid']);
if (!$user->uid && variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
drupal_add_js(drupal_get_path('module', 'comment') . '/comment.js');
}
$edit += array('name' => '', 'mail' => '', 'homepage' => '');
if ($user->uid) {
if (!empty($edit['cid']) && user_access('administer comments')) {
if (!empty($edit['author'])) {
$author = $edit['author'];
}
elseif (!empty($edit['name'])) {
$author = $edit['name'];
}
else {
$author = $edit['registered_name'];
}
if (!empty($edit['status'])) {
$status = $edit['status'];
}
else {
$status = 0;
}
if (!empty($edit['date'])) {
$date = $edit['date'];
}
else {
$date = format_date($edit['timestamp'], 'custom', 'Y-m-d H:i O');
}
$form['admin'] = array(
'#type' => 'fieldset',
'#title' => t('Administration'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => -2,
);
if ($edit['registered_name'] != '') {
// The comment is by a registered user.
$form['admin']['author'] = array(
'#type' => 'textfield',
'#title' => t('Authored by'),
'#size' => 30,
'#maxlength' => 60,
'#autocomplete_path' => 'user/autocomplete',
'#default_value' => $author,
'#weight' => -1,
);
}
else {
// The comment is by an anonymous user.
$form['is_anonymous'] = array(
'#type' => 'value',
'#value' => TRUE,
);
$form['admin']['name'] = array(
'#type' => 'textfield',
'#title' => t('Authored by'),
'#size' => 30,
'#maxlength' => 60,
'#default_value' => $author,
'#weight' => -1,
);
$form['admin']['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#maxlength' => 64,
'#size' => 30,
'#default_value' => $edit['mail'],
'#description' => t('The content of this field is kept private and will not be shown publicly.'),
);
$form['admin']['homepage'] = array(
'#type' => 'textfield',
'#title' => t('Homepage'),
'#maxlength' => 255,
'#size' => 30,
'#default_value' => $edit['homepage'],
);
}
$form['admin']['date'] = array(
'#type' => 'textfield',
'#parents' => array('date'),
'#title' => t('Authored on'),
'#size' => 20,
'#maxlength' => 25,
'#default_value' => $date,
'#weight' => -1,
);
$form['admin']['status'] = array(
'#type' => 'radios',
'#parents' => array('status'),
'#title' => t('Status'),
'#default_value' => $status,
'#options' => array(t('Not published'), t('Published')),
'#weight' => -1,
);
}
else {
$form['_author'] = array(
'#type' => 'item',
'#title' => t('Your name'),
'#markup' => theme('username', $user),
);
$form['author'] = array(
'#type' => 'value',
'#value' => $user->name,
);
}
}
elseif (variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAY_CONTACT) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 60,
'#size' => 30,
'#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous')),
);
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#maxlength' => 64,
'#size' => 30,
'#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.'),
);
$form['homepage'] = array(
'#type' => 'textfield',
'#title' => t('Homepage'),
'#maxlength' => 255,
'#size' => 30,
'#default_value' => $edit['homepage'],
);
}
elseif (variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 60,
'#size' => 30,
'#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous')),
'#required' => TRUE,
);
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#maxlength' => 64,
'#size' => 30,
'#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.'),
'#required' => TRUE,
);
$form['homepage'] = array(
'#type' => 'textfield',
'#title' => t('Homepage'),
'#maxlength' => 255,
'#size' => 30,
'#default_value' => $edit['homepage'],
);
}
if (variable_get('comment_subject_field_' . $node->type, 1) == 1) {
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#maxlength' => 64,
'#default_value' => !empty($edit['subject']) ? $edit['subject'] : '',
);
}
if (!empty($edit['comment'])) {
$default = $edit['comment'];
}
else {
$default = '';
}
$form['comment'] = array(
'#type' => 'textarea',
'#title' => t('Comment'),
'#rows' => 15,
'#default_value' => $default,
'#input_format' => isset($edit['format']) ? $edit['format'] : FILTER_FORMAT_DEFAULT,
'#required' => TRUE,
);
$form['cid'] = array(
'#type' => 'value',
'#value' => !empty($edit['cid']) ? $edit['cid'] : NULL,
);
$form['pid'] = array(
'#type' => 'value',
'#value' => !empty($edit['pid']) ? $edit['pid'] : NULL,
);
$form['nid'] = array(
'#type' => 'value',
'#value' => $edit['nid'],
);
$form['uid'] = array(
'#type' => 'value',
'#value' => !empty($edit['uid']) ? $edit['uid'] : 0,
);
// Only show the save button if comment previews are optional or if we are
// already previewing the submission. However, if there are form errors,
// we hide the save button no matter what, so that optional form elements
// (e.g., captchas) can be updated.
if (!form_get_errors() && ((variable_get('comment_preview_' . $node->type, COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) || ($op == t('Preview')) || ($op == t('Save')))) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 19,
);
}
$form['preview'] = array(
'#type' => 'button',
'#value' => t('Preview'),
'#weight' => 20,
);
$form['#token'] = 'comment' . $edit['nid'] . (isset($edit['pid']) ? $edit['pid'] : '');
if ($op == t('Preview')) {
$form['#after_build'] = array('comment_form_add_preview');
}
if (empty($edit['cid']) && empty($edit['pid'])) {
$form['#action'] = url('comment/reply/' . $edit['nid']);
}
return $form;
}