You are here

protected function CustomEntityField::createEntity in CiviCRM Entity 8.3

Populate the entity from CiviCRM API.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to be processed.

Return value

null|\Drupal\Core\Entity\FieldableEntityInterface Returns the processed entity.

1 call to CustomEntityField::createEntity()
CustomEntityField::getItems in src/Plugin/views/field/CustomEntityField.php
Gets an array of items for the field.

File

src/Plugin/views/field/CustomEntityField.php, line 233

Class

CustomEntityField
A field that displays entity field data for custom fields.

Namespace

Drupal\civicrm_entity\Plugin\views\field

Code

protected function createEntity(EntityInterface $entity) {
  $processed_entity = clone $entity;
  try {
    $result = $this->civicrmApi
      ->get('CustomValue', [
      'sequential' => 1,
      'return' => [
        $this->definition['field_name'],
      ],
      'entity_id' => $entity
        ->id(),
      'entity_table' => $entity
        ->getEntityTypeId(),
    ]);
    if (!empty($result)) {
      $result = reset($result);
      $result = array_filter($result, function ($key) {
        return is_int($key);
      }, ARRAY_FILTER_USE_KEY);
      if (!empty($result)) {
        $this->customValues = $result;
        $field_definition = $this
          ->getFieldDefinition();
        $processed_entity->{$this->definition['field_name']} = array_map(function ($value) use ($field_definition) {
          return $this
            ->getItemValue($value, $field_definition);
        }, $result);
      }
    }
  } catch (\CiviCRM_API3_Exception $e) {

    // Don't do anything.
  }
  return $processed_entity;
}