I try to get an array of 'names' of elements of a Collection class for a couple of days. I tried all possible ways I used in Shopware 6, but still can't get it at Subscriber:
public function onListingCriteria(ProductListingCriteriaEvent $event): void
{
$event->getCriteria()->addAssociation('properties');
$event->getCriteria()->addAssociation('properties.group');
$criteria = $event->getCriteria();
$filters = $criteria->getExtension('filters');
if (!$filters instanceof FilterCollection) {
return;
}
$propertyFilter = $filters->get('properties');
if (!$propertyFilter instanceof Filter || !\is_array($propertyFilter->getValues())) {
return;
}
$currentPropertyOptions = $propertyFilter->getValues();
$event->getContext()->addExtension('currentPropertyOptionsCriteria', new ArrayEntity($currentPropertyOptions));
$criteria1 = new Criteria();
$criteria1->addAssociation('Tags');
$criteria1->setIds($currentPropertyOption);
$tags = new TagCollection();
$options = $this->optionRepository->search($criteria1, $event->getContext())->getEntities();
foreach ($options as $option) {
$extension = $option->getExtension('myTags');
if (!$extension instanceof TagCollection) {
continue;
}
$tags->merge($extension);
}
$tagnames = $tags->getElements('myTags');
// doesn't work $tagnames = $options->getExtension('myTags')->get('names');
// doesn't work $tagnames = $options->getExtension('myTags')->getValues();
// etc..
// test $event->getContext()->addExtension('myTags1', $tags);
// test $event->getContext()->addExtension('myTagsNames1', new ArrayEntity($tagnames));
Here the myTags1 exctension dump:
Shopware\Core\System\Tag\TagCollection {#8104 ▼
#elements: array:10 [▼
"49aa54d94e394bc2affb5b702d402e42" => Shopware\Core\System\Tag\TagEntity {#12939 ▶}
"551fcda8a25f47c990d7a795ef251508" => Shopware\Core\System\Tag\TagEntity {#12943 ▶}
"8c526404df194a88bf72073fea2fdabd" => Shopware\Core\System\Tag\TagEntity {#12947 ▼
#name: "Name1"
Do someone know, how to get an array of names maybe?
foreach ($options as $option) {
$extension = $option->getExtension('myTags');
if (!$extension instanceof TagCollection) {
continue;
}
$tags->merge($extension);
}
$names = $tags->map(fn (TagEntity $tag) => $tag->getName());