-2

is there some solution how to filter value/values with Symfony Event Dispatcher like wordpress with add_filter that returns filtered value? Maybe store properties in Event and edit them with subscriber?

Ajax
  • 11
  • 3
  • Working both in Symfony and WordPress and having experience with Joomla filtering plugins, I understand your question, and in fact I already answered, but a Symfony dev with less experienced with filters would probably have a hard time understand. You should probably expand the description of what you need to achieve. – Francesco Abeni Nov 09 '20 at 10:41

1 Answers1

0

The solution you proposed is the best I can think of. There is no "native" mechanism intended to post-process a value like in WordPress filters or Joomla plugins.

If you dispatch a custom event and populate with the object you want to "filter" you can easily add a subscriber to do what you mean. As objects are always passed by reference, any alteration to the object will be propagated in the controller.

E.g.

controller:

$product = $this->productRepository->get($someId);
$this->eventDispacther->dispatch(
    BeforeDisplayEvent::class,
    new BeforeDisplayEvent($product)
)

return $this->render('product.html.twig', ['product' => $product]);

subscriber:

public function alterProduct(BeforeDisplayEvent $event)
{
    $product->setSomething($this->yourFilter($product));
}
Francesco Abeni
  • 4,190
  • 1
  • 19
  • 30