1

I want to increase the node title length on Drupal 7, and as I guess I need to do just two things (theoretically).

  1. Change the title column in the node table to acept more than 255 characters.
  2. Alter the node creating/editing form to let the title field accept more than 255 characters.

I've done, #2 and trying to get #1 using this code:

/**
 * Implements hook_schema_alter().
 */
function my_module_schema_alter(&$schema){
    if(isset($schema['node'])){
        dsm($schema['node']);
        $schema['node']['fields']['title'] = array(
            'type' => 'varchar',
            'length' => 555,
            'not null' => TRUE,
            'default' => '',
        );
        dsm($schema['node']);
    }
}

If I check dsm() before and after, I can see a change, but I don't see the title column in the node table affected. Am I missing something or using the bad hook?

I also use this code to change the form on creating/editing nodes (this works well):

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id){
    if($form['#id'] == 'norma-node-form'){
        $form['title']['#type'] = 'textarea';
        $form['title']['#maxlength'] = 555;
    }
}
5
  • IMPORTANT I've decided to create a new field and "migrate" the stored information with Views Bulk Operations using the "PHP option". The code was: $entity->field_new_title['und'][0]['value'] = $entity->title; node_save($entity); And then hide the title node generating a new one with auto_nodetitles module.
    – Yaazkal
    Commented Feb 8, 2012 at 20:12
  • For the record, if you are using MySQL 5.0.3 or older, varchar only goes up to 255 maximum.
    – Alex Weber
    Commented Apr 13, 2012 at 13:10
  • Also, since you solved your own question, you should add it as an answer and accept it. :)
    – Alex Weber
    Commented Apr 13, 2012 at 13:11
  • @Alex This question is very specific: "I don't see the title column in the node table affected. Am I missing something or using the bad hook?" The answer to that question is not what the OP wrote in his comment.
    – avpaderno
    Commented Apr 13, 2012 at 13:20
  • @kiamlaluno I meant that the OP stated he found an alternative solution in his comment and was happy with it, so therefore unlikely to check back here and accept any other answers. Also this question is hackish at best, IMO it requires drupal.org/project/bad_judgement to alter the core node schema :)
    – Alex Weber
    Commented Apr 15, 2012 at 3:43

4 Answers 4

2

When you are implementing hook_schema_alter(), the table's schema is not changed.
You are informing the modules calling drupal_get_schema() (which is then called by functions such as drupal_write_record()) what the actual schema is. If you altered a database table without implementing hook_schema_alter(), the functions/modules using drupal_get_schema() would get the schema implemented by the module using that database table, which is not anymore the one describing the database table.

You need to first alter the existing database table using db_change_field(), taking in consideration the following note:

IMPORTANT NOTE: To maintain database portability, you have to explicitly recreate all indices and primary keys that are using the changed field.

4
  • Thanks for your answer. I just created another field and user VBO to migrate the data (that was more easy to me).
    – Yaazkal
    Commented Apr 5, 2012 at 23:49
  • That said, it seems like a (very) poor idea to change core's node table schema. In my opinion the solution the OP came up with was the most elegant.
    – Alex Weber
    Commented Apr 13, 2012 at 13:12
  • I never support altering Drupal schemas, if not when it is strictly necessary. As the OP was wondering why the database were not changed, even if he implemented hook_schema_alter(), this answer replies to that.
    – avpaderno
    Commented Apr 13, 2012 at 13:23
  • Fair enough, I guess since it answers the original question its the best answer. +1 from me.
    – Alex Weber
    Commented Apr 15, 2012 at 3:44
2

There's a module for that: Node Title Length:

This module increases maximum length of node titles to 8000 characters.

0

as I mention it on a comment before, and just because this question must be marked as answered, I then copy it again:

I've decided to create a new field and "migrate" the stored information with Views Bulk Operations using the "PHP option". The code was:

$entity->field_new_title['und'][0]['value'] = $entity->title; node_save($entity);

And then hide the title node generating a new one with auto_nodetitles module.

0

The simplest solution is...

Create a new field (field_full_title) and set the maximum length to whatever value you want, use that to store the full title, optionally truncate this value to store in the original title field.

1
  • Hi, as my answer says it's what I did, just that I had data on that field so ran VBO to "migrate" the data and then remove the old field. (check my self answer).
    – Yaazkal
    Commented Mar 4, 2013 at 0:36

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.