Skip to content

Commit a913be6

Browse files
authored
Merge pull request #2 from dotCMS/fix-containers-data-attr
Fix containers data attr
2 parents 25f4985 + f6d16a3 commit a913be6

5 files changed

Lines changed: 65 additions & 28 deletions

File tree

examples/dotcms-laravel/app/Helpers/DotCmsHelpers.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Helpers;
44

55
use Dotcms\PhpSdk\Utils\DotCmsHelper;
6+
use Illuminate\Support\Facades\Log;
67

78
class DotCmsHelpers
89
{
@@ -15,7 +16,22 @@ class DotCmsHelpers
1516
*/
1617
public function getContainersData($containers, $container)
1718
{
18-
return DotCmsHelper::getContainerData($containers, $container);
19+
$containerData = DotCmsHelper::getContainerData($containers, $container);
20+
21+
if (!$containerData) {
22+
return [
23+
'contentlets' => [],
24+
'acceptTypes' => '',
25+
'maxContentlets' => 0,
26+
'variantId' => null
27+
];
28+
}
29+
30+
if (empty($containerData['contentlets'])) {
31+
Log::warning("No contentlets found for container: " . ($container['identifier'] ?? 'unknown') . ", uuid: " . ($container['uuid'] ?? 'unknown'));
32+
}
33+
34+
return $containerData;
1935
}
2036

2137
/**

examples/dotcms-laravel/resources/views/layouts/container.blade.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
@php
22
$containerObject = $dotCmsHelpers->getContainersData($containers, $container);
3-
$containerContentKey = 'uuid-' . $container['uuid'];
4-
$containerContent = isset($containerObject['contentlets'][$containerContentKey])
5-
? $containerObject['contentlets'][$containerContentKey]
6-
: [];
3+
$containerContent = $containerObject['contentlets'] ?? [];
74
85
$containerAttrs = [
96
'data-dot-object' => 'container',

examples/dotcms-symfony/src/Twig/DotCMSExtension.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,16 @@ public function generateHtmlBasedOnProperty(array $content): string
6969

7070
public function getContainersData(array $containers, array $containerRef): array
7171
{
72-
// First try to get the container data using the SDK helper
7372
$containerData = DotCmsHelper::getContainerData($containers, $containerRef);
7473

7574
if (!$containerData) {
7675
throw new RuntimeException("Container not found: " . ($containerRef['identifier'] ?? 'unknown'));
7776
}
7877

79-
$identifier = $containerRef['identifier'] ?? throw new RuntimeException("Missing container identifier");
80-
$uuid = $containerRef['uuid'] ?? throw new RuntimeException("Missing container UUID");
81-
82-
$structures = $containerData['containerStructures'] ?? [];
83-
$container = $containerData['container'] ?? [];
84-
85-
$contentlets = $containerData['contentlets']["uuid-$uuid"]
86-
?? $containerData['contentlets']["uuid-dotParser_$uuid"]
87-
?? [];
88-
89-
if (empty($contentlets)) {
90-
error_log("No contentlets found for container: $identifier, uuid: $uuid");
78+
if (empty($containerData['contentlets'])) {
79+
error_log("No contentlets found for container: " . ($containerRef['identifier'] ?? 'unknown') . ", uuid: " . ($containerRef['uuid'] ?? 'unknown'));
9180
}
9281

93-
return [
94-
...$container,
95-
'acceptTypes' => implode(',', array_column($structures, 'contentTypeVar')),
96-
'contentlets' => $contentlets,
97-
'variantId' => $container['parentPermissionable']['variantId'] ?? null
98-
];
82+
return $containerData;
9983
}
10084
}

src/Utils/DotCmsHelper.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static function getContainerData(array $containers, array $container): ?a
2121
}
2222

2323
$identifier = $container['identifier'] ?? null;
24+
$uuid = $container['uuid'] ?? null;
2425

2526
if (! $identifier || ! isset($containers[$identifier])) {
2627
return null;
@@ -30,7 +31,25 @@ public static function getContainerData(array $containers, array $container): ?a
3031
return null;
3132
}
3233

33-
return $containers[$identifier];
34+
$containerData = $containers[$identifier];
35+
$structures = $containerData['containerStructures'] ?? [];
36+
$container = $containerData['container'] ?? [];
37+
38+
$contentlets = [];
39+
if ($uuid !== null && (is_string($uuid) || is_numeric($uuid))) {
40+
$uuidStr = (string) $uuid;
41+
$contentlets = $containerData['contentlets']["uuid-$uuidStr"]
42+
?? $containerData['contentlets']["uuid-dotParser_$uuidStr"]
43+
?? [];
44+
}
45+
46+
return [
47+
...$container,
48+
'acceptTypes' => implode(',', array_column($structures, 'contentTypeVar')),
49+
'contentlets' => $contentlets,
50+
'maxContentlets' => $container['maxContentlets'] ?? 0,
51+
'variantId' => $container['parentPermissionable']['variantId'] ?? null,
52+
];
3453
}
3554

3655
/**

tests/Utils/DotCmsHelperTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,34 @@ class DotCmsHelperTest extends TestCase
1010
public function testGetContainerData(): void
1111
{
1212
$containers = [
13-
'abc123' => ['title' => 'Test Container'],
13+
'abc123' => [
14+
'container' => ['title' => 'Test Container'],
15+
'containerStructures' => [
16+
['contentTypeVar' => 'Banner'],
17+
['contentTypeVar' => 'Widget'],
18+
],
19+
'contentlets' => [
20+
'uuid-123' => [
21+
['title' => 'Test Content'],
22+
],
23+
],
24+
],
1425
'def456' => ['title' => 'Another Container'],
1526
];
1627

17-
$container = ['identifier' => 'abc123'];
28+
// Test with valid container
29+
$container = [
30+
'identifier' => 'abc123',
31+
'uuid' => '123',
32+
];
1833
$result = DotCmsHelper::getContainerData($containers, $container);
19-
$this->assertEquals(['title' => 'Test Container'], $result);
34+
$this->assertEquals([
35+
'title' => 'Test Container',
36+
'acceptTypes' => 'Banner,Widget',
37+
'contentlets' => [['title' => 'Test Content']],
38+
'maxContentlets' => 0,
39+
'variantId' => null,
40+
], $result);
2041

2142
// Test with non-existent identifier
2243
$container = ['identifier' => 'nonexistent'];

0 commit comments

Comments
 (0)