0

I am writing a module which is adding a new filed to comment table, and in the comment form I want a new filed to be displayed, saying: : " Which paragraph you want to put comment on?".

I have already added the new filed to comment table, I just do not know what hook should I use, so this new filed will be also shown on comment form.

1

First, you need to make sure Drupal knows about your paragraph field. Adding it to the database alone won't work. Use hook_schema_alter() to alter the DB schema information for the comment table. Do this by adding something like the below to your module's install file (a PHP file you name mymodule.install in your module's folder):

function MYMODULE_schema_alter(&$schema) {
    $schema['comment']['fields']['paragraph'] = array(
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'description' => 'Paragraph of the comment.',
    );
}

Be sure to flush your caches to update the schema.

Now you need to alter the comment form, and process the paragraph value. Something like the below should work:

function MYMODULE_form_comment_form_alter(&$form, &$form_state, $form_id) {
    $form['paragraph'] = array(
        '#type' => 'textfield',
        '#title' => t('Which paragraph you want to put comment on?'),
        '#required' => FALSE,
        '#size' => 10,
        '#maxlength' => 2,
        '#default_value' => !empty($form['#entity']->paragraph) ? $form['#entity']->paragraph : 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'][] = 'MYMODULE_form_comment_form_validate';
    $form['#submit'][] = 'MYMODULE_form_comment_form_submit';
}

function MYMODULE_form_comment_form_validate(&$form, &$form_state) {
    if (!is_numeric($form_state['values']['paragraph'])) {
        // Validate the paragraph value to make sure it is numeric. Non-numeric values will break because we created paragraph as an integer field
        form_set_error('paragraph', t('Only numeric values are allowed for paragraph number.'));
    }
}

function MYMODULE_form_comment_form_submit(&$form, &$form_state) {
    // update the paragraph field in the comment table
    db_update('comment')
    ->fields(
        array(
            'paragraph' => $form_state['values']['paragraph'],
        )
    )
    ->condition('cid', $form_state['comment']->cid)
    ->execute();
}
2
  • Do you know how can I show the value of this new field, in the comments. So when users views a comment, beside seeing the subject and body of the comment, he also sees the value of paragraph_id. The reason I need it is that, when a user wants to reply to a comment, he will know what is the paragraph number of the comment he is replying to. Oct 24 '14 at 4:29
  • I have also ask the question at drupal.stackexchange.com/questions/132236/… Oct 24 '14 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.