0

What is the best way of include ctools_include('ajax'); into a page already defined by another module?

I'm trying to use ctools ajax framework to replace content (similar to how the ctools example hello world works).

I've tried to integrate the ctools hello world example into my existing code but it looks like none of the setup is run. When clicking on the the link it forwards to the menu link defined rather than changing the content.

EDIT:

I've used ctools_include('ajax') on the following hooks (all hooks confirmed correct by added debug to them):

  • nodeapi (alter)
  • preprocess node

This is my hook_nodeapi('alter') code, which inserts the text as per the example module.

ctools_include('ajax'); $links =array(); $links[] = ctools_ajax_text_button(t('Hello world!'), "defs/nojs/test", t('Replace text with "hello world"')); $node->body .= theme('item_list', $links, t('Actions')); $node->body .= theme('ctools_ajax_sample_container', '<h1>' . t('Sample Content') . '</h1>'); 

This is my menu item.

 $items['defs/%ctools_js/test'] = array( 'page callback' => 'br_defintions_funkyajax', 'access arguments' => array('access content'), 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); 

This is my callback function from the menu item.

function br_defintions_funkyajax($js = NULL) { $output = '<h1>' . t('Hello World') . '</h1>'; if ($js) { ctools_include('ajax'); $commands = array(); $commands[] = ctools_ajax_command_html('#ctools-sample', $output); ctools_ajax_render($commands); // this function exits. } else { return $output; } } 

1 Answer 1

1

Can't you just do

 ctools_include('ajax'); 

At the start of your existing code.

Ctools should put classes in your code which are fairly easy to spot.

Edit

Thanks for the code. I'm not sure what it is it could be one of a couple of things, but may be neither.

you are using $op 'alter' in hook_nodeapi, this is quite a late stage hook, and it is possible that add_js doesn't work then. 'view' may be a better $op for you to use.

Also you are using theme 'ctools_ajax_sample_container' this is part of the ctools_sample module. Do you have that module installed.

It is also possible that you have a js error somewhere else which is knocking out the ctools js functionality. Check any errors you have with firebug.

6
  • Yeah I should but for some reason done of the classes are appearing e.g. the classes appended to body. Commented Jul 1, 2011 at 15:01
  • Do you know if your code is actually running. Can you output any debug? Posting a code sample may help. Commented Jul 1, 2011 at 15:15
  • Updated the original post with code used. Commented Jul 5, 2011 at 12:08
  • I added it into a the view op and no change. No javascript errors. I have the sample module enabled and can hit the page and confirm that it works correctly. It's really odd that I can't get my implementation to work. Commented Jul 5, 2011 at 13:17
  • In firebug you can see if ctools has processed the link it will have a class ctools-use-ajax-processed what classes are on your link? Commented Jul 5, 2011 at 13:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.