Simple GoogleMaps ViewHelper

After we added the events, it is time to add a first ViewHelper. Our locations currently has only a name. This is absolutely not enough for a search on GoogleMaps. So the first thing we have to do is extending the model and the add function for the locations.

Extending the location model

To have a complete address in our location model, we add the properties address, zip and city to our model. After doing this we have to generate the getter and setter methods.

Extending the form to add locations

After we finished extending the model we have to extend the form, so that we can add the new informations.

 

The ViewHelper

After we finished this we should start implementing the ViewHelper. We want to generate a HTML-Tag, so we extend the AbstractTagBasedViewHelper for our own ViewHelper.

For a better explanation I highlighted some lines of the source code below.

Line 22: Here we define the tagName. This is the HTML-Tag we want to use in our ViewHelper

Line 25,26: The initializeArguments function is required to register some arguments we want to use. We don’t have to register the arguments like address or zip here, we only have to register the arguments that are used directly in the tag. For example I registered the argument target, so that we can set the target to open the link in a new window for example.

Line 42: We add the link we want to have in the tag.

Line 43: The content inside the tag, the link name in this case can be set in the template by wrapping the new ViewHelper around the link name. In this case the link name in the template is a children of our ViewHelper and we can render this part by calling the function renderChildren.

Line 45: We have all information that we require for our link. Here we tell the ViewHelper to render the tag and return the rendered result.

 Using the ViewHelper

Now it is time to check if our ViewHelper works and we use the ViewHelper in our showLocation template now.

First thing we have to do is to tell Fluid where it has to look for the new ViewHelper. We add the definition of the namespace in the very first line of our template. We define the namespace, in this case it is called ev, and tell Fluid where the ViewHelpers are located.

In line 12-14 we use the ViewHelper. We call the ViewHelper with the following syntax: namespace:classname. In this case the namespace is ev and the class name is GoogleMapsViewHelper. The ViewHelper-part of the class name gets stripped so we call our ViewHelper with ev:googleMaps.

Then we add the arguments our ViewHelper requires. The first on is the target that we have registered, because it is added directly to the tag. Than we have the address, the zip and the city that we have defined in our model earlier.

The link text, between the opening and closing tags, is the part that is returned by Fluid with the function $this->renderChildren().

If we call the page now, we should have a link that is pointing to Google Maps and that has our address as parameter.