5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityTypeManager;
17 private $entityTypeManager;
22 public function __construct(ConfigFactoryInterface $configService, EntityTypeManager $entityTypeManager) {
23 $this->setConfigService($configService);
24 $this->entityTypeManager = $entityTypeManager;
38 foreach ($this->getPropertyList() as $property_id) {
39 if (isset($data->{$property_id})) {
40 $data->{$property_id} = $this->referenceProperty($property_id, $data->{$property_id});
57 private function referenceProperty(
string $property_id, $data) {
58 if (is_array($data)) {
59 return $this->referenceMultiple($property_id, $data);
63 return $this->referenceSingle($property_id, $data);
78 private function referenceMultiple(
string $property_id, array $values) : array {
80 foreach ($values as $value) {
81 $data = $this->referenceSingle($property_id, $value);
100 private function referenceSingle(
string $property_id, $value) {
101 $uuid = $this->checkExistingReference($property_id, $value);
103 $uuid = $this->createPropertyReference($property_id, $value);
109 if ($this->loggerService) {
110 $this->loggerService->get(
'value_referencer')->error(
111 'Neither found an existing nor could create a new reference for property_id: @property_id with value: @value',
113 '@property_id' => $property_id,
114 '@value' => var_export($value, TRUE),
136 private function checkExistingReference(
string $property_id, $data) {
137 $nodes = $this->entityTypeManager
140 'field_data_type' => $property_id,
141 'title' => md5(json_encode($data)),
144 if ($node = reset($nodes)) {
145 return $node->uuid();
165 private function createPropertyReference(
string $property_id, $value) {
167 $data =
new stdClass();
168 $data->identifier = $this->getUuidService()->generate($property_id, $value);
169 $data->data = $value;
172 $node = $this->entityTypeManager
175 'title' => md5(json_encode($value)),
177 'uuid' => $data->identifier,
178 'field_data_type' => $property_id,
179 'field_json_metadata' => json_encode($data),
183 return $node->uuid();