Skip to content

Commit

Permalink
[BUGFIX] Parse page links with anchor correctly
Browse files Browse the repository at this point in the history
Resolves: #382
  • Loading branch information
sypets committed Oct 29, 2024
1 parent 456cf65 commit f7a9409
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions Classes/Parser/LinkParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function findLinksForRecord(
}
if ($softReferenceParser->getParserKey() === 'typolink_tag') {
$this->analyzeTypoLinks($parserResult, $results, $htmlParser, $record, $field, $table);
} else {
} else {
$this->analyzeLinks($parserResult, $results, $record, $field, $table);
}
}
Expand Down Expand Up @@ -310,31 +310,61 @@ protected function analyzeLinks(
string $flexformField = '',
string $flexformFieldLabel = ''
): void {
$foundLinks = [];
$key = 0;
foreach ($parserResult->getMatchedElements() as $element) {
$reference = $element['subst'] ?? [];
$type = '';
$idRecord = $record['uid'];
if (empty($reference) || !is_array($reference)) {

// Type of referenced record

if (isset($reference['recordRef']) && strpos($reference['recordRef'], 'pages') !== false) {
$currentR = $reference;
// Contains number of the page
$referencedRecordType = $reference['tokenValue'];
$wasPage = true;
$pageKey = $key;
} elseif (isset($reference['recordRef']) && strpos($reference['recordRef'], 'tt_content') !== false
&& (isset($wasPage) && $wasPage === true)) {
// if type is ce and previous was page, we extend the page link and disregard the content link
//$referencedRecordType = $referencedRecordType . '#c' . $reference['tokenValue'];
$foundLinks[$pageKey]['pageAndAnchor'] .= '#c' . $reference['tokenValue'];
$wasPage = false;
continue;
} else {
$currentR = $reference;
}

if (empty($currentR) || !is_array($currentR)) {
continue;
}
$foundLinks[$key] = [
'substr' => $currentR,
'pageAndAnchor' => $referencedRecordType,
];
}

foreach ($foundLinks as $foundLink) {
$currentR = $foundLink['substr'];
/** @var AbstractLinktype $linktypeObject */
foreach ($this->configuration->getLinktypeObjects() as $key => $linktypeObject) {
$type = $linktypeObject->fetchType($reference, $type, $key);
$type = $linktypeObject->fetchType($currentR, $type, $key);
// Store the type that was found
// This prevents overriding by internal validator
if (!empty($type)) {
$reference['type'] = $type;
$currentR['type'] = $type;
}
}
$key = $table . ':' . $field . ':' . $flexformField . ':' . $idRecord . ':' . $reference['tokenID'];
$results[$type][$key]['substr'] = $reference;
$key = $table . ':' . $field . ':' . $flexformField . ':' . $idRecord . ':' . $currentR['tokenID'];
$results[$type][$key]['substr'] = $currentR;
$results[$type][$key]['row'] = $record;
$results[$type][$key]['table'] = $table;
$results[$type][$key]['field'] = $field;
$results[$type][$key]['flexformField'] = $flexformField;
$results[$type][$key]['flexformFieldLabel'] = $flexformFieldLabel;
$results[$type][$key]['uid'] = $idRecord;
$results[$type][$key]['pageAndAnchor'] = $foundLink['pageAndAnchor'] ?? '';
}
}

Expand Down

0 comments on commit f7a9409

Please sign in to comment.