Using Aspects

Aspects are something I am not really used to. I checked the documentation on and figured out a small example how we can use one for this tutorial.

Something that we use from Flow/Fluid from time to time are the flash messages. The markup for the flash messages in currently more or less hard coded so I had a hard time to wrap a div around the ul-list the flash messages are rendered in. So in this part we want to use aspects to wrap a div around the flash messages, but only if the flash messages have content.


Go ahead and create the folder Aspect below Classes. There we create the class FlashMessageAspect. Don’t forget the @aspect annotation above the class name, otherwise Flow does not now about the aspect.

Now create a public function and call it wrapAdditionalDivAround. The name of the method is not important here you can call it however you want to. The important stuff, like you already noticed during this tutorials, are the annotations above the method.

The following annotations have to be there:

@around: This tells Flow how and when to call the aspect. If you annotate it with around, it is possible that the function we want to use the aspect on is never called so you have to make sure that you call it in you method. There are also aspects like @before and @after where the original function is called after or before the new method.

The parameter for the @around annotation is the method we want to access. In this case we can name the original method. It is also possible to work with wildcards here but you have to be careful because as far as I can tell, if you work to much with aspects you can have a hard time debugging later and if you work with wildcards, it is possible to overwrite more functions than you want to.

The only other parameter here is the joinPoint. This is the connection to the original method.

The method itself is pretty easy. We define what we want to wrap around the output of the method TYPO3\Fluid\ViewHelpers\FlashMessagesViewHelper->render() and then we call this method using the joinPoint. We check the result of the original function and if the result is not empty, we know that there is a flashMessage available. If this is so, we wrap the div around the flashMessage and return the modified content.


2 thoughts on “Using Aspects

  1. Jan

    Hey Thomas,

    by playing around with AOP i stumbled over logging.
    So it seems there is no really tutorial about Logging with the Flow onboard Logger…
    Could you write one about how to log with the Flow Logger?

    That would be awesome!
    Cheers, Jan

    1. Thomas Layh Post author

      Good point. We have done quit some stuff with logging in TYPO3.Flow in the office and I like the idea. Will see if I find some time to collect some stuff and write it down.


Leave a Reply

Your email address will not be published. Required fields are marked *