4

I need to implement a custom module that adds fields and properties to an entity defined in another module. I felt the best way to do this was using hook_schema_alter and hook_update_n to achieve the data structure updates. However, I do not see these new fields on my entity. Is there another step to take to attach these to the entity via code?

11

Using the third-party Entity API module, you can:

  • Implement hook_schema_alter().
  • Use something like db_add_field() to actually make the change to the DB in YOURMODULE_install() or updateN(). hook_schema_alter() does not actually create the field in the database. It just tells other modules at the code level that the field exists in the schema.
  • Implement hook_entity_property_info_alter(). This tells the Entity API module that this field exists as a property of some entity, where it lives in the database table, and how to access it.
    • Clear the cache to have the new properties showing up.

An example property in the $info array is basically the following one:

  $properties['survey_count_to_complete'] = array(
    'label' => t('Number of Surveys to Complete'),
    'type' => 'integer',
    'description' => 'When a student logs in the system derives how many surveys they must complete. We store this value to make business logic easier.',
    // This is the schema field you made.
    'schema field' => 'survey_count_to_complete',
    'getter callback' => 'entity_property_verbatim_get',
    'setter callback' => 'entity_property_verbatim_set',
  );
|improve this answer|||||
  • where to call db_add_field() so that it is called only once? – yeahman Mar 29 '14 at 6:52
  • 1
    in either hook_install() for a module during install. Or, hook_update_n(). You can also check if a db column exists in any script before calling db_add_field with api.drupal.org/api/drupal/includes%21database%21database.inc/… ... so the code doesn't have to run once persay. – tenken Mar 29 '14 at 15:21
  • thx! that was helpful .. i can now add properties to the node entity type :) – yeahman Mar 29 '14 at 20:01
  • hook_schema_alter() DOES create/change the schema in the database. It is a wrong information (maybe it was correct back then, but not anymore). – Елин Й. Jan 29 '16 at 14:53
4

Here is a working example which implements hook_schema_alter:

/*
 * Implements hook_schema_alter().
 */
function mymodule_schema_alter(&$schema) {
  $schema['tablename']['fields']['fieldname'] = array(
    'type' => 'varchar',
    'default' => null
  );
}

/*
 * Implements hook_install().
 */
function mymodule_install() {
  if (!db_field_exists('tablename', 'fieldname')) {
    $schema = drupal_get_schema('tablename');
    db_add_field('tablename', 'status', $schema['fields']['fieldname']);
  }
}

/*
 * Implements hook_uninstall().
 */
function mymodule_uninstall() {
  if (db_field_exists('tablename', 'fieldname')) {
    $schema = drupal_get_schema('tablename');
    db_drop_field('tablename', 'fieldname');
  }
}

Where mymodule is your module name, tablename your table name and fieldname is your field name.

|improve this answer|||||

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.