Skip to content

Commit

Permalink
Added ibexa_field_group_name Twig function
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Dec 3, 2023
1 parent c9dd8a6 commit 77a0417
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/lib/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Ibexa\Contracts\Core\Repository\Values\ValueObject;
use Ibexa\Core\Base\Exceptions\InvalidArgumentType;
use Ibexa\Core\Helper\FieldHelper;
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use Ibexa\Core\Helper\TranslationHelper;
use Psr\Log\LoggerInterface;
use Twig\Extension\AbstractExtension;
Expand All @@ -33,18 +34,22 @@ class ContentExtension extends AbstractExtension
/** @var \Ibexa\Core\Helper\FieldHelper */
protected $fieldHelper;

private FieldsGroupsList $fieldsGroupsList;

/** @var \Psr\Log\LoggerInterface */
protected $logger;

public function __construct(
Repository $repository,
TranslationHelper $translationHelper,
FieldHelper $fieldHelper,
FieldsGroupsList $fieldsGroupsList,
LoggerInterface $logger = null
) {
$this->repository = $repository;
$this->translationHelper = $translationHelper;
$this->fieldHelper = $fieldHelper;
$this->fieldsGroupsList = $fieldsGroupsList;
$this->logger = $logger;
}

Expand Down Expand Up @@ -132,6 +137,10 @@ public function getFunctions()
'ibexa_field_description',
[$this, 'getTranslatedFieldDefinitionDescription']
),
new TwigFunction(
'ibexa_field_group_name',
[$this, 'getFieldGroupName']
),
new TwigFunction(
'ez_content_field_identifier_first_filled_image',
[$this, 'getFirstFilledImageFieldIdentifier'],
Expand Down Expand Up @@ -252,6 +261,11 @@ public function hasField(Content $content, string $fieldDefIdentifier): bool
return $content->getContentType()->hasFieldDefinition($fieldDefIdentifier);
}

public function getFieldGroupName(string $identifier): ?string
{
return $this->fieldsGroupsList->getGroups()[$identifier] ?? null;
}

/**
* 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 @@ -13,6 +13,7 @@
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Core\Helper\FieldHelper;
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use Ibexa\Core\Helper\TranslationHelper;
use Ibexa\Core\MVC\Symfony\Templating\Twig\Extension\ContentExtension;
use Ibexa\Core\Repository\Values\Content\Content;
Expand Down Expand Up @@ -52,7 +53,8 @@ public function getExtensions()
[],
$this->createMock(LoggerInterface::class)
),
$this->fieldHelperMock
$this->fieldHelperMock,
$this->getFieldsGroupsListMock()
),
];
}
Expand Down Expand Up @@ -146,6 +148,16 @@ private function getConfigResolverMock()
return $mock;
}

private function getFieldsGroupsListMock(): FieldsGroupsList
{
$fieldsGroupsList = $this->createMock(FieldsGroupsList::class);
$fieldsGroupsList->method('getGroups')->willReturn([
'content' => 'Content',
]);

return $fieldsGroupsList;
}

protected function getField($isEmpty)
{
$field = new Field(['fieldDefIdentifier' => 'testfield', 'value' => null]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
"ibexa_field_group_name" function
--TEMPLATE--
{{ ibexa_field_group_name('content') }}
--DATA--
return [];
--EXPECT--
Content

0 comments on commit 77a0417

Please sign in to comment.