LinkInternalContentImporter.php in Entity Share 8.3
File
modules/entity_share_client/src/Plugin/EntityShareClient/Processor/LinkInternalContentImporter.php
View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client\Plugin\EntityShareClient\Processor;
use Drupal\entity_share\EntityShareUtility;
use Drupal\entity_share_client\RuntimeImportContext;
class LinkInternalContentImporter extends EntityReference {
public function prepareImportableEntityData(RuntimeImportContext $runtime_import_context, array &$entity_json_data) {
if (isset($entity_json_data['attributes']) && is_array($entity_json_data['attributes'])) {
foreach ($entity_json_data['attributes'] as $field_name => $field_data) {
if (is_array($field_data)) {
if (EntityShareUtility::isNumericArray($field_data)) {
foreach ($field_data as $delta => $value) {
if (isset($value['content_entity_href'])) {
$entity_json_data['attributes'][$field_name][$delta]['uri'] = $this
->processLink($runtime_import_context, $value['uri'], $value['content_entity_href']);
}
}
}
elseif (isset($field_data['content_entity_href'])) {
$entity_json_data['attributes'][$field_name]['uri'] = $this
->processLink($runtime_import_context, $field_data['uri'], $field_data['content_entity_href']);
}
}
}
}
}
protected function processLink(RuntimeImportContext $runtime_import_context, string $uri, string $import_url) {
preg_match("/entity:(.*)\\/(.*)\\/(.*)/", $uri, $parsed_uri);
if (empty($parsed_uri)) {
return $uri;
}
$entity_type = $parsed_uri[1];
$entity_uuid = $parsed_uri[3];
$referenced_entities_ids = $this
->importUrl($runtime_import_context, $import_url);
if (!empty($referenced_entities_ids) && isset($referenced_entities_ids[$entity_uuid])) {
return 'entity:' . $entity_type . '/' . $referenced_entities_ids[$entity_uuid];
}
else {
return $uri;
}
}
}