CTools Modal Windows - Part II

Posted on 09/21/12

In the previous post in this series, I showed you how to create a simple modal window popup using the CTools modal framework. In this post, since we've already got a simple setup, we'll dive a little bit deeper into exactly how the modal window is generated. We'll need some of this knowledge before we tackle CTools modal forms in the next post.

As you recall, our module (mymodule) has a brand new hook_menu() implementation which registers a callback at the drupal path 'modal-test-callback'. Let's go to this page using our browser. So if you're site is http://example.com/, take a look at http://example.com/modal-test-callback. You should see some funky text like this:

What are we looking at? These are a set of JSON ajax commands. In other words, these are the commands that tell Drupal to pull up the modal window and display 'Hello World'. From our code before, these commands are generated via ctools_modal_render().

<?php
function mymodule_modal_callback() {
 
ctools_include('ajax');
 
ctools_include('modal');
 
ctools_modal_render(t('Modal Window Title'),t('Hello World'));
}
?>

ctools_modal_render() actually prints an array of ajax commands that Drupal's ajax framework understands. If we want, we can use our own commands to do something different.

<?php
function mymodule_modal_callback() {
 
ctools_include('ajax');
 
ctools_include('modal');
 
//ctools_modal_render(t('Modal Window Title'),t('Hello World'));
 
$commands[] = ajax_command_alert("THIS IS AN ALERT COMMAND");
 
$commands[] = ajax_command_after('.ctools-use-modal',"<h1>I JUST ADDED A HEADER</h1>");
  print
ajax_render($commands);
  exit;
}
?>

We've commented out ctools_modal_render() so we can play around with the ajax commands of our choosing. Here I'm using two different ajax commands. The first is ajax_command_alert() which shows a normal browser alert box. This command can sometimes be helpful when debugging. The second command invokes jQuery's 'after'. The first argument is a selector, and the second argument is some content. This command will add the <h1> content after any element with the ctools-use-modal class. Try your modal link now. You should see an alert popup and a big ol' "I JUST ADDED A HEADER" after the link. There's a long list of ajax commands that are part of core Drupal. I invite you to explore them.

The ajax_render() function turns the array of ajax commands into the weird looking JSON output you saw in your browser window earlier. Note that we print that content and exit...we don't want Drupal to display a full html page, so we don't return anything from this function. ctools_modal_render() handles all of this printing and exiting for us, but when we get into ctools_modal_form_wrapper() in the next post, we'll be doing the ajax rendering and printing on our own, so I thought I'd introduce it here.

Comments

This was exactly what i was looking for. Saved me a great deal of time.

Kieran
6/19/13 5:53 pm

Thnx again. Karin for Drupal Tutorial Czar.

Good dayy As i truly want оur оwn content аt blog. Thеy happen
to bе certaіnly goоd I speculate уou possess tо insert lots оf opeate in your arеa.
As ωell if you require to yоu are ablе tо tаke a look аt my webb site аnd depart
sеveral answer. Ӏt ωould likеly bee aсtually attractive.
Αll the best.

Ӊey there! Do you know if they make any plugins to assist wіth SEO?
I'mtrying to get mmy bloog to rajk for somе targeted keywords bbut I'm not seeiոg ver ցοߋd success.
If youu кnow of ɑny please share. Cheers!

Ϝeеl fee to surf to my homepage :: Software Seo

When I remember in 2004, I feel the present old. So many things have changed in that time!

The traditional gifts include silver items for this momentous occasion but you can give them anything which seems appropriate Most homes michael kors outlet only need several of such home made solar panels to shirk of conventional electricity Mobile Media works reason being it is cool, quick, and also powerful Properly they could not be further from the truth When interacting together with the constructing trades from start to finish, things could possibly get quite elaborate louis vuitton outlet Getting tropical plants can answer that issue, for the reason that you can search at lot of sites and have exactly what you were searching for Although this may sound louis vuitton easy, the values research approach is unequaled in its ability to extract and measure how beliefs, attitudes, emotion and reason integrate together to guide consumer choices and preferencesOne way of louis vuitton outlet determining what you need to do is to consider what it is you do not want The people should learn about the selection methods and procedures to buy these cake michael kors toppers These review the individual cleaners and can provide you some guidance as to which are worth buying, and which are not Portable compact is really a term frequently employed louis vuitton to describe them but they do not have a particular name kind This enables you to go a long way with them and also you recommending their name to other michael kors clients If your web site provides sufficient information and is easy to navigate for human users, you do not have to keep changing it with the hopes of getting better louis vuitton search engine rankings The planning process will make you understand how to run your business wellAlthough you might want to get used to Absinthe the initial time you use it, louis vuitton outlet online it is actually a tool that is created with the users in thoughtsStep 7: If in doubt Consult A fantastic DoctorShould you be over 40 or even in bad physical louis vuitton purses shape you must consult a great DoctorShopping for food on an unfilled stomach may enhance the probability of buying foodstuff that you do not actually need Items that are only louis vuitton luggage dull are often a lot cheaper than items that are of better excellence and are more expensive, this is a logical truth You can be cuttingedge in coming up with red bottom shoes the picture that will make your product stand out There are plenty of zero cost tools available on the web to measure keyword density, do don't overlook them Masonry is gucci bags one of the oldest recorded construction specialties, having grown into a highvolume business in most regions of the developed worldEven once the concrete is very long healed, soil movement or louis vuitton settlement can induce new cracks These jackets and coats made by Moncler have been in a position to help boost the character and created a fashionable appear to the person gucci outlet online in addition to giving protection from the chilly climate in winter It is vital that you do a regular evaluation of company printing needs if you wish to ensure that Christian Louboutin all of your printing needs are being satisfied as economically, successfully and efficiently as you possibly can Try to maximize the benefits of having a variety of options and use michael kors outlet the shelves to their most Most of the company owners decide to create eye catching advertisements so that the people might see them through various sources and develop an urge michael kors outlet online to use their goodsThe third reason is that you can find them rather easily

How delicateCoach Outlet Onlineare the tender shoots unfolded layerCoach Factory Outletby layer. Of what a whiteness isCoach Outletthe last baby one of all, of whatCoach Outlet Onlinea sweetness his flavor. It is Coach Outlet Storewell that this should be the lastCoach Outlet Store Onlinerite of the meal -- finis coronatCoach Outletopus -- so that we may go straightCoach Factory Onlineon to the business of the pipe.Coach Factory OutletCelery demands a pipe rather thanCoach Outleta cigar, and it can be eaten betterCoach Factory Outletin an inn or a London tavern thanCoach Outletin the home. Yes, and it should beCoach Outlet Storeeaten alone, for it is the only Coach Outlet Store Onlinedistinctly, "Here we go gatheringCoach Outlet Onlinenuts in May"? Season of mistsTrue Religion Jeansand mellow celery, then letTrue Religion Outletit be. A pat of butter underneathTrue Religionthe bough, a wedge of cheese, aMichael Kors Outlet Onlineoaf of bread and -- Thou.foodMichael Handbags Outletl which one really wants to hearMichael Kors Outlet Store Online oneself eat. Besides, in companyMichael Kors Outlet Storesone may have to consider the wantsChanel Outlet Online of others. Celery is not a thingLouis Vuitton Outletto share with any man. Alone inLouis Vuitton Handbagsyour country