You are here

public function ComputedFieldHelpers::executeCode in Computed Field 3.x

Fetches this field's compute function name for implementing elsewhere.

Parameters

string $field_name: Name of the field we need to calculate the value of.

\Drupal\Core\Entity\EntityInterface $entity: Entity containing the field.

int $delta: Field item delta.

Return value

string The function name.

File

src/ComputedFieldHelpers.php, line 54

Class

ComputedFieldHelpers
Class ComputedFieldHelpers.

Namespace

Drupal\computed_field

Code

public function executeCode($field_name, EntityInterface $entity, $delta) {
  $fields = $entity
    ->toArray();
  $value = '';
  if ($this
    ->computeFunctionNameExists($field_name)) {
    $compute_function = $this
      ->getComputeFunctionName($field_name);
    $value = $compute_function($this->entityTypeManager, $entity, $fields, $delta);
  }

  // Let other modules alter the values.
  $context = [
    'field_name' => $field_name,
    'entity' => $entity,
  ];
  $this->moduleHandler
    ->alter('computed_field_value', $value, $context);
  $this->moduleHandler
    ->alter('computed_field_' . $field_name . '_value', $value, $context);
  return $value;
}