Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Multiple function calls from links


Go to End


3 Posts   1634 Views

Avatar
LisaB

Community Member, 28 Posts

26 June 2012 at 2:59am

Hi all, In my template, ServiceProductHolder.ss, I have a control loop listing multiple ServiceProducts and within the control block I have:

<p><a href="$saveToMember">Add to My Programme</a></p>

I want saveToMember to run when the link is clicked, but it is obviously running for every ServiceProduct listed when the page loads. This must be a problem with my template syntax - how can I stop this and run it only when the link is clicked?

I have something similar working elsewhere using something like:

<a href="{$Top.Link}functionName">Do function</a>

but because this function is on the page controller it's slightly different.

Any help much appreciated!

Avatar
LisaB

Community Member, 28 Posts

27 June 2012 at 1:36am

Another thought on this. It seems it might not be possible to call my function that is on the data object from a link click because you can't call it via a URL as you could if it were on the page controller. If you put it in the template like I have above it will always run on page load when the template is parsed.

So, I started to look at doing this with a form and now I have a new problem with where to put the code. Because the easiest way to create the relation is in the ServiceProduct data object like this:

$member->ServiceProducts()->add($this);

And because the form needs to be displayed within the control block, I put the form in ServiceProduct.php. However I get method link() doesn't exist on ServiceProduct when trying to visit the holder page.

However, if I put the form in the page controller, I'm not sure how I'd get the current ServiceProduct ID out of the loop to relate it to the member. I'm sure I'm missing something easy here but it's been ages since I looked at any of this stuff.

I'm thinking of going back to a text link and ajax but will have to learn about it first!

Avatar
LisaB

Community Member, 28 Posts

5 July 2012 at 1:58am

For posterity, in case it's ever useful to anyone:

"However, if I put the form in the page controller, I'm not sure how I'd get the current ServiceProduct ID out of the loop to relate it to the member. I'm sure I'm missing something easy here but it's been ages since I looked at any of this stuff."

I was missing something easy. I put everything in the page controller rather than the data object, then from within my loop on the page template where I was controlling the data objects I used <a href="{$Top.Link}functionname/$ID">link</a>. The function gets that DO from the url parameter in order to create or destroy the relationship between it and the logged in member.

Thanks to r3v3rb and other on IRC for pointing me in the right direction.