Commit 663c2748 authored by webchick's avatar webchick

Issue #1618136 by fgm, larowlan, aspilicious, fago: Remove from entity hooks.

parent a03df8d1
<?php
use Drupal\entity\EntityInterface;
/**
* @file
* Hooks provided by the Comment module.
......@@ -91,11 +93,13 @@ function hook_comment_view(Drupal\comment\Comment $comment, $view_mode, $langcod
*
* @param $build
* A renderable array representing the comment.
* @param Drupal\comment\Comment $comment
* The comment being rendered.
*
* @see comment_view()
* @see hook_entity_view_alter()
*/
function hook_comment_view_alter(&$build) {
function hook_comment_view_alter(&$build, Drupal\comment\Comment $comment) {
// Check for the existence of a field added by another module.
if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
// Change its weight.
......
......@@ -994,8 +994,7 @@ function comment_view(Comment $comment, Node $node, $view_mode = 'full', $langco
}
// Allow modules to modify the structured comment.
$type = 'comment';
drupal_alter(array('comment_view', 'entity_view'), $build, $type);
drupal_alter(array('comment_view', 'entity_view'), $build, $comment);
return $build;
}
......@@ -1044,7 +1043,7 @@ function comment_build_content(Comment $comment, Node $node, $view_mode = 'full'
// Allow modules to make their own additions to the comment.
module_invoke_all('comment_view', $comment, $view_mode, $langcode);
module_invoke_all('entity_view', $comment, 'comment', $view_mode, $langcode);
module_invoke_all('entity_view', $comment, $view_mode, $langcode);
}
/**
......
......@@ -226,45 +226,39 @@ function hook_entity_info_alter(&$entity_info) {
* This is a generic load hook called for all entity types loaded via the
* entity API.
*
* @param $entities
* @param array $entities
* The entities keyed by entity ID.
* @param $type
* @param string $entity_type
* The type of entities being loaded (i.e. node, user, comment).
*/
function hook_entity_load($entities, $type) {
function hook_entity_load($entities, $entity_type) {
foreach ($entities as $entity) {
$entity->foo = mymodule_add_something($entity, $type);
$entity->foo = mymodule_add_something($entity);
}
}
/**
* Act on an entity before it is about to be created or updated.
*
* @param $entity
* @param Drupal\entity\EntityInterface $entity
* The entity object.
* @param $type
* The type of entity being saved (i.e. node, user, comment).
*/
function hook_entity_presave($entity, $type) {
function hook_entity_presave(Drupal\entity\EntityInterface $entity) {
$entity->changed = REQUEST_TIME;
}
/**
* Act on entities when inserted.
*
* @param $entity
* @param Drupal\entity\EntityInterface $entity
* The entity object.
* @param $type
* The type of entity being inserted (i.e. node, user, comment).
*/
function hook_entity_insert($entity, $type) {
function hook_entity_insert(Drupal\entity\EntityInterface $entity) {
// Insert the new entity into a fictional table of all entities.
$info = entity_get_info($type);
list($id) = entity_extract_ids($type, $entity);
db_insert('example_entity')
->fields(array(
'type' => $type,
'id' => $id,
'type' => $entity->entityType(),
'id' => $entity->id(),
'created' => REQUEST_TIME,
'updated' => REQUEST_TIME,
))
......@@ -274,21 +268,17 @@ function hook_entity_insert($entity, $type) {
/**
* Act on entities when updated.
*
* @param $entity
* @param Drupal\entity\EntityInterface $entity
* The entity object.
* @param $type
* The type of entity being updated (e.g. node, user, comment).
*/
function hook_entity_update($entity, $type) {
function hook_entity_update(Drupal\entity\EntityInterface $entity) {
// Update the entity's entry in a fictional table of all entities.
$info = entity_get_info($type);
list($id) = entity_extract_ids($type, $entity);
db_update('example_entity')
->fields(array(
'updated' => REQUEST_TIME,
))
->condition('type', $type)
->condition('id', $id)
->condition('type', $entity->entityType())
->condition('id', $entity->id())
->execute();
}
......@@ -297,15 +287,14 @@ function hook_entity_update($entity, $type) {
*
* This hook runs after the entity type-specific predelete hook.
*
* @param $entity
* @param Drupal\entity\EntityInterface $entity
* The entity object for the entity that is about to be deleted.
* @param $type
* The type of entity being deleted (e.g. node, user, comment).
*/
function hook_entity_predelete($entity, $type) {
function hook_entity_predelete(Drupal\entity\EntityInterface $entity) {
// Count references to this entity in a custom table before they are removed
// upon entity deletion.
list($id) = entity_extract_ids($type, $entity);
$id = $entity->id();
$type = $entity->entityType();
$count = db_select('example_entity_data')
->condition('type', $type)
->condition('id', $id)
......@@ -327,18 +316,14 @@ function hook_entity_predelete($entity, $type) {
*
* This hook runs after the entity type-specific delete hook.
*
* @param $entity
* @param Drupal\entity\EntityInterface $entity
* The entity object for the entity that has been deleted.
* @param $type
* The type of entity being deleted (i.e. node, user, comment).
*/
function hook_entity_delete($entity, $type) {
function hook_entity_delete(Drupal\entity\EntityInterface $entity) {
// Delete the entity's entry from a fictional table of all entities.
$info = entity_get_info($type);
list($id) = entity_extract_ids($type, $entity);
db_delete('example_entity')
->condition('type', $type)
->condition('id', $id)
->condition('type', $entity->entityType())
->condition('id', $entity->id())
->execute();
}
......@@ -364,17 +349,15 @@ function hook_entity_delete($entity, $type) {
* ($query->pager && $query->count), allowing the driver to return 0 from
* the count query and disable the pager.
*/
function hook_entity_query_alter($query) {
function hook_entity_query_alter(Drupal\entity\EntityFieldQuery $query) {
$query->executeCallback = 'my_module_query_callback';
}
/**
* Act on entities being assembled before rendering.
*
* @param $entity
* @param Drupal\entity\EntityInterface $entity
* The entity object.
* @param $type
* The type of entity being rendered (i.e. node, user, comment).
* @param $view_mode
* The view mode the entity is rendered in.
* @param $langcode
......@@ -389,7 +372,7 @@ function hook_entity_query_alter($query) {
* @see hook_node_view()
* @see hook_user_view()
*/
function hook_entity_view($entity, $type, $view_mode, $langcode) {
function hook_entity_view(Drupal\entity\EntityInterface $entity, $view_mode, $langcode) {
$entity->content['my_additional_field'] = array(
'#markup' => $additional_field,
'#weight' => 10,
......@@ -412,8 +395,8 @@ function hook_entity_view($entity, $type, $view_mode, $langcode) {
*
* @param $build
* A renderable array representing the entity content.
* @param $type
* The type of entity being rendered (i.e. node, user, comment).
* @param Drupal\entity\EntityInterface $entity
* The entity object being rendered.
*
* @see hook_entity_view()
* @see hook_comment_view_alter()
......@@ -421,7 +404,7 @@ function hook_entity_view($entity, $type, $view_mode, $langcode) {
* @see hook_taxonomy_term_view_alter()
* @see hook_user_view_alter()
*/
function hook_entity_view_alter(&$build, $type) {
function hook_entity_view_alter(&$build, Drupal\entity\EntityInterface $entity) {
if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
// Change its weight.
$build['an_additional_field']['#weight'] = -10;
......@@ -438,14 +421,14 @@ function hook_entity_view_alter(&$build, $type) {
* view. Only use this if attaching the data during the entity loading phase
* is not appropriate, for example when attaching other 'entity' style objects.
*
* @param $entities
* @param array $entities
* The entities keyed by entity ID.
* @param $type
* The type of entities being loaded (i.e. node, user, comment).
* @param string $entity_type
* The type of entities being viewed (i.e. node, user, comment).
*/
function hook_entity_prepare_view($entities, $type) {
function hook_entity_prepare_view($entities, $entity_type) {
// Load a specific node into the user object for later theming.
if ($type == 'user') {
if (!empty($entities) && $entity_type == 'user') {
$nodes = mymodule_get_user_nodes(array_keys($entities));
foreach ($entities as $uid => $entity) {
$entity->user_node = $nodes[$uid];
......
......@@ -5,11 +5,13 @@
* Test module for the Entity CRUD API.
*/
use Drupal\entity\EntityInterface;
/**
* Implements hook_entity_presave().
*/
function entity_crud_hook_test_entity_presave($entity, $type) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $type);
function entity_crud_hook_test_entity_presave(EntityInterface $entity) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $entity->entityType());
}
/**
......@@ -57,8 +59,8 @@ function entity_crud_hook_test_user_presave() {
/**
* Implements hook_entity_insert().
*/
function entity_crud_hook_test_entity_insert($entity, $type) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $type);
function entity_crud_hook_test_entity_insert(EntityInterface $entity) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $entity->entityType());
}
/**
......@@ -155,8 +157,8 @@ function entity_crud_hook_test_user_load() {
/**
* Implements hook_entity_update().
*/
function entity_crud_hook_test_entity_update($entity, $type) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $type);
function entity_crud_hook_test_entity_update(EntityInterface $entity) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $entity->entityType());
}
/**
......@@ -204,8 +206,8 @@ function entity_crud_hook_test_user_update() {
/**
* Implements hook_entity_predelete().
*/
function entity_crud_hook_test_entity_predelete($entity, $type) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $type);
function entity_crud_hook_test_entity_predelete(EntityInterface $entity) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $entity->entityType());
}
/**
......@@ -253,8 +255,8 @@ function entity_crud_hook_test_user_predelete() {
/**
* Implements hook_entity_delete().
*/
function entity_crud_hook_test_entity_delete($entity, $type) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $type);
function entity_crud_hook_test_entity_delete(EntityInterface $entity) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $entity->entityType());
}
/**
......
<?php
use Drupal\entity\EntityInterface;
/**
* @file
* Hooks provided by the Node module.
......@@ -841,13 +843,15 @@ function hook_node_view(Drupal\node\Node $node, $view_mode, $langcode) {
*
* @param $build
* A renderable array representing the node content.
* @param Drupal\node\Node $node
* The node being rendered.
*
* @see node_view()
* @see hook_entity_view_alter()
*
* @ingroup node_api_hooks
*/
function hook_node_view_alter(&$build) {
function hook_node_view_alter(&$build, Drupal\node\Node $node) {
if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
// Change its weight.
$build['an_additional_field']['#weight'] = -10;
......
......@@ -1159,7 +1159,7 @@ function node_view(Node $node, $view_mode = 'full', $langcode = NULL) {
// Allow modules to modify the structured node.
$type = 'node';
drupal_alter(array('node_view', 'entity_view'), $build, $type);
drupal_alter(array('node_view', 'entity_view'), $build, $node);
return $build;
}
......@@ -1239,7 +1239,7 @@ function node_build_content(Node $node, $view_mode = 'full', $langcode = NULL) {
// Allow modules to make their own additions to the node.
module_invoke_all('node_view', $node, $view_mode, $langcode);
module_invoke_all('entity_view', $node, 'node', $view_mode, $langcode);
module_invoke_all('entity_view', $node, $view_mode, $langcode);
}
/**
......
<?php
use Drupal\entity\EntityInterface;
/**
* @file
* Hooks provided by the Taxonomy module.
......@@ -237,10 +239,12 @@ function hook_taxonomy_term_delete(Drupal\taxonomy\Term $term) {
*
* @param $build
* A renderable array representing the taxonomy term content.
* @param Drupal\taxonomy\Term $term
* The taxonomy term being rendered.
*
* @see hook_entity_view_alter()
*/
function hook_taxonomy_term_view_alter(&$build) {
function hook_taxonomy_term_view_alter(&$build, Drupal\taxonomy\Term $term) {
if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
// Change its weight.
$build['an_additional_field']['#weight'] = -10;
......
......@@ -612,8 +612,7 @@ function taxonomy_term_view(Term $term, $view_mode = 'full', $langcode = NULL) {
$build['#attached']['css'][] = drupal_get_path('module', 'taxonomy') . '/taxonomy.css';
// Allow modules to modify the structured term.
$type = 'taxonomy_term';
drupal_alter(array('taxonomy_term_view', 'entity_view'), $build, $type);
drupal_alter(array('taxonomy_term_view', 'entity_view'), $build, $term);
return $build;
}
......
<?php
use Drupal\entity\EntityInterface;
/**
* @file
* Hooks provided by the User module.
......@@ -359,11 +361,13 @@ function hook_user_view($account, $view_mode, $langcode) {
*
* @param $build
* A renderable array representing the user.
* @param Drupal\user\User $account
* The user account being rendered.
*
* @see user_view()
* @see hook_entity_view_alter()
*/
function hook_user_view_alter(&$build) {
function hook_user_view_alter(&$build, Drupal\user\User $account) {
// Check for the existence of a field added by another module.
if (isset($build['an_additional_field'])) {
// Change its weight.
......
......@@ -2385,8 +2385,7 @@ function user_view($account, $view_mode = 'full', $langcode = NULL) {
);
// Allow modules to modify the structured user.
$type = 'user';
drupal_alter(array('user_view', 'entity_view'), $build, $type);
drupal_alter(array('user_view', 'entity_view'), $build, $account);
return $build;
}
......@@ -2417,7 +2416,7 @@ function user_build_content($account, $view_mode = 'full', $langcode = NULL) {
// Populate $account->content with a render() array.
module_invoke_all('user_view', $account, $view_mode, $langcode);
module_invoke_all('entity_view', $account, 'user', $view_mode, $langcode);
module_invoke_all('entity_view', $account, $view_mode, $langcode);
}
/**
......
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