Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
Shopware 6 How do I correctly add an array as extensions to SearchResult

I have multiple working subscriber classes subscribing to ProductListingResultEvent, that add an array as extension to the result.

The code looks something like this:

$myExampleArray = [
    ['key' => 'foobar', 'val1' => 'bar', 'val2' => 'foobarfoo'],
    ['key' => 'barfoo', 'val1' => 'foo', 'val2' => 'barfoobar']
];
$myStructCollection = new StructCollection();
foreach ($myExampleArray as $myExampleElement)
{
    $myStructCollection->set($myExampleElement['key'], $myExampleElement);
}
$event->getResult()->addExtension('myExampleExtension', $myStructCollection);

Is that the right way to do it? Do I always have to instanciate a new Struct to add data as an extension?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Yes you always have to instantiate a new Struct-Class, as the extension-struct you will add should be independent from the core or other plugins, so they don't interfere with your logic and may override stuff e.g.

Therefore you should always use your vendor prefix in the naming of the extension, to ensure, that there are no naming conflicts with other plugins.

For the case you are describing there is a special ArrayStruct you can use to easier create an struct for an array. However the array struct expects that the input array is indexed by the key, so you may need to change the structure of your array data a bit:

$myExampleArray = [
    'foobar' => ['key' => 'foobar', 'val1' => 'bar', 'val2' => 'foobarfoo'],
    'barfoo' => ['key' => 'barfoo', 'val1' => 'foo', 'val2' => 'barfoobar']
];

$event->getResult()->addExtension('myExampleExtension', new ArrayStruct($myExampleArray);

If it is not that easy to change the structure of your array, your solution is totally fine too, but depending on the size of the array you might want to get rid of the extra loop through it.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!