Skip to content

Commit f70b071

Browse files
committed
refactor(container): Update type hinting and simplify content handling in DotCMSService and DotCMSExtension
- Changed import paths for PageAsset and NavigationItem to reflect updated structure. - Updated generateHtmlBasedOnProperty method to accept Contentlet type, enhancing type safety. - Simplified container content handling in Twig template by directly using container properties.
1 parent 3b16f6c commit f70b071

3 files changed

Lines changed: 21 additions & 33 deletions

File tree

examples/dotcms-symfony/src/Service/DotCMSService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace App\Service;
44

55
use Dotcms\PhpSdk\DotCMSClient;
6-
use Dotcms\PhpSdk\Model\PageAsset;
7-
use Dotcms\PhpSdk\Model\NavigationItem;
6+
use Dotcms\PhpSdk\Model\Page\PageAsset;
7+
use Dotcms\PhpSdk\Model\Content\NavigationItem;
88

99
class DotCMSService
1010
{

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Twig;
44

55
use Dotcms\PhpSdk\Utils\DotCmsHelper;
6+
use Dotcms\PhpSdk\Model\Content\Contentlet;
67
use Twig\Environment;
78
use Twig\Extension\AbstractExtension;
89
use Twig\TwigFunction;
@@ -20,7 +21,6 @@ public function getFunctions(): array
2021
{
2122
return [
2223
new TwigFunction('getGridClass', [$this, 'getGridClass']),
23-
new TwigFunction('getContainersData', [$this, 'getContainersData']),
2424
new TwigFunction('generateHtmlBasedOnProperty', [$this, 'generateHtmlBasedOnProperty'], ['is_safe' => ['html']]),
2525
new TwigFunction('htmlAttr', [$this, 'htmlAttr'], ['is_safe' => ['html']])
2626
];
@@ -40,31 +40,22 @@ public function getGridClass(int $position, string $type = 'start'): string
4040
};
4141
}
4242

43-
public function generateHtmlBasedOnProperty(array $content): string
43+
public function generateHtmlBasedOnProperty(Contentlet $content): string
4444
{
45-
if (!isset($content->contentType)) {
45+
if (empty($content)) {
4646
return '';
4747
}
4848

49-
$twig = $this->twig;
50-
$template = match($content->contentType) {
51-
'Banner' => 'dotcms/content-types/banner.twig',
52-
'Product' => 'dotcms/content-types/product.twig',
53-
'Activity' => 'dotcms/content-types/activity.twig',
54-
default => ''
55-
};
56-
57-
if (empty($template)) {
58-
// Fall back to the SDK simple content HTML renderer if no template is found
59-
return DotCmsHelper::simpleContentHtml($content);
49+
$contentType = $content->contentType;
50+
if ($contentType) {
51+
$template = 'dotcms/content-types/' . strtolower($contentType) . '.twig';
52+
if ($this->twig->getLoader()->exists($template)) {
53+
return $this->twig->render($template, ['content' => $content]);
54+
}
6055
}
6156

62-
try {
63-
return $twig->render($template, ['content' => $content]);
64-
} catch (\Exception $e) {
65-
// Fall back to the SDK simple content HTML renderer if rendering fails
66-
return DotCmsHelper::simpleContentHtml($content);
67-
}
57+
// Fall back to the SDK simple HTML renderer
58+
return DotCmsHelper::simpleContentHtml($content->jsonSerialize());
6859
}
6960

7061
public function getContainersData(array $containers, array $containerRef): array

examples/dotcms-symfony/templates/dotcms/container.twig

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
{% set containerObject = getContainersData(containers, container) %}
2-
{% set containerContent = containers[container.identifier].contentlets['uuid-' ~ container.uuid]|default([]) %}
3-
41
{% set containerAttrs = {
52
'data-dot-object': 'container',
6-
'data-dot-identifier': containerObject.path|default(containerObject.identifier),
7-
'data-dot-accept-types': containerObject.acceptTypes,
8-
'data-max-contentlets': containerObject.maxContentlets,
3+
'data-dot-identifier': container.identifier,
4+
'data-dot-accept-types': container.acceptTypes,
5+
'data-max-contentlets': container.maxContentlets,
96
'data-dot-uuid': container.uuid
107
} %}
118

129
<div {{ htmlAttr(containerAttrs) }}>
13-
{% for content in containerContent %}
10+
{% for content in container.contentlets %}
1411
{% set contentAttrs = {
1512
'data-dot-object': 'contentlet',
1613
'data-dot-identifier': content.identifier,
@@ -19,10 +16,10 @@
1916
'data-dot-inode': content.inode,
2017
'data-dot-type': content.contentType,
2118
'data-dot-container': {
22-
'acceptTypes': containerObject.acceptTypes,
23-
'identifier': containerObject.path|default(containerObject.identifier),
24-
'maxContentlets': containerObject.maxContentlets,
25-
'variantId': containerObject.variantId,
19+
'acceptTypes': container.acceptTypes,
20+
'identifier': container.identifier,
21+
'maxContentlets': container.maxContentlets,
22+
'variantId': container.variantId,
2623
'uuid': container.uuid
2724
}|json_encode
2825
} %}

0 commit comments

Comments
 (0)