Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/CiviEntityStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\civicrm_entity;

use Drupal\civicrm_entity\Entity\CivicrmEntity;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
Expand Down Expand Up @@ -429,6 +430,34 @@ protected function initFieldValues(ContentEntityInterface $entity, array $values
}
$main_property_name = $definition->getFieldStorageDefinition()->getMainPropertyName();

if ($definition->getFieldStorageDefinition()->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED &&
$definition->getFieldStorageDefinition()->hasCustomStorage() && strpos($definition->getName(), 'custom_') !== FALSE) {
$civicrm_connection_name = drupal_valid_test_ua() ? 'civicrm_test' : 'civicrm';
$civicrm_database_info = Database::getConnectionInfo($civicrm_connection_name);

if (isset($civicrm_database_info['default'])) {
$settings = $definition->getItemDefinition()->getSetting('civicrm_entity_field_metadata');

if (isset($settings['table_name']) && isset($settings['column_name']) && $settings['is_multiple']) {
$connection = Database::getConnection('default', $civicrm_connection_name);
$query = $connection
->select($settings['table_name'], 't')
->fields('t', [$settings['column_name']])
->condition('entity_id', $entity->id(), '=')
->orderBy('id', 'ASC');

$result = $query->execute();

$item_values = [];
foreach ($result as $row) {
$item_values[] = $row->{$settings['column_name']};
}

$items->setValue($item_values);
}
}
}

// Set a default format for text fields.
if ($definition->getType() === 'text_long') {
$filter_format = $civicrm_entity_settings->get('filter_format') ?: filter_fallback_format();
Expand Down
5 changes: 5 additions & 0 deletions src/Entity/FieldDefinitionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public function getBaseFieldDefinition(array $civicrm_field) {
$field->setDefaultValue($field['api.default']);
}

if (isset($civicrm_field['is_multiple']) && $civicrm_field['is_multiple']) {
$field->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$field->setCustomStorage(TRUE);
}

return $field;
}

Expand Down
135 changes: 0 additions & 135 deletions src/Plugin/views/field/CustomEntityField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
use Drupal\views\Plugin\views\field\EntityField;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\Core\Entity\EntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Element;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

Expand Down Expand Up @@ -148,120 +144,6 @@ public function query($use_groupby = FALSE) {
$this->getEntityFieldRenderer()->query($this->query, $this->relationship);
}

/**
* {@inheritdoc}
*/
public function getItems(ResultRow $values) {
$display = [
'type' => $this->options['type'],
'settings' => $this->options['settings'],
'label' => 'hidden',
];

if (($entity = $this->getEntity($values)) && isset($entity->{$this->definition['field_name']})) {
$entity = $this->createEntity($entity);

if (isset($this->aliases['id']) && isset($values->{$this->aliases['id']})) {
$values->delta = $this->getDelta($values->{$this->aliases['id']});
}

$build_list = $entity->{$this->definition['field_name']}->view($display);
}
else {
$build_list = NULL;
}

if (!$build_list) {
return [];
}

if ($this->options['field_api_classes']) {
return [['rendered' => $this->renderer->render($build_list)]];
}

$items = [];
$bubbleable = BubbleableMetadata::createFromRenderArray($build_list);
foreach (Element::children($build_list) as $delta) {
BubbleableMetadata::createFromRenderArray($build_list[$delta])
->merge($bubbleable)
->applyTo($build_list[$delta]);
$items[$delta] = [
'rendered' => $build_list[$delta],
'raw' => $build_list['#items'][$delta],
];
}

return $this->prepareItemsByDelta($items);
}

/**
* {@inheritdoc}
*/
protected function prepareItemsByDelta(array $all_values) {
if ($this->limit_values) {
$row = $this->view->result[$this->view->row_index];

if (!$this->options['group_rows'] && isset($all_values[$row->delta]) && is_numeric($row->delta)) {
return [$all_values[$row->delta]];
}
}

return parent::prepareItemsByDelta($all_values);
}

/**
* Populate the entity from CiviCRM API.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be processed.
*
* @return null|\Drupal\Core\Entity\FieldableEntityInterface
* Returns the processed entity.
*/
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)) {
if (isset($result[0]) && is_array($result[0])) {
$result = reset($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);
}
}
elseif ($this->getFieldDefinition()->getType() == 'boolean') {
// CiviCRM API3 will return no result when quering a custom
// field row that has no values. In this case we want to set
// the field to NULL otherwise it defaults to false.
$processed_entity->{$this->definition['field_name']} = NULL;
}
}
catch (\CiviCRM_API3_Exception $e) {
// Don't do anything.
}

return $processed_entity;
}

/**
* Process each value depending on the set definition type.
*
Expand Down Expand Up @@ -299,23 +181,6 @@ protected function getItemValue($value, FieldDefinitionInterface $definition) {
return $value;
}

/**
* Guess the delta based on the custom values.
*
* @param int $id
* The ID of the custom value.
*
* @return int
* The guessed delta.
*/
protected function getDelta($id) {
if ($this->customValues) {
return array_search($id, array_keys($this->customValues));
}

return 0;
}

/**
* Check if date field should be converted to UTC or not.
*
Expand Down
Loading