Calling a controller function from a block view

Permalink 2 users found helpful
Same idea as using $this->action('my_function_name') in a view to access a controller method, but I want to do it in a block.

I'm trying to have my block view print a link which, when clicked, will call some method in its controller. Drawing blanks. Any help would be appreciated

View Replies:
Cravener replied on at Permalink Reply

Any link need to reload page on click - default behavior. If you want to run function without page reloading you need to call function as AJAX.

Try to use jQuery (built-in concrete5).

Some example:
<a href="#" onclick="
url: '<?php echo str_replace('&amp;', '&', $this->action('my_action')); ?>',
data: {variable: value},
success: function(data) {
error: function(req, textStatus) {
">Click Me</a>

success - function after completing request, data - returned data after processing request (controller returns some data after processing action)
focus43 replied on at Permalink Reply
Thanks for the quick response. Whether the page refreshes or not isn't actually a concern.

I simply need to know how to create a button/link in a block's view that, when clicked, will call a function in that block's controller. I've used $this->action(), and it doesn't seem to work.

Is there a function like $this->blockAction() or something like that?
Mnkras replied on at Permalink Reply
heres how,

$file = $controller->getFileObject();

getFileObject is a function in the controller

hope this is what ya needed

ScottC replied on at Permalink Reply
try doing the following. You seem like you know what you are doing :)

print_r($controller in your block view)bingo if that works

print_r($this->controller); more likely

if that fails, do in your block $this->set('blockController',$this);

you may need to play around with putting that in your block's view method or your on_page_view method(not sure which has the scope and is applicable off of the top of my head.

then if you print_r($blockController) from your view you should have access to the block controller object.

Hope that helps.
ScottC replied on at Permalink Reply
simply use

$arrayEtc = $this->controller->method(); from a block view.
jordanlev replied on at Permalink Reply
What happens exactly when you put $this->action('some_name') in your view file? Do you get a php error, or does it output a link that just doesn't go anywhere?

And are you putting this link in the front-end of a block (view.php), or in the edit interface of the block (add.php or edit.php)? Sometimes you need to prepend the controller function name with "action_", and sometimes you don't (can't remember which one that's for now, but I think add/edit doesn't have "action_" prepended and view does).

Another issue is if you're hard-coding this block into the template itself (as opposed to including it via the normal "Edit Page" admin interface), those $this->action() links won't work at all.

Hope that helps.

agedman replied on at Permalink Reply
@Jordan: I just tried this in a sandbox site. I added a public function called testSet() in my block controller like this:
public function testSet() {
      Log::addEntry("Doing something...");
      $this->set("something", "A value!!!");
and stuck this in the view:
<div><?=$something?></div><a href="<?=$this->action('testSet')?>"><?=t('Click here to set a variable >')?></a>
$this->action() does create a link in the view that looks like this:
<a href="/NewCadbury/index.php?cID=1&amp;bID=198&amp;arHandle=Main&amp;ccm_token=1280079726:657a06c2ff416e0c8aed9c489a9da605&amp;btask=passthru&amp;method=testSet">Click here to set a variable &gt;</a>
When I click the link, I can see the log entry, so the function is getting called. The page gets re-loaded but the set() variable $something doesn't show up
CORRECTION: I was confused -- the log entry didn't show up after all. I tried prepending the action name with 'action' like this
<a href="<?=$this->action('action_testSet')?>"><?=t('Click here to set a variable >')?></a>
but still not seeing the log message.

Interesting question, jonohartmann...
ScottC replied on at Permalink Reply
i have a payment block for, what I gleaned from this thing(via trial and error) is that this action
action="<?=$this->action('form_process_payment'); ?>"

function action_form_process_payment(){ //runs and processes a 1 time payment

Haven't really looked at or into why it is like that, just figured it out and moved on.

Let me know if you care to trace out why it has that behavior(assuming the action method on a block view always prepends action_ to any method name )
jordanlev replied on at Permalink Reply
I just looked into some of my code (that I know works) -- so in both a block and a single_page, the call to $this->action('some_stuff') is the same, but the controller function that RESPONDS to that action is different:

* In a single_page, the responding function name is the same as the string you pass to $this->action() -- so in this example it would be:
public function some_stuff() { ... }

* In a block's view.php file (not add/edit), the responding function name is "action_" concatenated with the string you pass to $this->action() -- so in this example it would be:
public function action_some_stuff { ... }
agedman replied on at Permalink Reply
Great! Works fine in the sandbox now. Thanks Scott and Jordan!
tash replied on at Permalink Reply
So, correct me if I'm wrong.

If I have a block and within view.php I have a form and set the action like this:

action="<?php echo $this->action('create_reminder')?>"

and in my controller.php of the block I create a function like this:
public function action_create_reminder() { ..... }

... then when I submit my form, the form should look within controller.php for a function called action_create_reminder and execute the code within? If that is the case, that isn't happening for me. When I do that (I am doing it via ajax) the returned data in an alert is the HTML of the entire c5 page. It's like it isn't finding the correct action file/function or something.
ScottC replied on at Permalink Reply
tash replied on at Permalink Reply
Thanks, I got it to work.

action="<?php echo $this->action('create_reminder');?>"

and in my controller.php:
public function action_create_reminder(){blahblahblah}

Think I was missing the ';' character. For some reason, my xhrPost returns the entire page in "data". Meanin, in my xhrPost's function that returns the data, I have an alert: alert(data)
and that usually returns something like "You have scheduled your reminder" from an echo in the action file, but now it is returning the entire page. Annoying. For now I just set the alert to manually say something, but I'd rather use the alert(data) if possible.
Vahan replied on at Permalink Reply
you must quit execution of the script to prevent rendering the entire page in the action. something like this:

public function action_create_reminder(){