Skip to content

Commit

Permalink
Use callback instead of listener method for help icon
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 17, 2020
1 parent 5c72385 commit 444c736
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
Expand Up @@ -58,28 +58,27 @@ public function __construct(ContaoFramework $framework, TranslatorInterface $tra
*/
public function disableRoutingFields(): void
{
/** @var Image $adapter */
$adapter = $this->framework->getAdapter(Image::class);

$renderHelpIcon = function () use ($adapter) {
return $adapter->getHtml(
'show.svg',
'',
sprintf(
'title="%s"',
StringUtil::specialchars($this->translator->trans('tl_page.legacyRouting', [], 'contao_tl_page'))
)
);
};

foreach (['urlPrefix', 'urlSuffix'] as $field) {
$GLOBALS['TL_DCA']['tl_page']['fields'][$field]['eval']['disabled'] = true;
$GLOBALS['TL_DCA']['tl_page']['fields'][$field]['eval']['helpwizard'] = false;
$GLOBALS['TL_DCA']['tl_page']['fields'][$field]['xlabel'][] = [self::class, 'renderHelpIcon'];
$GLOBALS['TL_DCA']['tl_page']['fields'][$field]['xlabel'][] = $renderHelpIcon;
}
}

public function renderHelpIcon(): string
{
/** @var Image $adapter */
$adapter = $this->framework->getAdapter(Image::class);

return $adapter->getHtml(
'show.svg',
'',
sprintf(
'title="%s"',
StringUtil::specialchars($this->translator->trans('tl_page.legacyRouting', [], 'contao_tl_page'))
)
);
}

/**
* @Callback(table="tl_page", target="fields.urlPrefix.load")
*/
Expand Down
Expand Up @@ -34,32 +34,33 @@ public function testDisablesTheRoutingFields(): void
$GLOBALS['TL_DCA']['tl_page']['fields']['urlPrefix']['eval'] = [];
$GLOBALS['TL_DCA']['tl_page']['fields']['urlSuffix']['eval'] = [];

$adapter = $this->mockAdapter(['getHtml']);
$framework = $this->mockContaoFramework([Image::class => $adapter]);

$listener = new LegacyRoutingListener(
$this->mockContaoFramework(),
$framework,
$this->createMock(TranslatorInterface::class)
);

$listener->disableRoutingFields();

$expect = [
'eval' => [
'disabled' => true,
'helpwizard' => false,
],
'xlabel' => [
[LegacyRoutingListener::class, 'renderHelpIcon'],
],
'disabled' => true,
'helpwizard' => false,
];

$this->assertSame($expect, $GLOBALS['TL_DCA']['tl_page']['fields']['urlPrefix']);
$this->assertSame($expect, $GLOBALS['TL_DCA']['tl_page']['fields']['urlSuffix']);
$this->assertSame($expect, $GLOBALS['TL_DCA']['tl_page']['fields']['urlPrefix']['eval']);
$this->assertSame($expect, $GLOBALS['TL_DCA']['tl_page']['fields']['urlSuffix']['eval']);
}

public function testAddsTheRoutingWarning(): void
{
$GLOBALS['TL_DCA']['tl_page']['fields']['urlPrefix']['eval'] = [];
$GLOBALS['TL_DCA']['tl_page']['fields']['urlSuffix']['eval'] = [];

$adapter = $this->mockAdapter(['getHtml']);
$adapter
->expects($this->once())
->expects($this->exactly(2))
->method('getHtml')
->with('show.svg', '', 'title="disabled"')
->willReturn('<img src="show.svg" alt="" title="disabled">')
Expand All @@ -69,14 +70,20 @@ public function testAddsTheRoutingWarning(): void

$translator = $this->createMock(TranslatorInterface::class);
$translator
->expects($this->once())
->expects($this->exactly(2))
->method('trans')
->with('tl_page.legacyRouting', [], 'contao_tl_page')
->willReturn('disabled')
;

$listener = new LegacyRoutingListener($framework, $translator);
$listener->renderHelpIcon();
$listener->disableRoutingFields();

$this->assertInstanceOf(\Closure::class, $GLOBALS['TL_DCA']['tl_page']['fields']['urlPrefix']['xlabel'][0]);
$this->assertInstanceOf(\Closure::class, $GLOBALS['TL_DCA']['tl_page']['fields']['urlSuffix']['xlabel'][0]);

$this->assertSame('<img src="show.svg" alt="" title="disabled">', $GLOBALS['TL_DCA']['tl_page']['fields']['urlPrefix']['xlabel'][0]());
$this->assertSame('<img src="show.svg" alt="" title="disabled">', $GLOBALS['TL_DCA']['tl_page']['fields']['urlSuffix']['xlabel'][0]());
}

public function testOverridesTheUrlPrefixWithPrependLocale(): void
Expand Down

0 comments on commit 444c736

Please sign in to comment.