Allow parameters in the tabs generation callback
svihel - August 14, 2008 - 08:41
| Project: | Magic Tabs |
| Version: | 5.x-1.2 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Description
Hello,
I would like to use the magic tabs and add something like following code in node:
print magic_tabs_get('nulajedna','10','11','12');
The output should be a node with 3 tabs which will show nodes by ID 10, 11, 12. Is it somehow possible?
Or maybe I just misunderstood the function, right now I believe that only way to do something like this is to create template for every page in template.php file in themes.

#1
You need to create a module, and prepare a function that fills in the tabs, something like:
<?phpfunction mymodule_mytabs($active=0) {
$nids = array(10, 11, 12);
$tabs = array();
foreach ($nids as $nid) {
$node = node_view(node_load($nid));
$tabs[] = array (
'title' => "node #$nid",
'content' => $node,
);
}
return $tabs;
}
?>
Then, where you want to print the tabs, you can just call
<?phpprint magic_tabs_get('mymodule_mytabs');
?>
#2
Thanks for answer.
Sorry for my english, I maybe put it wrong. What I really meant was - could I somehow pass the parameters from node to the code to the mymodule_mytabs function?
For example if i put this code in node body:
<?phpprint magic_tabs_get('mymodule_mytabs','first-node-id','second-node-id','third-node-id');
?>
It would create node with 3 tabs with content based on node IDs. Meaning that I want to re-use the function every time I want to create a node with tabs, just every time with different ones.
#3
No, you cannot do it at the moment. This is not straight forward to implement due to ajax functionality.
I'll keep this in my task list, and as usual - patches are welcome.
#4
Thats unfortunate, but ok, thanks for info. Looking forward to see this function in next release :]
I unfortunately am not really a programer to do it myself :(
#5
No problem, by submitting this request you already contributed :)
If you are not a programmer, you might want to take a look at http://drupal.org/project/quicktabs which does great work in keeping tabs management PHP-free. You need to know some PHP to use this module...
#6
I thought about this for a while and get the idea to use Content Template module with CCK Link. Goal was to create easy to operate user interface which could allow users to create tabbed node.
First I created node type, added cck Link field, allow multiple entries (each entry should contain node id). Then I created Contemplate for this content type and as Body Template I input this:
<?phpfunction asdf($active=0) {
$nids = array(49, 50, 46);
$titles = array("Overview", "Documentation", "Buy");
$tabs = array();
$i = 0;
foreach ($nids as $nid) {
$node = node_view(node_load($nid));
$tabs[] = array (
'title' => t($titles[$i]),
'content' => $node,
);
$i++;
}
return $tabs;
}
print magic_tabs_get('asdf');
?>
This actually work (meaning that it created tabs from nodes 49,50 and 46), so I wanted to go further and create template that will show nodes based on IDs that is submited in node. BUT there was one bug I didnt noticed at first, although code above showed tabs as they should be I later realized that when I clicked on them probably the AJAX script did something wrong and only blank space appeared. Strange thing is that the selected tab is showing all right (the right content is shown), so I'm guessing this script couldn't be completely wrong.
The final script should look somehow like this:
<?phpfunction asdf($active=0) {
$tabs = array();
foreach ((array)$node->field_tab as $item) {
$node = node_view(node_load($item['url']));
$tabs[] = array (
'title' => t($item['title']),
'content' => $node,
);
}
return $tabs;
}
print magic_tabs_get('asdf');
?>
#7
Quick Patch: Not tested yet
I added an argument to magic_tabs_get
<?phpfunction magic_tabs_get($callback, $active = 'first', $args = array(), $_ajax = FALSE) {
?>
So we can parse $arguments into the themefunction with call_user_func_array()
When we have this functionality, things like a magic tabs panel style plugin should be possible
// not tested yet
#8
#9
I've committed a fix to the CVS (http://drupal.org/cvs?commit=141819)
I have tested this both with/without JS, with logged in and anonymous, and it seems to work. If you need this and you are brave - please try and let me know if it works.
I updated the documentation of magic_tabs_get() function (the comment above the function), and the example callback now uses arguments - please consult these two on how to use this.
#10
wait i would switch $args and $_ajax in the argument, so all current usages of the module would work without rewrite the code
#11
Take a look at the code, I removed the $_ajax completely, so this is not a problem anymore.
#12
To all who subscribed to this thread - I'll appreciate if you can test the latest dev version and see if it works to your needs.
#13
Automatically closed -- issue fixed for two weeks with no activity.
#14
First, THANKS FOR THIS CLEVER ADDITION!
I just tested it, but I didn't like the way the URL looks for the additional args, so i changed args from serialize/unserialize to implode/explode:
@ Line 108
<?php//$cb_args = !empty($args) ? urlencode(serialize($args)) : '';
$cb_args = !empty($args) ? urlencode(implode(';',$args)) : '';
?>
@ Line 168
<?php// $args = unserialize($_GET['args']);
$args = explode(';',$_GET['args']);
?>
Thus for:
<?phpprint magic_tabs_get('asdf','first',$node->nid);
?>
http://server/node/4011?asdf_tab=0&referer=node/4011&args=a:1:{i:0;s:4:"4011";}
... becomes ...
http://server/node/4011?asdf_tab=0&referer=node/4011&args=4011
NOTE this is untested with multiple arguments of varying TYPES, the best thing i can recommend is to just be sure to cast correctly in your callback if you need something other than text. In my case, I cast the first extra arg to an int (since its always going to be the $nid).
I'm sure if someone wanted, this could even be improved by JSON-izing the args instead of serialize or implode/explode ;)
Cheers!
#15
i like this idea:
Its additional, i think a little bit faster ^^
$ php implode.php1.20513796806342
$ php serialize.php
1.4147288799286
We have to create an array out of the ; ; ; String.
This could be done with
<?php$array = array();
foreach (explode() as $value) {
$array[] = $value;
}
?>
Any other suggestions?
#16
The problem with the implode/explode scheme is that it is limited in the type and value of the variables themselves. For example, if you want to send an array, or a string with ';' in it, it will fail completely.
The only way I know to keep this non-JS friendly, is to send arguments on the URL, and the only safe way I know to send (unknown types of) arguments in the URL is to serialize them.