Hi

I'm using hook_nodeapi to add buttons to a node view:

<?php
function registration_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      if ($node->type == 'event_registration') {
        $node->content['complete_registration'] = array(
          '#type' => 'button',
          '#value' => 'Complete registration',
          '#weight' => 20,
        );
        $node->content['add_another'] = array(
          '#type' => 'submit',
          '#value' => 'Register another entrant',
          '#weight' => 21,
        );
      }
  }
}
?>

How do I get my buttons to actually do something?

Thanks

Glenn

Comments

glennnz’s picture

Bump

Thanks

Glenn

Glenn
THECA Group

glennnz’s picture

Bump.

Is anyone ale to help with this?

Thanks

Glenn

Glenn
THECA Group

ericduran’s picture

I believe you can add a submit callback to your button.
ex.

  $node->content['complete_registration'] = array(
          '#type' => 'button',
          '#value' => 'Complete registration',
          '#weight' => 20
          '#submit' => 'my_special_btn_function';
        );

When that button is press that function will be call.

Personally I would make a form with a submit callback. So I'm not really sure if the submit functions work with just a button. But you can try it.

This should help you.

http://api.drupal.org/api/file/developer/topics/forms_api_reference.html

Eric

glennnz’s picture

Thanks Eric, I'll try that.

I agree, a form would be much easier, but doesn't work for this use case.

Cheers

Glenn

--UPDATE--

Nope, can't get that to work.

Code is:

<?php
function registration_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      if ($node->type == 'tournament_registration' && $node->field_status['0']['value'] != 1) {
        $node->content['complete_registration_button'] = array(
          '#type' => 'button',
          '#name' => 'complete_registration',
          '#value' => 'Complete registration',
          '#weight' => -31,
          '#submit' => 'cart_redirect',
        );
    }
  }
}

function cart_redirect() {
  drupal_goto('cart');
}
?>

Thanks

Glenn

Glenn
THECA Group

colinjones’s picture

Glenn

Did you ever get this working? I am trying to add something myself to trigger a php function, but have no idea on the creation of a module to encapsulate the code, nor what the solution to your issue above might be, so that I can add a new element to the node view that can be pressed to execute the code...

Col.

glennnz’s picture

I never figured this out.

I ended up using a link with an image.,..

Glenn

Glenn
THECA Group

LEternity’s picture

You can add a button to your node view using computed fields. The advantage is that it's fairly uncomplicated and you don't have to include code in your module.

1. Add a computed field field. 2. Add the following into the "Computed Code" field (without the php tags):

<?php

$button = '<form method="link" action="http://www.example.com"><input type="submit" value="Hello World"></form>';
$node_field[0]['value'] = $button;

?>

3. Don't change anything in the "Display Format" field.

Voila, here's your button. You can also call custom functions from your computed field -- the sky is the limit.

jaypan’s picture

You have to call a form from hook_node_api()

function registration_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      if ($node->type == 'event_registration') {
       drupal_get_form('my_module_button_form');
      }
  }
}

function my_module_button_form($form_state)
{
  $form['complete_registration'] = array(
     '#type' => 'button',
    '#value' => t('Complete registration'),
    '#weight' => 20,
  );
  $forum['add_another'] = array(
    '#type' => 'submit',
    '#value' => t('Register another entrant'),
    '#weight' => 21,
  );
}

function my_module_button_form_submit($form, &$form_state)
{
  switch($form_state['values']['op'])
  {
    case t("Complete registration"):
      // do something when this button is pushed
      break;
    case t("Register another entrant"):
      // do something when this button is pushed
      break;
  }
}

Contact me to contract me for D7 -> D10/11 migrations.