PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 21, 2022

[FIXED] Symfony 4 Event Dispatcher - is there some solution how to filter value with Event Dispatcher? (like wordpress "add_filter" function)

 January 21, 2022     events, symfony, symfony-eventdispatcher, wordpress     No comments   

Issue

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?


Solution

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));
}


Answered By - Francesco Abeni
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing