I have written a module that allows users to put comment on each paragraph. So in comment form there is a field that asks the user which paragraph do they want to put comment on. I have to mention that I have added a column to my comment table, which saves that paragraph number.
function Annotations_form_comment_form_alter(&$form, &$form_state, $form_id) {
$form['paragraph_id'] = array(
'#type' => 'textfield',
'#title' => t('Which paragraph you want to put comment on?'),
'#required' => TRUE,
'#size' => 10,
'#maxlength' => 2,
'#default_value' => !empty($form['#entity']->paragraph_id) ? $form['#entity']- >paragraph_id : NULL, // set the default value to the current paragraph value
'#weight' => $form['subject']['#weight'] + 1, // place after the subject field by setting the weight to +1 of subject weight
);
$form['#validate'][] = 'annotations_form_comment_form_validate';
$form['#submit'][] = 'annotations_form_comment_form_submit';
}
I also have Annotations_form_comment_form_validate and Annotations_form_comment_form_submit functions for validation and submitting.
I have also written the following code to sort the comments based on the number of the paragraph. So first you see the comments on paragraph 1, and then 2, etc.
<?php
define('COMMENT_SORT_OLDER_FIRST', 1); // default
define('COMMENT_SORT_Paragraph', 2);
/* Alter node type form: allow user to decide comments sorting */
function Annotations_form_node_type_form_alter(&$form, $form_state) {
$form['comment']['comment_default_sorting'] = array(
'#title' => t('Sort'),
'#type' => 'select',
'#options' => array(1 => t('Older first'),
2 => t('Paragraph Number')),
'#default_value' => variable_get('comment_default_sorting_' . $form['#node_type']->type, COMMENT_SORT_OLDER_FIRST),
);
}
/* Alter comments query */
function Annotations_query_comment_filter_alter(QueryAlterableInterface $query){
if ( ($node = $query->getMetaData('node')) && (get_class($query) == 'PagerDefault') ) {
$sort = variable_get('comment_default_sorting_' . $node->type, COMMENT_SORT_OLDER_FIRST);
if ($sort == COMMENT_SORT_Paragraph) {
$orderby =& $query->getOrderBy();
$expressions =& $query->getExpressions();
if (isset($orderby['torder'])) {
unset($expressions['torder']);
unset($orderby['torder']);
$orderby['c.paragraph_id'] = 'ASC';
} else {
$orderby['c.cid'] = 'DESC';
}
}
}
}
I want to have a line like: " Comments on Paragraph 1 " and then show the comments for paragraph 1, and the same for rest of the comments.
Does anybody know how can I do that or which hooks I can use? Do you know some other modules or examples that do something similar, so it will give me some idea that how to do it?