Using Aspects

Aspects are something I am not really used to. I checked the documentation on flow.typo3.org 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.

FlashMessageAspect

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.