Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add twig function to get assigned element tags #16882

Open
wants to merge 2 commits into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions doc/02_MVC/02_Template/README.md
Expand Up @@ -76,7 +76,7 @@ generates HTML as otherwise the HTML will be escaped by twig.

### Functions

#### Loading Objects
#### Loading elements

The following functions can be used to load Pimcore elements from within a template:

Expand All @@ -87,6 +87,7 @@ The following functions can be used to load Pimcore elements from within a templ
* `pimcore_asset_by_path`
* `pimcore_object`
* `pimcore_object_by_path`
* `pimcore_user`

```twig
{% set myObject = pimcore_object(123) %}
Expand All @@ -103,6 +104,17 @@ For documents, Pimcore also provides a function to handle hardlinks through the
See [PimcoreObjectExtension](https://github.com/pimcore/pimcore/blob/11.x/lib/Twig/Extension/PimcoreObjectExtension.php)
for details.

##### Element helper functions

The following functions can be used to get specific data from Pimcore elements:

| Function | Description |
|---------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|
| `pimcore_object_classificationstore_group(id)` | Get classification store group definition by id. |
| `pimcore_object_classificationstore_get_field_definition_from_json(definition, type)` | Returns the classification store field definition from the given definition JSON, enabling structured access to field properties. |
| `pimcore_object_brick_definition_key(key)` | Get objectbrick definition by id. |
| `pimcore_element_tags(element, asNameList)` | Returns a list of assigned element tags. Use `true` as the second parameter to receive only the tag names. |


#### Subrequests

Expand All @@ -114,7 +126,7 @@ for details.
See [Template Extensions](./02_Template_Extensions/README.md) for details.


#### Templating Extensions
#### Templating extensions

The following extensions can directly be used on Twig. See [Template Extensions](./02_Template_Extensions/README.md) for a
detailed description of every helper:
Expand Down Expand Up @@ -154,7 +166,7 @@ Can be used to test if an object is an instance of a given class.
{% endif %}
```

#### Pimcore Specialities
#### Pimcore specialities

Pimcore provides a few special functionalities to make templates even more powerful.
These are explained in following sub chapters:
Expand Down
18 changes: 18 additions & 0 deletions lib/Twig/Extension/PimcoreObjectExtension.php
Expand Up @@ -20,6 +20,8 @@
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject;
use Pimcore\Model\Document;
use Pimcore\Model\Element\AbstractElement;
use Pimcore\Model\Element\Tag;
use Pimcore\Model\Site;
use Pimcore\Model\User;
use Twig\Extension\AbstractExtension;
Expand Down Expand Up @@ -50,6 +52,7 @@ public function getFunctions(): array
new TwigFunction('pimcore_object_classificationstore_group', [DataObject\Classificationstore\GroupConfig::class, 'getById']),
new TwigFunction('pimcore_object_classificationstore_get_field_definition_from_json', [$this, 'getFieldDefinitionFromJson']),
new TwigFunction('pimcore_object_brick_definition_key', [DataObject\Objectbrick\Definition::class, 'getByKey']),
new TwigFunction('pimcore_element_tags', [$this, 'getPimcoreElementTags']),
];
}

Expand All @@ -61,4 +64,19 @@ public function getFieldDefinitionFromJson(array|string $definition, string $typ

return DataObject\Classificationstore\Service::getFieldDefinitionFromJson($definition, $type);
}

public function getPimcoreElementTags(?AbstractElement $element, bool $asNameList): array
{
if (!$element) {
return [];
}

if ($asNameList) {
return array_map(function ($tag) {
return $tag->getName();
}, Tag::getTagsForElement($element->getType(), $element->getId()));
}

return Tag::getTagsForElement($element->getType(), $element->getId());
}
}