Foren

Getting a parent page url

thumbnail
William Gosse, geändert vor 8 Jahren.

Getting a parent page url

Liferay Master Beiträge: 533 Beitrittsdatum: 04.07.10 Neueste Beiträge
I recently had a need to get a parent page url in order to implement a back button in my theme. The only way I could do it was the enable the servicelocator variable in my portal-ext.properties file by adding the following:
velocity.engine.restricted.classes=
velocity.engine.restricted.variables=

That allowed me to do the following:
in the init_custom.vm file I added:
#set($layoutService = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))

in the portal_normal.vm file I added:
#set($parentLayout = $layoutService.getLayout($layout.getParentPlid()))
#set($parentURL = $parentLayout.getFriendlyURL($theme_display.getLocale()))
#if ($validator.isNotNull($parentURL))
<td valign="middle" width="30px"><a href="$parentURL"><img src="$theme_display.getPathThemeImages()/arrow_left.png" /></a></td>
#end

It works but is there a better or easier way to do this? if not what are the downsides of turning off the velocity engine restrictions?
thumbnail
David H Nebinger, geändert vor 8 Jahren.

RE: Getting a parent page url

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
The restrictions aren't too bad, but they help keep wayward developers/portal admins from accessing something you don't want them to access.

Alternatively you could inject values into the velocity context using a ServicePreAction hook... That way you expose what you want without opening the gates to velocity...
thumbnail
William Gosse, geändert vor 8 Jahren.

RE: Getting a parent page url

Liferay Master Beiträge: 533 Beitrittsdatum: 04.07.10 Neueste Beiträge
David H Nebinger:

Alternatively you could inject values into the velocity context using a ServicePreAction hook... That way you expose what you want without opening the gates to velocity...


Can you give a code example of this?
thumbnail
Jitendra Rajput, geändert vor 8 Jahren.

RE: Getting a parent page url

Liferay Master Beiträge: 875 Beitrittsdatum: 07.01.11 Neueste Beiträge
Service Pre Action Hook you create as mentioned in this blog.

From ServicePreAction hook you can read parent url and set this url in to VM_Variables.

Map vmVariables = (Map)req.getAttribute(WebKeys.VM_VARIABLES);
vmVariables .put("parentUrl", url);

Set this map in request.
thumbnail
David H Nebinger, geändert vor 8 Jahren.

RE: Getting a parent page url

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
It's a simple hook, actually. The blog mentioned by Jitendra has the technical information, but for practicality sake your hook needs two things.

First is a class which extends com.liferay.portal.kernel.events.Action to handle your velocity changes:

	public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {

		Map<string, object> vars = (Map<string, object>) request.getAttribute(WebKeys.VM_VARIABLES);

		if (vars == null) {
			vars = new HashMap<string, object>();
			request.setAttribute(WebKeys.VM_VARIABLES, vars);
		}

		vars.put("myVariable", "myValue");
	}</string,></string,></string,>


You also need a property file for your hook with the entry:

servlet.service.events.pre=com.example.MyServicePreAction


Once this is deployed, you can then access $myVariable in your velocity templates just as you did for $theme_display and the like.