entity_reference_uuid-8.x-1.x-dev/tests/modules/entity_reference_uuid_test/entity_reference_uuid_test.module
tests/modules/entity_reference_uuid_test/entity_reference_uuid_test.module
<?php
/**
* @file
* Module to provide test views and entity types.
*/
use Drupal\node\NodeInterface;
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function entity_reference_uuid_test_node_presave(NodeInterface $entity) {
if ($entity->bundle() === 'test_nodetype_chemical') {
$constituents = (array) $entity->field_chemical_constituents->getValue();
if (!isset($constituents[0]) || $constituents[0]['target_uuid'] !== $entity->uuid()) {
// This is a dirty hack to force a self-reference to exist as the first
// reference.
$value = [
'target_uuid' => $entity->uuid(),
];
array_unshift($constituents, $value);
$entity->field_chemical_constituents->setValue($constituents);
}
if ($entity->field_chemical_constituents->count() > 1) {
$entity->field_chemical_mixture_flag = TRUE;
}
else {
$entity->field_chemical_mixture_flag = FALSE;
}
}
}
