2

I want to create a module that on installing and creating a new field on a content type it should alter the schema of another module.

Im trying to use hook_storage_create_field to react on creating an new field but its not running.

function mymodule_field_storage_create_field($field) {
  exit(var_dump($schema));
  $schema = _field_sql_storage_schema($field);
}

Im I misusing the hook? or there is something faulty in my module?

Update

What Im trying to do is to create a new module that extends the Link module to work like Facebook parser.

So when users add a new link a preview will show a preview like this Redsun parser The functionality is already made and adapted into Drupal, but still having problem extending link schema to have extra fields for images and body fields.

A new widget is added to link field called "link with parser", and it will fire our extra codes to save and validate the link.

  • Can you tell us why you need to change the schema ? – yvan Sep 26 '12 at 13:06
  • @yvan I updated the question. – saadlulu Sep 26 '12 at 13:31
  • Not sure why your hook is not firing, but I would recommend storing the information in your own table rather than altering another module's schema. Some modules do things like that (c.f. uuid), but it can be confusing to folks inspecting the state of the link module after your code has come along and altered the default schema. – greg_1_anderson Sep 26 '12 at 14:28
1

hook_field_storage_create_field and hook_field_create are called when the user creates a new field, i.e. from "Manage Fields", "Add new field". In my tests, hook_field_create was called as expected, but hook_field_storage_create_field was not called. I did not look into why this was, because (a) you could use hook_field_create here just as well as hook_field_storage_create, and (b) this isn't really the approach you should be taking to implement the functionality you need. If you were going to modify the schema for the link field, you would do it once when your module was installed, in hook_install. Your use case is much more complicated than the standard behavior of the link field, though; you would be better off creating your own field type, and do all of your field storage management in one place. You might want to look at the source for existing fields that have complex data storage needs, such as the date field.

0

To alter schema, you can use hook_schema_alter, if it's a new field, you need to create such field in hook_install or hook_update_X (if it's already installed). E.g.

/**
 * Adds new field to the existing user schema.
 *
 * Implements hook_schema_alter().
 */
function foo_user_schema_alter(&$schema) {
  // Add field to existing schema.
  $schema['users']['fields']['migrated'] = array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'Timestamp for when user was migrated.',
  );
}

/**
 * Implements hook_install().
 */
function foo_user_install() {
  $schema = drupal_get_schema('users');
  db_add_field('users', 'migrated', $schema['fields']['migrated']);
}

/**
 * Implements hook_uninstall().
 */
function foo_user_uninstall() {
  db_drop_field('users', 'migrated');
}

/**
 * Adds migrated field for user schema.
 */
function foo_user_update_7101() {
  foo_user_install();
}

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.