Commit 1c7bca0b authored by webchick's avatar webchick

#605442 by catch: Add a generic hook_entity_load().

parent 92f5a844
......@@ -223,8 +223,9 @@ protected function buildQuery() {
/**
* Attach data to entities upon loading.
*
* This will attach fields, if the entity is fieldable. It also calls
* hook_TYPE_load() on the loaded entities. For example
* This will attach fields, if the entity is fieldable. It calls
* hook_entity_load() for modules which need to add data to all entities.
* It also calls hook_TYPE_load() on the loaded entities. For example
* hook_node_load() or hook_user_load(). If your hook_TYPE_load()
* expects special parameters apart from the queried entities, you can set
* $this->hookLoadArguments prior to calling the method.
......@@ -241,6 +242,11 @@ protected function attachLoad(&$queried_entities) {
}
}
// Call hook_entity_load().
foreach (module_implements('entity_load') as $module) {
$function = $module . '_entity_load';
$function($queried_entities, $this->entityType);
}
// Call hook_TYPE_load(). The first argument for hook_TYPE_load() are
// always the queried entities, followed by additional arguments set in
// $this->hookLoadArguments.
......
......@@ -1373,7 +1373,7 @@ class FieldFormTestCase extends FieldTestCase {
preg_match('|test-entity/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
$entity = field_test_entity_load($id);
$entity = field_test_entity_test_load($id);
$this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was saved');
// Display edit form.
......@@ -1386,7 +1386,7 @@ class FieldFormTestCase extends FieldTestCase {
$edit = array("{$this->field_name}[$langcode][0][value]" => $value);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated');
$entity = field_test_entity_load($id);
$entity = field_test_entity_test_load($id);
$this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was updated');
// Empty the field.
......@@ -1394,7 +1394,7 @@ class FieldFormTestCase extends FieldTestCase {
$edit = array("{$this->field_name}[$langcode][0][value]" => $value);
$this->drupalPost('test-entity/' . $id . '/edit', $edit, t('Save'));
$this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated');
$entity = field_test_entity_load($id);
$entity = field_test_entity_test_load($id);
$this->assertIdentical($entity->{$this->field_name}, array(), 'Field was emptied');
}
......@@ -1420,7 +1420,7 @@ class FieldFormTestCase extends FieldTestCase {
preg_match('|test-entity/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
$entity = field_test_entity_load($id);
$entity = field_test_entity_test_load($id);
$this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was saved');
// Edit with missing required value.
......@@ -1500,7 +1500,7 @@ class FieldFormTestCase extends FieldTestCase {
preg_match('|test-entity/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
$entity = field_test_entity_load($id);
$entity = field_test_entity_test_load($id);
ksort($field_values);
$field_values = array_values($field_values);
$this->assertIdentical($entity->{$this->field_name}[$langcode], $field_values, 'Field values were saved in the correct order');
......
......@@ -115,7 +115,7 @@ class TextFieldTestCase extends DrupalWebTestCase {
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
// Display the object.
$entity = field_test_entity_load($id);
$entity = field_test_entity_test_load($id);
$entity->content = field_attach_view($entity_type, $entity);
$this->content = drupal_render($entity->content);
$this->assertText($value, 'Filtered tags are not displayed');
......@@ -179,7 +179,7 @@ class TextFieldTestCase extends DrupalWebTestCase {
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
// Display the object.
$entity = field_test_entity_load($id);
$entity = field_test_entity_test_load($id);
$entity->content = field_attach_view($entity_type, $entity);
$this->content = drupal_render($entity->content);
$this->assertNoRaw($value, t('HTML tags are not displayed.'));
......@@ -212,7 +212,7 @@ class TextFieldTestCase extends DrupalWebTestCase {
$this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), t('Entity was updated'));
// Display the object.
$entity = field_test_entity_load($id);
$entity = field_test_entity_test_load($id);
$entity->content = field_attach_view($entity_type, $entity);
$this->content = drupal_render($entity->content);
$this->assertRaw($value, t('Value is displayed unfiltered'));
......
......@@ -38,7 +38,7 @@ function field_test_menu() {
'type' => MENU_NORMAL_ITEM,
);
}
$items['test-entity/%field_test_entity/edit'] = array(
$items['test-entity/%field_test_entity_test/edit'] = array(
'title' => 'Edit test entity',
'page callback' => 'field_test_entity_edit',
'page arguments' => array(1),
......@@ -193,7 +193,7 @@ function field_test_create_stub_entity($id = 1, $vid = 1, $bundle = FIELD_TEST_B
return $entity;
}
function field_test_entity_load($ftid, $ftvid = NULL) {
function field_test_entity_test_load($ftid, $ftvid = NULL) {
// Load basic strucure.
$query = db_select('test_entity', 'fte', array())
->fields('fte')
......
......@@ -149,6 +149,23 @@ function hook_entity_info_alter(&$entity_info) {
$entity_info['node']['controller class'] = 'MyCustomNodeController';
}
/**
* Act on entities when loaded.
*
* This is a generic load hook called for all entity types loaded via the
* entity API.
*
* @param $entities
* The entities keyed by entity ID.
* @param $type
* The type of entities being loaded (i.e. node, user, comment).
*/
function hook_entity_load($entities, $type) {
foreach ($entities as $entity) {
$entity->foo = mymodule_add_something($entity, $entity_type);
}
}
/**
* Perform periodic actions.
*
......
......@@ -757,7 +757,7 @@ class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase {
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
// Display the object.
$entity = field_test_entity_load($id);
$entity = field_test_entity_test_load($id);
$entities = array($id => $entity);
field_attach_prepare_view($entity_type, $entities, 'full');
$entity->content = field_attach_view($entity_type, $entity);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment