Skip to content

Commit 9271f40

Browse files
authored
Merge pull request #5 from dotCMS/use-object-access
Update the classes to use object access instead of array
2 parents b78e6c0 + 85d36a3 commit 9271f40

81 files changed

Lines changed: 4212 additions & 1216 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 274 additions & 49 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"require": {
66
"php": "^8.2",
77
"guzzlehttp/guzzle": "^7.9",
8+
"guzzlehttp/promises": "^2.2",
89
"monolog/monolog": "^3.8",
910
"respect/validation": "^2.4",
1011
"symfony/property-access": "^7.2",

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/basic.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@
3535

3636
// Get the page
3737
$page = $client->getPage($pageRequest);
38-
38+
39+
$test = $page->template->title;
40+
$test1 = $page->layout['rows'][0];
41+
42+
$test2 = $page->layout->body->rows[0]->columns[0]->containers[0];
43+
3944
// Display some information about the page
4045
echo "Page title: " . $page->page->title . "\n";
4146
echo "Page URL: " . $page->page->pageUrl . "\n";

examples/dotcms-laravel/README.md

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,10 @@ Create helper functions for DotCMS rendering in `app/Helpers/DotCmsHelpers.php`.
153153
namespace App\Helpers;
154154

155155
use Dotcms\PhpSdk\Utils\DotCmsHelper;
156+
use Dotcms\PhpSdk\Model\Content\Contentlet;
156157

157158
class DotCmsHelpers
158159
{
159-
/**
160-
* Get container data from the containers array
161-
*
162-
* @param array $containers
163-
* @param array $container
164-
* @return array|null
165-
*/
166-
public function getContainersData($containers, $container)
167-
{
168-
return DotCmsHelper::getContainerData($containers, $container);
169-
}
170-
171160
/**
172161
* Generate HTML attributes from an associative array
173162
*
@@ -182,17 +171,17 @@ class DotCmsHelpers
182171
/**
183172
* Generate HTML based on contentlet properties
184173
*
185-
* @param array $content
174+
* @param Contentlet $content
186175
* @return string
187176
*/
188-
public function generateHtmlBasedOnProperty($content)
177+
public function generateHtmlBasedOnProperty(Contentlet $content)
189178
{
190179
if (empty($content)) {
191180
return '';
192181
}
193182

194183
// Check if we have a template to render
195-
$contentType = $content['contentType'] ?? '';
184+
$contentType = $content->contentType;
196185
if ($contentType) {
197186
$viewPath = 'dotcms.content-types.' . strtolower($contentType);
198187
if (view()->exists($viewPath)) {
@@ -201,13 +190,12 @@ class DotCmsHelpers
201190
}
202191

203192
// Fall back to the SDK simple HTML renderer
204-
return DotCmsHelper::simpleContentHtml($content);
193+
return DotCmsHelper::simpleContentHtml($content->jsonSerialize());
205194
}
206195
}
207196
```
208197

209-
The DotCMSHelpers class provides three key functions, all leveraging the SDK's `DotCmsHelper` utility class:
210-
- `getContainersData`: Retrieves container content from the DotCMS page structure
198+
The DotCMSHelpers class provides two key functions, all leveraging the SDK's `DotCmsHelper` utility class:
211199
- `htmlAttr`: Safely generates HTML attributes from arrays, handling special cases like boolean attributes
212200
- `generateHtmlBasedOnProperty`: Renders content intelligently by looking for type-specific templates or falling back to the SDK's default renderer
213201

@@ -446,37 +434,31 @@ This page template:
446434

447435
```php
448436
@php
449-
$containerObject = $dotCmsHelpers->getContainersData($containers, $container);
450-
$containerContentKey = 'uuid-' . $container['uuid'];
451-
$containerContent = isset($containerObject['contentlets'][$containerContentKey])
452-
? $containerObject['contentlets'][$containerContentKey]
453-
: [];
454-
455437
$containerAttrs = [
456438
'data-dot-object' => 'container',
457-
'data-dot-identifier' => $container['identifier'] ?? '',
458-
'data-dot-accept-types' => $containerObject['acceptTypes'] ?? '',
459-
'data-max-contentlets' => $containerObject['maxContentlets'] ?? '',
460-
'data-dot-uuid' => $container['uuid'] ?? ''
439+
'data-dot-identifier' => $container->identifier,
440+
'data-dot-accept-types' => $container->acceptTypes,
441+
'data-max-contentlets' => $container->maxContentlets,
442+
'data-dot-uuid' => $container->uuid
461443
];
462444
@endphp
463445

464446
<div {!! $dotCmsHelpers->htmlAttr($containerAttrs) !!}>
465-
@foreach($containerContent as $content)
447+
@foreach($container->contentlets as $content)
466448
@php
467449
$contentAttrs = [
468450
'data-dot-object' => 'contentlet',
469-
'data-dot-identifier' => $content['identifier'] ?? '',
470-
'data-dot-basetype' => $content['baseType'] ?? '',
471-
'data-dot-title' => $content['widgetTitle'] ?? $content['title'] ?? '',
472-
'data-dot-inode' => $content['inode'] ?? '',
473-
'data-dot-type' => $content['contentType'] ?? '',
451+
'data-dot-identifier' => $content->identifier,
452+
'data-dot-basetype' => $content->baseType,
453+
'data-dot-title' => $content->widgetTitle ?? $content->title,
454+
'data-dot-inode' => $content->inode,
455+
'data-dot-type' => $content->contentType,
474456
'data-dot-container' => json_encode([
475-
'acceptTypes' => $containerObject['acceptTypes'] ?? '',
476-
'identifier' => $container['identifier'] ?? '',
477-
'maxContentlets' => $containerObject['maxContentlets'] ?? '',
478-
'variantId' => $containerObject['variantId'] ?? '',
479-
'uuid' => $container['uuid'] ?? ''
457+
'acceptTypes' => $container->acceptTypes,
458+
'identifier' => $container->identifier,
459+
'maxContentlets' => $container->maxContentlets,
460+
'variantId' => $container->variantId,
461+
'uuid' => $container->uuid
480462
])
481463
];
482464
@endphp
@@ -489,7 +471,6 @@ $containerAttrs = [
489471
```
490472

491473
The container template:
492-
- Extracts container data using helper functions
493474
- Sets up data attributes for DotCMS integration and edit-mode functionality
494475
- Iterates through content items in the container
495476
- Adds necessary metadata attributes to each content element

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

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,10 @@
33
namespace App\Helpers;
44

55
use Dotcms\PhpSdk\Utils\DotCmsHelper;
6-
use Illuminate\Support\Facades\Log;
6+
use Dotcms\PhpSdk\Model\Content\Contentlet;
77

88
class DotCmsHelpers
99
{
10-
/**
11-
* Get container data from the containers array
12-
*
13-
* @param array $containers
14-
* @param array $container
15-
* @return array|null
16-
*/
17-
public function getContainersData($containers, $container)
18-
{
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;
35-
}
36-
3710
/**
3811
* Generate HTML attributes from an associative array
3912
*
@@ -48,17 +21,17 @@ public function htmlAttr($attributes)
4821
/**
4922
* Generate HTML based on contentlet properties
5023
*
51-
* @param array $content
24+
* @param Contentlet $content
5225
* @return string
5326
*/
54-
public function generateHtmlBasedOnProperty($content)
27+
public function generateHtmlBasedOnProperty(Contentlet $content)
5528
{
5629
if (empty($content)) {
5730
return '';
5831
}
5932

6033
// Check if we have a template to render
61-
$contentType = $content['contentType'] ?? '';
34+
$contentType = $content->contentType;
6235
if ($contentType) {
6336
$viewPath = 'dotcms.content-types.' . strtolower($contentType);
6437
if (view()->exists($viewPath)) {
@@ -67,6 +40,6 @@ public function generateHtmlBasedOnProperty($content)
6740
}
6841

6942
// Fall back to the SDK simple HTML renderer
70-
return DotCmsHelper::simpleContentHtml($content);
43+
return DotCmsHelper::simpleContentHtml($content->jsonSerialize());
7144
}
7245
}

examples/dotcms-laravel/resources/js/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import './bootstrap';
22

33
// dotUVE is a global variable that is set by the dotCMS UVE JavaScript API
44
if (window.dotUVE) {
5-
window.dotUVE.createUVESubscription('changes', (changes) => {
5+
window.dotUVE.createSubscription('changes', (changes) => {
66
window.location.reload();
77
})
88
} else {

examples/dotcms-laravel/resources/views/dotcms/components/navigation.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<nav>
33
<ul class="flex space-x-4 text-white">
44
@if(isset($navigation) && $navigation->hasChildren())
5-
@foreach($navigation->getChildren() as $item)
5+
@foreach($navigation->children as $item)
66
<li>
77
<a href="{{ $item->href ?? $item['href'] }}" target="{{ $item->target ?? $item['target'] }}">{{ $item->title ?? $item['title'] }}</a>
88
@if($item->hasChildren())
99
<ul class="flex gap-4 mt-2">
10-
@foreach($item->getChildren() as $child)
10+
@foreach($item->children as $child)
1111
<li>
1212
<a href="{{ $child->href ?? $child['href'] }}" target="{{ $child->target ?? $child['target'] }}">{{ $child->title ?? $child['title'] }}</a>
1313
</li>

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
@php
2-
$containerObject = $dotCmsHelpers->getContainersData($containers, $container);
3-
$containerContent = $containerObject['contentlets'] ?? [];
4-
52
$containerAttrs = [
63
'data-dot-object' => 'container',
7-
'data-dot-identifier' => $container['identifier'] ?? '',
8-
'data-dot-accept-types' => $containerObject['acceptTypes'] ?? '',
9-
'data-max-contentlets' => $containerObject['maxContentlets'] ?? '',
10-
'data-dot-uuid' => $container['uuid'] ?? ''
4+
'data-dot-identifier' => $containerRef->identifier ?? '',
5+
'data-dot-accept-types' => $containerRef->acceptTypes ?? '',
6+
'data-max-contentlets' => $containerRef->maxContentlets ?? '',
7+
'data-dot-uuid' => $containerRef->uuid ?? ''
118
];
129
@endphp
1310

1411
<div {!! $dotCmsHelpers->htmlAttr($containerAttrs) !!}>
15-
@foreach($containerContent as $content)
12+
@foreach($containerRef->contentlets as $content)
1613
@php
1714
$contentAttrs = [
1815
'data-dot-object' => 'contentlet',
19-
'data-dot-identifier' => $content['identifier'] ?? '',
20-
'data-dot-basetype' => $content['baseType'] ?? '',
21-
'data-dot-title' => $content['widgetTitle'] ?? $content['title'] ?? '',
22-
'data-dot-inode' => $content['inode'] ?? '',
23-
'data-dot-type' => $content['contentType'] ?? '',
16+
'data-dot-identifier' => $content->identifier ?? '',
17+
'data-dot-basetype' => $content->baseType ?? '',
18+
'data-dot-title' => $content->widgetTitle ?? $content->title ?? '',
19+
'data-dot-inode' => $content->inode ?? '',
20+
'data-dot-type' => $content->contentType ?? '',
2421
'data-dot-container' => json_encode([
25-
'acceptTypes' => $containerObject['acceptTypes'] ?? '',
26-
'identifier' => $container['identifier'] ?? '',
27-
'maxContentlets' => $containerObject['maxContentlets'] ?? '',
28-
'variantId' => $containerObject['variantId'] ?? '',
29-
'uuid' => $container['uuid'] ?? ''
22+
'acceptTypes' => $containerRef->acceptTypes ?? '',
23+
'identifier' => $containerRef->identifier ?? '',
24+
'maxContentlets' => $containerRef->maxContentlets ?? '',
25+
'variantId' => $containerRef->variantId ?? '',
26+
'uuid' => $containerRef->uuid ?? ''
3027
])
3128
];
3229
@endphp

examples/dotcms-laravel/resources/views/page.blade.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
@section('content')
66

77
{{-- Page Content --}}
8-
@if(isset($pageAsset->layout) && isset($pageAsset->layout->body) && isset($pageAsset->layout->body['rows']))
9-
@foreach($pageAsset->layout->body['rows'] as $row)
8+
@if(isset($pageAsset->layout) && isset($pageAsset->layout->body) && isset($pageAsset->layout->body->rows))
9+
@foreach($pageAsset->layout->body->rows as $row)
1010
<div class="container">
11-
<div data-dot-object="row" class="row{{ isset($row['styleClass']) ? ' ' . $row['styleClass'] : '' }}">
12-
@if(isset($row['columns']) && !empty($row['columns']))
13-
@foreach($row['columns'] as $column)
11+
<div data-dot-object="row" class="row{{ isset($row->styleClass) ? ' ' . $row->styleClass : '' }}">
12+
@if(isset($row->columns) && !empty($row->columns))
13+
@foreach($row->columns as $column)
1414
@php
15-
$startClass = 'col-start-' . ($column['leftOffset'] ?? 0);
16-
$endClass = 'col-end-' . (($column['width'] ?? 12) + ($column['leftOffset'] ?? 0));
15+
$startClass = 'col-start-' . ($column->leftOffset ?? 0);
16+
$endClass = 'col-end-' . (($column->width ?? 12) + ($column->leftOffset ?? 0));
1717
@endphp
1818

19-
<div data-dot-object="column" class="{{ $startClass }} {{ $endClass }}{{ isset($column['styleClass']) ? ' ' . $column['styleClass'] : '' }}">
20-
@if(isset($column['containers']) && !empty($column['containers']))
21-
@foreach($column['containers'] as $container)
19+
<div data-dot-object="column" class="{{ $startClass }} {{ $endClass }}{{ isset($column->styleClass) ? ' ' . $column->styleClass : '' }}">
20+
@if(isset($column->containers) && !empty($column->containers))
21+
@foreach($column->containers as $containerRef)
2222
@include('layouts.container', [
23-
'container' => $container,
23+
'containerRef' => $containerRef,
2424
'containers' => $pageAsset->containers ?? []
2525
])
2626
@endforeach

0 commit comments

Comments
 (0)