Skip to content

Commit

Permalink
[!!!][TASK] Replace custom TranslateViewHelper with LanguagePathViewH…
Browse files Browse the repository at this point in the history
…elper
  • Loading branch information
nhovratov committed Aug 16, 2024
1 parent a3befa5 commit ce8d8af
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h3 class="card-title">
<f:variable name="link_title">
<f:if condition="{item.link_title}">
<f:then>{item.link_title}</f:then>
<f:else><cb:translate key="readmore"/></f:else>
<f:else><f:translate key="{cb:languagePath()}:readmore"/></f:else>
</f:if>
</f:variable>
<div class="card-footer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h3 class="card-title">
<f:variable name="link_title">
<f:if condition="{item.link_title}">
<f:then>{item.link_title}</f:then>
<f:else><cb:translate key="readmore"/></f:else>
<f:else><f:translate key="{cb:languagePath()}:readmore"/></f:else>
</f:if>
</f:variable>
<div class="card-footer">
Expand Down
10 changes: 10 additions & 0 deletions Classes/ViewHelpers/AssetPathViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Exception;

/**
* AssetPathViewHelper
*
* ONLY TO BE USED INSIDE CONTENT BLOCKS
*
* Examples
* ========
*
* <f:asset.css identifier="my-css" href="{cb:assetPath()}/style.css" />
*/
class AssetPathViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
Expand Down
60 changes: 60 additions & 0 deletions Classes/ViewHelpers/LanguagePathViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\ContentBlocks\ViewHelpers;

use TYPO3\CMS\ContentBlocks\Registry\ContentBlockRegistry;
use TYPO3\CMS\ContentBlocks\Utility\ContentBlockPathUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Exception;

/**
* LanguagePathViewHelper
*
* ONLY TO BE USED INSIDE CONTENT BLOCKS
*
* Examples
* ========
*
* <f:translate key="{cb:languagePath()}:header" />
*/
class LanguagePathViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;

public function initializeArguments(): void
{
$this->registerArgument('name', 'string', 'The vendor/package of the Content Block.');
}

public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
{
$name = (string)($arguments['name'] ?? $renderingContext->getVariableProvider()->get('data._name'));
if ($name === '') {
throw new Exception(__CLASS__ . ' seemingly called outside Content Blocks context.', 1699271759);
}
$contentBlockRegistry = GeneralUtility::makeInstance(ContentBlockRegistry::class);
if (!$contentBlockRegistry->hasContentBlock($name)) {
throw new Exception('Content block with the name "' . $name . '" is not registered.', 1699272189);
}
$languagePath = 'LLL:' . $contentBlockRegistry->getContentBlockExtPath($name) . '/' . ContentBlockPathUtility::getLanguageFilePath();
return $languagePath;
}
}
111 changes: 0 additions & 111 deletions Classes/ViewHelpers/TranslateViewHelper.php

This file was deleted.

7 changes: 4 additions & 3 deletions Documentation/Templating/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,20 @@ means if you use an asset ViewHelper in a partial, you have to provide
Translation ViewHelper
======================

This ViewHelper looks directly in the `Labels.xlf` file for the given key.
Content Blocks provides a LanguagePathViewHelper to retrieve the LLL-path for
the Labels.xlf file of the current Content Block.

.. code-block:: html

<cb:translate key="my.contentblock.header" />
<f:translate key="{cb:languagePath()}:header"/>

As described above in the asset ViewHelper, the :html:`{data}` variable is
required to resolve the Content Block automatically. You can also set
:html:`name` by hand:

.. code-block:: html

<cb:translate key="my.contentblock.header" name="vendor/name" />
<f:translate key="{cb:languagePath(name: 'vendor/name')}:header"/>

Partials
========
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

<h1>{data.header}</h1>
<p>{data.bodytext}</p>
<span><cb:translate key="title"/></span>
<span><f:translate key="{cb:languagePath()}:title"/></span>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ <h1>{data.header}</h1>
<p>tableName:{data.mainType}</p>
<p>creationDate:{data.systemProperties.createdAt.timestamp}</p>
<p>updateDate:{data.systemProperties.lastUpdatedAt.timestamp}</p>
<span><cb:translate key="title"/></span>
<span><f:translate key="{cb:languagePath()}:title"/></span>
<img src="{f:uri.resource(path: '{cb:assetPath()}/Icon.svg')}" alt="">
<img src="{f:uri.resource(path: '{cb:assetPath()}/Icon.svg', absolute: '1')}" alt="">
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
>

<f:asset.css identifier="content-block-css" href="{cb:assetPath()}/Frontend.css"/>
<f:asset.script identifier="content-block-script" file="{cb:assetPath()}/Frontend.js"/>
<f:asset.script identifier="content-block-script" src="{cb:assetPath()}/Frontend.js"/>

<h1>{data.header}</h1>
<p>{data.bodytext}</p>
<span><cb:translate key="title"/></span>
<span><f:translate key="{cb:languagePath()}:title"/></span>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use TYPO3Fluid\Fluid\Exception;
use TYPO3Fluid\Fluid\View\TemplateView;

final class TranslateViewHelperTest extends FunctionalTestCase
final class LanguagePathViewHelperTest extends FunctionalTestCase
{
protected bool $initializeDatabase = false;

Expand All @@ -38,25 +38,13 @@ final class TranslateViewHelperTest extends FunctionalTestCase
public static function renderReturnsStringDataProvider(): array
{
return [
'fallback to default attribute for not existing label' => [
'<cb:translate name="typo3tests/content-element-b" key="iDoNotExist" default="myDefault" />',
'myDefault',
'name explicitly set' => [
'<cb:languagePath name="typo3tests/content-element-b" />',
'LLL:EXT:test_content_blocks_b/ContentBlocks/ContentElements/content-element-b/Source/Language/Labels.xlf',
],
'fallback to default attribute for static label' => [
'<cb:translate name="typo3tests/content-element-b" key="static label" default="myDefault" />',
'myDefault',
],
'fallback to child for not existing label' => [
'<cb:translate name="typo3tests/content-element-b" key="iDoNotExist">myDefault</cb:translate>',
'myDefault',
],
'fallback to child for static label' => [
'<cb:translate name="typo3tests/content-element-b" key="static label">myDefault</cb:translate>',
'myDefault',
],
'key and name given' => [
'<cb:translate key="title" name="typo3tests/content-element-b" />',
'Content Block title',
'fallback to name from context' => [
'<cb:languagePath />',
'LLL:EXT:test_content_blocks_b/ContentBlocks/ContentElements/content-element-b/Source/Language/Labels.xlf',
],
];
}
Expand All @@ -68,15 +56,17 @@ public function renderReturnsString(string $template, string $expected): void
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');
$context = $this->get(RenderingContextFactory::class)->create();
$context->getTemplatePaths()->setTemplateSource($template);
self::assertSame($expected, (new TemplateView($context))->render());
$view = new TemplateView($context);
$view->assign('data', ['_name' => 'typo3tests/content-element-b']);
self::assertSame($expected, $view->render());
}

#[Test]
public function invalidContentBlockThrowsFluidException(): void
{
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');
$context = $this->get(RenderingContextFactory::class)->create();
$context->getTemplatePaths()->setTemplateSource('<cb:translate key="dummy" name="fizz/buzz" />');
$context->getTemplatePaths()->setTemplateSource('<cb:languagePath name="fizz/buzz" />');

$this->expectException(Exception::class);
$this->expectExceptionCode(1699272189);
Expand All @@ -89,24 +79,11 @@ public function missingNameThrowsFluidException(): void
{
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');
$context = $this->get(RenderingContextFactory::class)->create();
$context->getTemplatePaths()->setTemplateSource('<cb:translate key="dummy" />');
$context->getTemplatePaths()->setTemplateSource('<cb:languagePath />');

$this->expectException(Exception::class);
$this->expectExceptionCode(1699271759);

(new TemplateView($context))->render();
}

#[Test]
public function emptyKeyThrowsFluidException(): void
{
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');
$context = $this->get(RenderingContextFactory::class)->create();
$context->getTemplatePaths()->setTemplateSource('<cb:translate key="" name="typo3tests/content-element-b" />');

$this->expectException(Exception::class);
$this->expectExceptionCode(1699271873);

(new TemplateView($context))->render();
}
}

0 comments on commit ce8d8af

Please sign in to comment.