Skip to content

Commit

Permalink
Added ibexa_has_field Twig function
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Dec 3, 2023
1 parent faa57c2 commit 7966baa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, \Ibexa\Core\Repository\Values\ContentType\FieldDefinition[]> */
private $fieldDefinitions = [];

/** @var int[] */
Expand Down Expand Up @@ -115,6 +115,9 @@ protected function getContent(string $contentTypeIdentifier, array $fieldsData,
),
]
),
'contentType' => new ContentType([
'fieldDefinitions' => new FieldDefinitionCollection($this->fieldDefinitions[$contentTypeId] ?? []),
]),
]
);

Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7966baa

Please sign in to comment.