Basic file skeleton + CRUD hooks
Last updated on
14 October 2016
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
Values to replace:
| {ENTITY_ID} | The internal identifier for your entity, e.g. "taxonomy_term". |
| {ENTITY} | The term for a single entity. |
| {ENTITIES} | The term for several entities. |
| {$ENTITY} | A variable for a single entity. |
| {$ENTITIES} | A variable for an array of entities. |
| {ENTITY_CLASS} | The entity class for your entity. |
/**
* @file
* Hooks provided by this module.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Acts on {ENTITIES} being loaded from the database.
*
* This hook is invoked during {ENTITY} loading, which is handled by
* entity_load(), via the EntityCRUDController.
*
* @param array {$ENTITIES}
* An array of {ENTITY} entities being loaded, keyed by id.
*
* @see hook_entity_load()
*/
function hook_{ENTITY_ID}_load(array {$ENTITIES}) {
$result = db_query('SELECT pid, foo FROM {mytable} WHERE pid IN(:ids)', array(':ids' => array_keys({$ENTITIES})));
foreach ($result as $record) {
{$ENTITIES}[$record->pid]->foo = $record->foo;
}
}
/**
* Responds when a {ENTITY} is inserted.
*
* This hook is invoked after the {ENTITY} is inserted into the database.
*
* @param {ENTITY_CLASS} {$ENTITY}
* The {ENTITY} that is being inserted.
*
* @see hook_entity_insert()
*/
function hook_{ENTITY_ID}_insert({ENTITY_CLASS} {$ENTITY}) {
db_insert('mytable')
->fields(array(
'id' => entity_id('{ENTITY_ID}', {$ENTITY}),
'extra' => print_r({$ENTITY}, TRUE),
))
->execute();
}
/**
* Acts on a {ENTITY} being inserted or updated.
*
* This hook is invoked before the {ENTITY} is saved to the database.
*
* @param {ENTITY_CLASS} {$ENTITY}
* The {ENTITY} that is being inserted or updated.
*
* @see hook_entity_presave()
*/
function hook_{ENTITY_ID}_presave({ENTITY_CLASS} {$ENTITY}) {
{$ENTITY}->name = 'foo';
}
/**
* Responds to a {ENTITY} being updated.
*
* This hook is invoked after the {ENTITY} has been updated in the database.
*
* @param {ENTITY_CLASS} {$ENTITY}
* The {ENTITY} that is being updated.
*
* @see hook_entity_update()
*/
function hook_{ENTITY_ID}_update({ENTITY_CLASS} {$ENTITY}) {
db_update('mytable')
->fields(array('extra' => print_r({$ENTITY}, TRUE)))
->condition('id', entity_id('{ENTITY_ID}', {$ENTITY}))
->execute();
}
/**
* Responds to {ENTITY} deletion.
*
* This hook is invoked after the {ENTITY} has been removed from the database.
*
* @param {ENTITY_CLASS} {$ENTITY}
* The {ENTITY} that is being deleted.
*
* @see hook_entity_delete()
*/
function hook_{ENTITY_ID}_delete({ENTITY_CLASS} {$ENTITY}) {
db_delete('mytable')
->condition('pid', entity_id('{ENTITY_ID}', {$ENTITY}))
->execute();
}
/**
* @}
*/
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.