Skip to content

Commit

Permalink
[BUGFIX] Correct template lookup when page record is new
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Oct 10, 2023
1 parent ae31509 commit 2eecad3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
15 changes: 13 additions & 2 deletions Classes/Provider/PageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,25 @@ public function getForm(array $row, ?string $forField = null): ?Form
return null;
}

$pageTemplateConfiguration = $this->pageService->getPageTemplateConfiguration($row['uid']);

// If field is main field and "this page" has no selection or field is sub field and "subpages" has no selection
if (($forField === self::FIELD_NAME_MAIN && empty($row[self::FIELD_ACTION_MAIN]))
|| ($forField === self::FIELD_NAME_SUB && empty($row[self::FIELD_ACTION_SUB]))
) {
// The page inherits page layout from parent(s). Read the root line for the first page that defines a value
// in the sub-action field, then use that record and resolve the Form used in the sub-configuration field.
// If the row is a new page, use the inherited form from the parent page.
$pageUid = $row['uid'] ?? 0;
$pageUidIsParent = false;
if (is_string($pageUid) && substr($pageUid, 0, 3) === 'NEW') {
$pageUid = $row['pid'] ?? 0;
$pageUidIsParent = true;
}

$pageTemplateConfiguration = $this->pageService->getPageTemplateConfiguration(
(integer) $pageUid,
$pageUidIsParent
);

$form = parent::getForm($pageTemplateConfiguration['record_sub'] ?? $row, self::FIELD_NAME_SUB);
} else {
$form = parent::getForm($row, $forField);
Expand Down
5 changes: 3 additions & 2 deletions Classes/Service/PageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(
*
* @api
*/
public function getPageTemplateConfiguration(int $pageUid): ?array
public function getPageTemplateConfiguration(int $pageUid, bool $pageUidIsParentUid = false): ?array
{
$pageUid = (integer) $pageUid;
if (!$pageUid) {
Expand All @@ -81,12 +81,13 @@ public function getPageTemplateConfiguration(int $pageUid): ?array
// Initialize with possibly-empty values and loop root line
// to fill values as they are detected.
foreach ($rootLine as $page) {
$rootLinePageUid = (integer) ($page['uid'] ?? 0);
$mainFieldValue = $page[PageProvider::FIELD_ACTION_MAIN] ?? null;
$subFieldValue = $page[PageProvider::FIELD_ACTION_SUB] ?? null;
$resolvedMainTemplateIdentity = is_array($mainFieldValue) ? $mainFieldValue[0] : $mainFieldValue;
$resolvedSubTemplateIdentity = is_array($subFieldValue) ? $subFieldValue[0] : $subFieldValue;
$containsSubDefinition = (strpos($subFieldValue ?? '', '->') !== false);
$isCandidate = ((integer) ($page['uid'] ?? 0) !== $pageUid);
$isCandidate = $pageUidIsParentUid ? true : $rootLinePageUid !== $pageUid;
if ($containsSubDefinition && $isCandidate) {
$resolvedSubTemplateIdentity = $subFieldValue;
$recordDefiningSub = $page;
Expand Down
62 changes: 62 additions & 0 deletions Tests/Unit/Provider/PageProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,66 @@ public function testGetControllerActionFromRecordReturnsDefaultIfActionIsEmpty()
$subject->method('getControllerActionReferenceFromRecord')->willReturn('');
self::assertSame('default', $subject->getControllerActionFromRecord(['uid' => 123]));
}

/**
* @dataProvider getFormTestValues
*/
public function testGetForm(array $row, ?string $forField, ?int $expectedLookupPageUid, ?Form $expectedReturn): void
{
$pageService = $this->getMockBuilder(PageService::class)
->onlyMethods(['getPageTemplateConfiguration'])
->disableOriginalConstructor()
->getMock();
$subject = $this->getMockBuilder(PageProvider::class)
->onlyMethods(['extractConfiguration', 'createCustomFormInstance'])
->disableOriginalConstructor()
->getMock();
$this->setInaccessiblePropertyValue($subject, 'pageService', $pageService);

if ($row['deleted'] ?? false) {
$pageService->expects(self::never())->method('getPageTemplateConfiguration');
$subject->expects(self::never())->method('extractConfiguration');
} elseif ($expectedLookupPageUid === null) {
$pageService->expects(self::never())->method('getPageTemplateConfiguration');
$subject->expects(self::once())->method('extractConfiguration')->willReturn($expectedReturn);
} else {
$fromParent = ['record_sub' => ['uid' => $expectedLookupPageUid]];
$pageService->expects(self::once())
->method('getPageTemplateConfiguration')
->with($expectedLookupPageUid)
->willReturn($fromParent);
$subject->expects(self::once())
->method('extractConfiguration')
->with($fromParent['record_sub'])
->willReturn($expectedReturn);
}
$output = $subject->getForm($row, $forField);
self::assertSame($expectedReturn, $output);
}

public function getFormTestValues(): array
{
$dummyForm = Form::create();
return [
'deleted page returns null early' => [['deleted' => 1], null, null, null],
'existing record uses inherited configuration' => [
['uid' => 123],
PageProvider::FIELD_NAME_MAIN,
123,
$dummyForm
],
'existing record and unspecified field does not look up inherited configuration' => [
['uid' => 123],
null,
null,
$dummyForm
],
'new record uses parent page form' => [
['uid' => 'NEW123', 'pid' => 33],
PageProvider::FIELD_NAME_MAIN,
33,
$dummyForm
],
];
}
}

0 comments on commit 2eecad3

Please sign in to comment.