3

I want to alter the comment subject field title width.

Core comment module sets it to 64 characters, and I want to change it up to 100 characters.

Now I tried to alter the schema using hook_schema_alter() function. Below is my code snippet, which I tried,

/**
 * Implements hook_schema_alter().
 */
function comment_alter_schema_alter(&$schema) {
    if (isset($schema['comment'])) {
        $schema['comment']['fields']['subject'] = array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The comment title.',
        );
    }
}

I am confused whether do I have to write some stuff in .install file or not?

Now after installing module how can i check whether the width of subject field is increased or not. If I see the comment table then it is as it is to 64 characters. I could find this in your table structure using PHPMyAdmin.

1 Answer 1

2

This hook is not used to edit the database as such. It is used to alter the schema when using the drupal_get_schema

Gets the schema definition of a table, or the whole database schema.

The returned schema will include any modifications made by any module that implements hook_schema_alter().

To do what you want you will have to use the db_change_field and this would go in the .install file.

You can check out the Schema API to get other relavent functions

You have to write db_change_field() in hook_enable() function.

Here is my modified answer:

/**
 * Implements hook_enable().
 */
function comment_alter_enable() {
    db_change_field('comment', 'subject', 'subject', array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The comment title.',
        )
    );
}
7
  • I have added following code in .install file, but it is still not working. /** * Implements hook_install(). */ function comment_alter_install() { db_change_field('comment', 'subject', 'subject', array( 'type' => 'varchar', 'length' => 100, 'not null' => TRUE, 'default' => '', 'description' => 'The comment title.', ) ); } Commented Feb 5, 2013 at 6:53
  • did you uninstall and then reinstall the module> Commented Feb 5, 2013 at 7:07
  • yes. i did all the things.. Commented Feb 5, 2013 at 7:09
  • can you try it in hook_enable. Commented Feb 5, 2013 at 7:18
  • you suggestion for hook_enable() worked...Cheers..Update the answer.. Commented Feb 5, 2013 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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