kenneth-bolivar-castro / ng2_entity

Drupal Angular 2 Entity module - See https://www.drupal.org/sandbox/keboca/2755391
2 stars 0 forks source link

Ng2EntityViewDisplay::getFieldValue() method - Allow external functions and validate it before execute it #1

Closed kenneth-bolivar-castro closed 8 years ago

kenneth-bolivar-castro commented 8 years ago

Allow external functions to be called when callback function is invoke inside "getFieldValue()" method.

kenneth-bolivar-castro commented 8 years ago

Added "parseAsFirstValueOrCallback" protected method, where a custom function can be call it based on metadata defined into YML definition.

The "getFieldValue()" method changed, it check first a token implementation, then it tries to retrieve value from first value into field if it exists into given entity, and if custom function callback is defined it gets to be invoke it.

As example if we have one custom module name: "utitlity" and one date type field named "field_born_date" into an entity definition, and we want to execute any business logic right after get the raw field value, it can looks like this into YML file definition:

properties:
  - field_born_date:value:utility_custom_logic

Then into our "utility" module we can create that callback function:

/**
 * Callback function to execute after grab first value from field into entity.
 * 
 * @param mixed $value
 *   Given first value from field into entity.
 * @param $entity
 *   Given entity parsed.
 * @param string $field_name
 * 
 * @return mixed
 *   Final value parsed.
 */
function utility_custom_logic($value, $entity, $field_name) {
  // Complex logic to overwrite $value parameter.
  $value = uniqid();
  // Returns new $value.
  return $value;
}