Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PlaceholdersBehavior to extract placeholders from translated fields #104

Merged
merged 8 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
"php": ">=8.1",
"ext-intl": "*",
"ext-json": "*",
"bedita/core": "^5.0.0",
"bedita/core": "^5.14.0",
"bedita/i18n": "^4.0",
"cakephp/cakephp": "^4.0",
"cakephp/authentication": "^2.0",
"cakephp/twig-view": "^1.3",
"chialab/ip": "^1.0"
},
"require-dev": {
"bedita/placeholders": "^2.0",
"cakephp/cakephp-codesniffer": "^5.0",
"cakephp/plugin-installer": "^1.0",
"phpunit/phpunit": "~8.5.0 || ^9.3"
},
"suggest": {
"bedita/placeholders": "To manage placeholders in templates",
"chialab/rna-cakephp": "To manage JavaScript and CSS bundles with RNA"
},
"autoload": {
Expand Down
19 changes: 14 additions & 5 deletions src/View/Helper/PlaceholdersHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
namespace Chialab\FrontendKit\View\Helper;

use BEdita\Core\Model\Table\ObjectTypesTable;
use BEdita\Placeholders\Model\Behavior\PlaceholdersBehavior;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\Utility\Hash;
use Cake\View\Helper;
use InvalidArgumentException;
use Iterator;
Expand Down Expand Up @@ -103,15 +103,24 @@ public static function defaultTemplater(EntityInterface $entity, string $field,
}

$deltas = [];
$extracted = PlaceholdersBehavior::extractPlaceholders($entity, [$field]);
$getInfo = function (array $extracted, int $id, string $field): array {
foreach ($extracted as $it) {
if ($it['id'] === $id) {
return $it['params'][$field] ?? [];
}
}

return [];
};

foreach ($placeholders as $placeholder) {
$info = Hash::get($placeholder, ['relation', 'params', $field], []);
$info = $getInfo($extracted, $placeholder['id'], $field);
foreach ($info as $i) {
$offset = $i['offset'];
$delta = array_sum(array_filter(
$deltas,
function (int $pos) use ($offset): bool {
return $pos < $offset;
},
fn (int $pos): bool => $pos < $offset,
ARRAY_FILTER_USE_KEY
));
$length = $i['length'];
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/View/Helper/PlaceholdersHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function setUp(): void
'params' => [
'body' => [[
'offset' => 15,
'length' => 18,
'length' => 25,
]],
],
],
Expand All @@ -88,8 +88,8 @@ public function setUp(): void
'relation' => [
'params' => [
'body' => [[
'offset' => 37,
'length' => 18,
'offset' => 44,
'length' => 25,
'params' => [
'class' => 'test',
],
Expand All @@ -101,7 +101,7 @@ public function setUp(): void
$this->object = new ObjectEntity([
'id' => 1,
'type' => 'objects',
'body' => '<p>Hello World <!-- TEMPLATE1 --></p><!-- TEMPLATE2 -->',
'body' => '<p>Hello World <!-- BE-PLACEHOLDER.2 --></p><!-- BE-PLACEHOLDER.3.eyJjbGFzcyI6InRlc3QifQ== -->',
'placeholder' => [$this->image1, $this->image2],
]);
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public function testGetTemplate()
*/
public function testDefaultTemplater()
{
$contents = $this->Placeholders::defaultTemplater($this->object, 'body', [$this->image1, $this->image2], fn ($entity, $params) => sprintf('%s %s', $entity->title, $params ? json_encode($params) : ''));
$contents = $this->Placeholders::defaultTemplater($this->object, 'body', [$this->image1, $this->image2], fn ($entity, $params) => sprintf('%s %s', $entity->title, $params ? $params : ''));
fquffio marked this conversation as resolved.
Show resolved Hide resolved
$this->assertSame('<p>Hello World image1 </p>image2 {"class":"test"}', $contents);
}

Expand Down
Loading