Commit 9c1d5d39 authored by mkalkbrenner's avatar mkalkbrenner Committed by mkalkbrenner
Browse files

Issue #3274840 by mkalkbrenner: Rewrite internal and entity links

parent 85f2d3ca
......@@ -298,7 +298,7 @@ class Importer {
}
$this->linkManager->setLinkDomain($this->getLinkDomain($file));
$class = $this->entityTypeManager->getDefinition($entity_type)->getClass();
$this->preDenormalize($file, $entity_type);
$needs_second_run = $this->preDenormalize($file, $entity_type);
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $this->serializer->denormalize($file['data'], $class, 'hal_json', ['request_method' => 'POST']);
......@@ -321,7 +321,7 @@ class Importer {
}
}
if (empty($file['references']) || $i === 1) {
if ((!$needs_second_run && empty($file['references'])) || $i === 1) {
// Don't handle entities without references twice. Don't handle
// entities with references again in the third run for path aliases.
unset($files[$uuid]);
......@@ -505,18 +505,19 @@ class Importer {
*
* @param $file
*
* @return $this
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function preDenormalize(&$file, $entity_type) {
$needs_second_run = FALSE;
$this->updateTargetRevisionId($file['data']);
$needs_second_run |= $this->updateInternalLinks($file['data']);
if ($entity_type === 'path_alias') {
$this->updatePathAliasTargetId($file['data']);
}
return $this;
return $needs_second_run;
}
/**
......@@ -630,24 +631,70 @@ class Importer {
}
/**
* Rewrite path aliases to target entity IDs that were assigned during import.
* Rewrite internal links to target entity IDs that were assigned during
* import.
*
* @param $decode
* @param array $decode
* The decoded entity.
*
* @return $this
* @return bool
* TRUE if the entity contains an internal link field.
*/
private function updatePathAliasTargetId(&$decode) {
if ($alias = $decode['path'][0]['value'] ?? NULL) {
if (preg_match('@^/(\w+)/(\d+)([/?#].*|)$@', $alias, $matches)) {
$entity_type_id = str_replace('_', '/', $matches[1]);
if ($uuid = $this->entityLookup[$entity_type_id][$matches[2]] ?? NULL) {
if ($id = $this->entityIdLookup[$uuid] ?? NULL) {
$decode['path'][0]['value'] = '/' . $matches[1] . '/' . $id . $matches[3];
private function updateInternalLinks(array &$decode): bool {
$has_internal_links = FALSE;
foreach ($decode as $field => $items) {
foreach ($items as $index => $item) {
foreach ($item as $name => $value) {
if ('uri' === $name) {
if (str_starts_with($value, 'internal:')) {
$decode[$field][$index][$name] = 'internal:' . $this->getUpdatedInternalPath(str_replace('internal:', '', $value));
$has_internal_links = TRUE;
}
elseif (str_starts_with($value, 'entity:')) {
$decode[$field][$index][$name] = 'entity:' . trim($this->getUpdatedInternalPath(str_replace('entity:', '/', $value), $item['target_uuid'] ?? NULL), '/');
$has_internal_links = TRUE;
}
}
}
}
}
return $this;
return $has_internal_links;
}
/**
* Rewrite path aliases to target entity IDs that were assigned during import.
*
* @param array $decode
* The decoded entity.
*/
private function updatePathAliasTargetId(array &$decode) {
if ($path = $decode['path'][0]['value'] ?? NULL) {
$decode['path'][0]['value'] = $this->getUpdatedInternalPath($path);
}
}
/**
* Rewrite a path to target entity IDs that were assigned during import.
*
* @param string $path
* The path from exported content.
*
* @return string
* The updated path.
*/
private function getUpdatedInternalPath($path, $uuid = NULL): string {
if (preg_match('@^/(\w+)/(\d+)([/?#].*|)$@', $path, $matches)) {
$entity_type_id = str_replace('_', '/', $matches[1]);
if ($uuid || ($uuid = $this->entityLookup[$entity_type_id][$matches[2]] ?? NULL)) {
if ($id = $this->entityIdLookup[$uuid] ?? NULL) {
return '/' . $matches[1] . '/' . $id . $matches[3];
}
}
}
return $path;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment