You are here

class ComputedFieldHelpers in Computed Field 3.x

Class ComputedFieldHelpers.

Hierarchy

Expanded class hierarchy of ComputedFieldHelpers

1 string reference to 'ComputedFieldHelpers'
computed_field.services.yml in ./computed_field.services.yml
computed_field.services.yml
1 service uses ComputedFieldHelpers
computed_field.helpers in ./computed_field.services.yml
Drupal\computed_field\ComputedFieldHelpers

File

src/ComputedFieldHelpers.php, line 12

Namespace

Drupal\computed_field
View source
class ComputedFieldHelpers {

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * ComputedFieldHelpers constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   Module handler service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
  }

  /**
   * Fetches this field's compute function name for implementing elsewhere.
   *
   * @param string $field_name
   *   Name of the field we need to calculate the value of.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   Entity containing the field.
   * @param int $delta
   *   Field item delta.
   *
   * @return string
   *   The function name.
   */
  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;
  }

  /**
   * Fetches this field's compute function name for implementing elsewhere.
   *
   * @param string $field_name
   *   Current field name.
   *
   * @return string
   *   The function name.
   */
  public function getComputeFunctionName($field_name) {
    return 'computed_field_' . $field_name . '_compute';
  }

  /**
   * Determines if a compute function exists for this field.
   *
   * @param string $field_name
   *   Current field name.
   *
   * @return bool
   *   TRUE if the function exists, FALSE if not.
   */
  public function computeFunctionNameExists($field_name) {
    return function_exists($this
      ->getComputeFunctionName($field_name));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ComputedFieldHelpers::$entityTypeManager protected property Entity type manager.
ComputedFieldHelpers::$moduleHandler protected property Module handler.
ComputedFieldHelpers::computeFunctionNameExists public function Determines if a compute function exists for this field.
ComputedFieldHelpers::executeCode public function Fetches this field's compute function name for implementing elsewhere.
ComputedFieldHelpers::getComputeFunctionName public function Fetches this field's compute function name for implementing elsewhere.
ComputedFieldHelpers::__construct public function ComputedFieldHelpers constructor.