You are here

function _realistic_dummy_content_api_entity_presave in Realistic Dummy Content 7

Generic function called by various hooks in Drupal.

hook_entity_insert(), hook_user_insert() and hook_user_presave() have subtle differences. This function aims to be more abstract and uses the concept of a filter, see below.

Parameters

$entity: The object for a given type, for example this can be a user object or a node object.

$type: The entity type of the information to change, for example 'user' or 'node'.

$filter: If set, only certain fields will be considered when manipulating the object. This can be useful, for example for users, because two separate manipulations need to be performed, depending on whether hook_user_insert() or hook_user_presave(). Both hooks need to modify only certain properties and fields, but taken together the entire object can be manipulated. The filter is an associative array which can contain no key (all fields and properties should be manipulated), the include key (fields included are the only ones to be manipulated, or the exclude key (all fields except those included are the ones to be manipulated).

realistic_dummy_content_api_user_insert() defines the array ('exclude' => array(picture)) whereas realistic_dummy_content_api_user_presave() defines the array ('include' => array(picture)). Therefore taken together these two hooks manipulate the entire user object, but in two phases.

This allows hook implementations to return a different class based on the type of filter.

3 calls to _realistic_dummy_content_api_entity_presave()
realistic_dummy_content_api_entity_presave in api/realistic_dummy_content_api.module
Implements hook_entity_insert().
realistic_dummy_content_api_user_insert in api/realistic_dummy_content_api.module
Implements hook_user_insert().
realistic_dummy_content_api_user_presave in api/realistic_dummy_content_api.module
Implements hook_user_presave().

File

api/realistic_dummy_content_api.module, line 154
API code allowing other modules to generate realistic dummy content. See the Realistic Dummy Content module for an example of how to use.

Code

function _realistic_dummy_content_api_entity_presave($entity, $type, $filter = array()) {
  try {
    if (realistic_dummy_content_api_is_dummy($entity, $type)) {
      $candidate = $entity;
      realistic_dummy_content_api_improve_dummy_content($candidate, $type, $filter);
      realistic_dummy_content_api_validate($candidate, $type);

      //$entity = $candidate;
    }
  } catch (Exception $e) {
    drupal_set_message(t('realistic_dummy_content_api failed to modify dummy objects: message: @m', array(
      '@m' => $e
        ->getMessage(),
    )), 'error', FALSE);
  }
}