Skip to content

Commit 56cecb4

Browse files
committed
fix: add magic methods to enable Twig property access for Contentlet fields
1 parent 4665d92 commit 56cecb4

5 files changed

Lines changed: 78 additions & 19 deletions

File tree

examples/dotcms-symfony/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
"symfony/runtime": "7.2.*",
3636
"symfony/security-bundle": "7.2.*",
3737
"symfony/serializer": "7.2.*",
38-
"symfony/stimulus-bundle": "^2.25.2",
38+
"symfony/stimulus-bundle": "^2.26.1",
3939
"symfony/string": "7.2.*",
4040
"symfony/translation": "7.2.*",
4141
"symfony/twig-bundle": "7.2.*",
42-
"symfony/ux-turbo": "^2.25.2",
42+
"symfony/ux-turbo": "^2.26.1",
4343
"symfony/validator": "7.2.*",
4444
"symfony/web-link": "7.2.*",
4545
"symfony/yaml": "7.2.*",

examples/dotcms-symfony/composer.lock

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

examples/dotcms-symfony/templates/dotcms/content-types/banner.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<article>
99
{{ imageHtml|default('')|raw }}
1010
{% if content.title %}<h2>{{ content.title }}</h2>{% endif %}
11-
{% if content.caption %}<p>{{ content.caption }}</p>{% endif %}
12-
{% if content.buttonText %}
11+
{% if content.caption|default('') %}<p>{{ content.caption }}</p>{% endif %}
12+
{% if content.buttonText|default('') %}
1313
<a href="{{ content.link|default('#') }}">{{ content.buttonText }}</a>
1414
{% endif %}
1515
</article>

src/Model/Core/AbstractModel.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ protected function has(string $name): bool
5555
return property_exists($this, $name) || isset($this->additionalProperties[$name]);
5656
}
5757

58+
/**
59+
* Magic method to get property values
60+
* This enables property access syntax (e.g., $obj->propertyName) for additional properties
61+
*
62+
* @param string $name Property name
63+
* @return mixed Property value or null if not found
64+
*/
65+
public function __get(string $name): mixed
66+
{
67+
return $this->get($name);
68+
}
69+
70+
/**
71+
* Magic method to check if a property exists
72+
* This enables isset() checks on additional properties
73+
*
74+
* @param string $name Property name
75+
* @return bool True if the property exists
76+
*/
77+
public function __isset(string $name): bool
78+
{
79+
return $this->has($name);
80+
}
81+
5882
/**
5983
* @inheritDoc
6084
*/

tests/Model/AbstractModelTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,40 @@ public function testJsonSerialize(): void
109109
// Test JSON encoding
110110
$this->assertEquals(json_encode($expected), json_encode($model));
111111
}
112+
113+
public function testMagicGetMethod(): void
114+
{
115+
$additionalProps = ['buttonText' => 'Click Me', 'caption' => 'Test Caption'];
116+
$model = $this->createConcreteModel($additionalProps);
117+
118+
// Test accessing existing properties
119+
$this->assertEquals('test-id', $model->identifier);
120+
$this->assertEquals('Test Title', $model->title);
121+
122+
// Test accessing additional properties via magic __get
123+
$this->assertEquals('Click Me', $model->buttonText);
124+
$this->assertEquals('Test Caption', $model->caption);
125+
126+
// Test accessing non-existent property
127+
$this->assertNull($model->nonExistentProp);
128+
}
129+
130+
public function testMagicIssetMethod(): void
131+
{
132+
$additionalProps = ['buttonText' => 'Click Me', 'emptyProp' => ''];
133+
$model = $this->createConcreteModel($additionalProps);
134+
135+
// Test isset on existing properties
136+
$this->assertTrue(isset($model->identifier));
137+
$this->assertTrue(isset($model->title));
138+
139+
// Test isset on additional properties
140+
$this->assertTrue(isset($model->buttonText));
141+
$this->assertTrue(isset($model->emptyProp)); // Even empty string should return true for isset
142+
143+
// Test isset on non-existent property
144+
$this->assertFalse(isset($model->nonExistentProp));
145+
}
112146
}
113147

114148
/**

0 commit comments

Comments
 (0)