You are here

public function Drupal7::genericEntityPresave in Realistic Dummy Content 8.2

Same name and namespace in other branches
  1. 7.2 api/src/Framework/Drupal7.php \Drupal\realistic_dummy_content_api\Framework\Drupal7::genericEntityPresave()

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

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

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

array $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 Drupal7::genericEntityPresave()
Drupal7::hookEntityPresave in api/src/Framework/Drupal7.php
Drupal7::hookUserInsert in api/src/Framework/Drupal7.php
Drupal7::hookUserPresave in api/src/Framework/Drupal7.php

File

api/src/Framework/Drupal7.php, line 152

Class

Drupal7
Drupal 7-specific code.

Namespace

Drupal\realistic_dummy_content_api\Framework

Code

public function genericEntityPresave($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);
  }
}