diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 98cbd64acb..bbc1b4a434 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -48910,11 +48910,6 @@ parameters: count: 1 path: tests/lib/MVC/Symfony/Templating/RenderLocationStrategyTest.php - - - message: "#^Cannot access offset mixed on Ibexa\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinition\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtensionTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtensionTest\\:\\:getConfigResolverMock\\(\\) has no return type specified\\.$#" count: 1 @@ -48940,11 +48935,6 @@ parameters: count: 1 path: tests/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtensionTest.php - - - message: "#^Parameter \\#1 \\$fieldDefinitions of class Ibexa\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinitionCollection constructor expects iterable, Ibexa\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinition given\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtensionTest.php - - message: "#^Parameter \\#1 \\$repository of class Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Templating\\\\Twig\\\\Extension\\\\ContentExtension constructor expects Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Repository, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" count: 1 diff --git a/src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php b/src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php index 3adab79847..0de15736a9 100644 --- a/src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php +++ b/src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php @@ -100,6 +100,10 @@ public function getFunctions() 'alternative' => 'ibexa_field_is_empty', ] ), + new TwigFunction( + 'ibexa_has_field', + [$this, 'hasField'] + ), new TwigFunction( 'ibexa_field_is_empty', [$this, 'isFieldEmpty'] @@ -243,6 +247,11 @@ public function getTranslatedFieldDefinitionDescription(ValueObject $content, $f throw new InvalidArgumentType('$content', 'Content|ContentInfo', $content); } + public function hasField(Content $content, string $fieldDefIdentifier): bool + { + return $content->getContentType()->hasFieldDefinition($fieldDefIdentifier); + } + /** * Checks if a given field is considered empty. * This method accepts field as Objects or by identifiers. diff --git a/tests/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtensionTest.php b/tests/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtensionTest.php index 9bf3fd69ac..d37ee68e55 100644 --- a/tests/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtensionTest.php +++ b/tests/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtensionTest.php @@ -32,7 +32,7 @@ class ContentExtensionTest extends FileSystemTwigIntegrationTestCase /** @var \Ibexa\Contracts\Core\Repository\ContentTypeService|\PHPUnit\Framework\MockObject\MockObject */ private $fieldHelperMock; - /** @var \Ibexa\Core\Repository\Values\ContentType\FieldDefinition[] */ + /** @var array */ private $fieldDefinitions = []; /** @var int[] */ @@ -115,6 +115,9 @@ protected function getContent(string $contentTypeIdentifier, array $fieldsData, ), ] ), + 'contentType' => new ContentType([ + 'fieldDefinitions' => new FieldDefinitionCollection($this->fieldDefinitions[$contentTypeId] ?? []), + ]), ] ); diff --git a/tests/lib/MVC/Symfony/Templating/Twig/Extension/_fixtures/content_functions/ibexa_has_field.test b/tests/lib/MVC/Symfony/Templating/Twig/Extension/_fixtures/content_functions/ibexa_has_field.test new file mode 100644 index 0000000000..bd9964c30a --- /dev/null +++ b/tests/lib/MVC/Symfony/Templating/Twig/Extension/_fixtures/content_functions/ibexa_has_field.test @@ -0,0 +1,23 @@ +--TEST-- +"ibexa_has_field" function +--TEMPLATE-- +{{ ibexa_has_field(content, 'existing') ? 'YES' : 'NO' }} +{{ ibexa_has_field(content, 'non-existing') ? 'YES' : 'NO' }} + +--DATA-- +return [ + 'content' => $this->getContent( + 'test_content', + [ + 'ezstring' => [ + 'id' => 125, + 'fieldDefIdentifier' => 'existing', + 'value' => 'value', + 'languageCode' => 'eng-GB', + ], + ] + ), +]; +--EXPECT-- +YES +NO