render quicktab programatically
linwj - November 11, 2008 - 11:48
| Project: | Quick Tabs |
| Version: | 6.x-2.x-dev |
| Component: | Documentation |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
can quick tabs be used for normal pages as well ?
current iam using it like this
quick tab block shows only if page is "book1" and location of quick tab block is set in the center
its the quicktab block which does all the pulling of data from all over the place and displays them within each tab
the actual "book1" node shows up only very little content
so it seems like the quicktab block IS the content for book1

#1
if php format is enabled for the node, then you can insert quicktabs like this:
<?php$qtid = 42; // write here your quicktabs id.
$quicktabs = quicktabs_load($qtid);
print theme('quicktabs', $quicktabs);
?>
you can even create the quicktabs dynamically, for example:
<?php
$tabs['first'] = array(
'title' => t('One'),
'type' => 'view',
'vid' => 'my_view_id',
'display' => 'my_display',
'args' => 'my_arguments',
);
$tabs['second'] = array(
'title' => t('Two'),
'type' => 'block',
'bid' => 'my_block_id', // check comment #14
'hide_title' => TRUE,
);
$tabs['third'] = array(
'title' => t('Three'),
'type' => 'node',
'nid' => 'my_node_id',
'teaser' => TRUE,
'hide_title' => TRUE,
);
$tabs['fourth'] = array(
'title' => t('Four'),
'type' => 'freetext',
'text' => 'Hello World',
);
$quicktabs['qtid'] = 'any-unique-id';
$quicktabs['tabs'] = $tabs;
$quicktabs['style'] = 'Excel';
$quicktabs['ajax'] = FALSE;
print theme('quicktabs', $quicktabs);
?>
so you don't need to place the quicktabs block to any region.. does it answer your question?
maybe using an insert_quicktabs module (not exists yet) would be easier
EDIT: this example does not work correctly in ajax mode, change the identifier to 'anyuniqueid'. Do not use hyphens in qtid till this issue is not fixed #449628: Programmatically Create Quicktabs $quicktabs['qtid'] cannot contain dashes
#2
Hi,
I have created an issue http://drupal.org/node/345581 regarding arguments in ajax. Thanks
#3
create a book page about "render quicktab programatically" from these examples
#4
Can you please expand on this? I have created a quicktab programmatically in a module i've written but when executing
$content = theme('quicktabs', $quicktabs) I keep getting a NULL result?
#5
I need to know what you have in the $quicktabs variable to be able to help you..
This simple example should always work in a node with PHP input format:
<?php
$tabs['0'] = array(
'title' => t('Hello'),
'type' => 'freetext',
'text' => 'Hello World',
);
$quicktabs['qtid'] = 'unique-id';
$quicktabs['tabs'] = $tabs;
$quicktabs['style'] = 'Excel';
$quicktabs['ajax'] = FALSE;
print theme('quicktabs', $quicktabs);
?>
#6
Does this include drupal 5.x
#7
@ezunkwelle: no. This works only with 6.x-2.0 (and above)
#8
I found it necessary to start out the code snippets above with
<?php$tabs = array();
?>
#9
as I know, if you define one element in the array then it is an array, so I don't understand why do you have to specially define it..
#10
Code in #1 works fine for me
#11
important note: "freetext" type tab does not work with ajax quicktab, it is not possible.
#12
for the original question. other options to display quicktab in one node:
1. if PHP input format is not enabled you can use the Insert Block module
2. create a theme template file page-node-42.tpl.php (or other template file options: http://drupal.org/node/190815), and insert the PHP snippet (#1) into that file (EDIT: this is not wrong, see comment #13)
#13
this script mostly does not work inside page.tpl.php and page-*tpl.php templates as quicktabs needs additional js and css files. (this problem is not Quick Tabs specific, same applies for example for Views) Calling drupal_add_css() or drupal_add_js() from page.tpl.php has no effect..
So if you really need to render quicktabs inside page.tpl.php you need to add the required files manually.. As it is hard to list all the required css and js files, the better option would be to make the quicktab rendering inside the preprocess function..
you can do it in theme's template.php file
<?php
function MYTHEMENAME_preprocess_page(&$vars) {
// As quicktabs require additional js and css files, we must construct the quicktab here.
$quicktabs = quicktabs_load(42);
$vars['myquicktab'] = theme('quicktabs', $quicktabs);
// Reconstruct CSS and JS variables.
$vars['css'] = drupal_add_css();
$vars['styles'] = drupal_get_css();
$vars['scripts'] = drupal_get_js();
}
?>
now, in your page.tpl.php you can use
<?phpprint $myquicktab;
?>
#14
I try to render the quicktabs in a node using the example #1.
I can't get it to load bloack using this example;
<?php$tabs['second'] = array(
'title' => t('Two'),
'type' => 'block',
'bid' => 'my_block_id',
'hide_title' => TRUE,
);
?>
I think I got the block_id's wrong. Is that supposed to be the ID I see when I edit a block, ore the ID I find in the database (BID). Both don't seem to work.
#15
Re #14
oh, that is not that simple. That id is really wrong in quicktabs. (I should have fixed that long time ago. Maybe in the 3.x version.)
$pos = strpos($tab['bid'], '_delta_');$blockmodule = substr($tab['bid'], 0, $pos);
$blockdelta = substr($tab['bid'], $pos + 7);
format: $module_delta_$bid
example: "user_delta_3" should be the "Who's online" block..
#16
@ Pasqualle, Thanks for your fast reply. You made my day!!
You can see it in action at www.zookah.com at the homepage.
Thanks!!
#17
You are welcome. That page is a nice usage example of quicktabs..
Those login boxes should check additional settings and permissions. For example the Register tab should not be displayed if you disable registration.. But you may easily add that logic as you created the tabs programatically, maybe you already done that..
#18
I use the codes in #1 in a block (rather than in a node). Each tab refers to some other standard blocks. The codes generate the tabs but there are no contents for the tab.
Anything I shall revise if the codes are used in a block?
#19
@askit: can you show a snippet from the code? Did you name the bid correctly, as described in comment #15?
#20
Hi Pasqualle,
Thanks for all the help here. I have one question.
This is how I add tab for example free text type
<?php$tabs['fourth'] = array(
'title' => t('Four'),
'type' => 'freetext',
'text' => 'Hello World',
);
?>
I'm trying to nest few quicktabs into other quicktab instance.
Is this correct way? It doesn't seem to do anything.
<?php$tabs['first'] = array(
'title' => t('Academics'),
'type' => 'quicktabs',
'qtid' => 'academicsqtid',
);
?>
Thanks a million,
Eddie
#21
Just so we don't go back and forward I'm getting some data from view and trying to populate three tabs with nested tabs.
This is the code I'm using:
<?php
$view_result = views_get_view_result('services', $display_id = "default", $args = array(7));
foreach($view_result as $item){
$node = node_load($item->nid);
$academicstabs[$node->nid] = array(
'title' => t($node->title),
'type' => 'node',
'nid' => $node->nid,
'teaser' => FALSE,
'hide_title' => TRUE,
);
}
$quicktabsacademics['qtid'] = 'academicsqtid';
$quicktabsacademics['tabs'] = $academicstabs;
$quicktabsacademics['style'] = 'Excel';
$quicktabsacademics['ajax'] = FALSE;
$view_result = views_get_view_result('services', $display_id = "default", $args = array(8));
foreach($view_result as $item){
$node = node_load($item->nid);
$studentsstabs[$node->nid] = array(
'title' => t($node->title),
'type' => 'node',
'nid' => $node->nid,
'teaser' => FALSE,
'hide_title' => TRUE,
);
}
$quicktabsstudents['qtid'] = 'studentsqtid';
$quicktabsstudents['tabs'] = $studentsstabs;
$quicktabsstudents['style'] = 'Excel';
$quicktabsstudents['ajax'] = FALSE;
$view_result = views_get_view_result('services', $display_id = "default", $args = array(9));
foreach($view_result as $item){
$node = node_load($item->nid);
$staffstabs[$node->nid] = array(
'title' => t($node->title),
'type' => 'node',
'nid' => $node->nid,
'teaser' => FALSE,
'hide_title' => TRUE,
);
}
$quicktabsstaff['qtid'] = 'staffqtid';
$quicktabsstaff['tabs'] = $staffstabs;
$quicktabsstaff['style'] = 'Excel';
$quicktabsstaff['ajax'] = FALSE;
$tabs['first'] = array(
'title' => t('Academics'),
'type' => 'qtabs',
'qtid' => 'academicsqtid',
);
$tabs['second'] = array(
'title' => t('Students'),
'type' => 'qtabs',
'qtid' => 'studentsqtid',
);
$tabs['third'] = array(
'title' => t('Staff'),
'type' => 'qtabs',
'qtid' => 'staffqtid',
);
$quicktabs['qtid'] = 'all';
$quicktabs['tabs'] = $tabs;
$quicktabs['style'] = 'Excel';
$quicktabs['ajax'] = FALSE;
print theme('quicktabs', $quicktabs);
?>
I changed 'type' => 'quicktabs' to 'type' => 'qtabs' looking at the module code.
I tried to print quicktabs genrated by view results and they display ok but they won't nest into the bottom one.
Can't figure out why is this not working :)
Please help
Thanks,
Eddie
#22
The problem is that 'qtabs' type works only with quicktabs stored in the database..
Try to change the last quicktab like this:
<?php$tabs['first'] = array(
'title' => t('Academics'),
'type' => 'freetext',
'text' => theme('quicktabs', $quicktabsacademics),
);
$tabs['second'] = array(
'title' => t('Students'),
'type' => 'freetext',
'text' => theme('quicktabs', $quicktabsstudents),
);
$tabs['third'] = array(
'title' => t('Staff'),
'type' => 'freetext',
'text' => theme('quicktabs', $quicktabsstaff),
);
?>
#23
Thanks a lot man :)
You made my day. It's working perfect.
Ed
#24
Hi Pasqualle, thank you for the hints on block id - that's the cause. I used "block-og-0" (group detail block). QuickTabs works when I change block id to "og_delta_0". Thank you so much!
#25
Hello,
I am trying to get some quicktabs containing full nodes to render programatically. The node has children (using node hierarchy) and I want to display one of each content type (if present).
The problem is I would like to use AJAX but as soon as I set $quicktabs['ajax'] = TRUE then only the first tab displays the full node and the rest display teasers only...
<?php
// Get all children
$result = db_query("SELECT nid FROM {nodehierarchy} WHERE parent = ".arg(1)." ORDER BY order_by");
while ( $row = db_fetch_array($result)) {
$nid = $row['nid'];
$node = node_load($nid);
// Test for node type
switch ( $node->type ) {
case 'event_images':
// Only load one of each type (first one in the results array)
if(!$tabs['images']){
$tab = 'images';
$title = 'Images';
}
break;
case 'event_text':
if(!$tabs['text']){
$tab = 'text';
$title = 'Text';
}
break;
case 'event_audio':
if(!$tabs['audio']){
$tab = 'audio';
$title = 'Audio';
}
break;
case 'event_video':
if(!$tabs['video']){
$tab = 'video';
$title = 'Video';
}
break;
case 'event_publication':
if(!$tabs['publication']){
$tab = 'publication';
$title = 'Related Publication';
}
break;
default:
$tab = '';
$title = '';
break;
}
if($tab != ''){
$tabs[$tab] = array(
'title' => t($title),
'type' => 'node',
'nid' => $nid,
'teaser' => FALSE,
'hide_title' => TRUE,
);
}
}
$quicktabs['qtid'] = 'eventtabs';
$quicktabs['tabs'] = $tabs;
$quicktabs['style'] = 'nostyle';
$quicktabs['ajax'] = FALSE;
print theme('quicktabs', $quicktabs);
?>
Is there anything you can see that I'm doing wrong? Thanks in advance.
#26
@featherbelly: yes, I can confirm the problem. this is some kind of php weirdness..
use:
'teaser' => 0,that works..
#27
Thanks a lot for the hints in #1. I also read the samples from #21 and tried to reproduce this but failed.
How can I call an existing QuickTab inside a QTab or nest it also programmatically tab by tab inside a QuickTab?
Example:
Tab 1: Info (type: view. works fine)
Tab 2: Media (type: freetext?)
-> Pictures (type: view. How to nest this view inside 'Tab 2: Media' ?)
-> Podcasts (type: view. Here the same questions)
Tab 3: Another info (type: view)
Here my attempt:
<?php
$tabs['first'] = array(
'title' => $node->title,
'type' => 'view',
'vid' => 'my_view',
'display' => 'block_1',
'args' => '%1',
);
$tabs['second'] = array(
'title' => t('Media'),
'type' => 'freetext',
'text' => theme('quicktabs', $quicktabsmedia),
);
$quicktabsmedia['first'] = array(
'title' => t('Pictures'),
'type' => 'view',
'vid' => 'my_picture_view',
'display' => 'block_7',
'args' => '%1',
);
$quicktabsmedia['second'] = array(
'title' => t('Podcasts'),
'type' => 'view',
'vid' => 'my_podcast_view',
'display' => 'block_4',
'args' => '%1',
);
$quicktabs['qtid'] = '13';
$quicktabs['tabs'] = $quicktabsmedia;
$quicktabs['style'] = 'Sky';
$quicktabs['ajax'] = FALSE;
print theme('quicktabs', $quicktabs); // I think here is something wrong
$tabs['third'] = array(
'title' => t('Another info'),
'type' => 'view',
'vid' => 'my_view',
'display' => 'block_6',
'args' => '%1',
);
$quicktabs['qtid'] = '12';
$quicktabs['tabs'] = $tabs;
$quicktabs['style'] = 'Sky';
$quicktabs['ajax'] = FALSE;
print theme('quicktabs', $quicktabs);
?>
Thanks a lot for your help. :)
Kind regards,
Stefan
#28
this should work:
<?php
$tabs['first'] = array(
...
);
$quicktabsmedia_tabs['first'] = array(
...
);
$quicktabsmedia_tabs['second'] = array(
...
);
$quicktabsmedia['qtid'] = '13';
$quicktabsmedia['tabs'] = $quicktabsmedia_tabs;
$quicktabsmedia['style'] = 'Sky';
$quicktabsmedia['ajax'] = FALSE;
$tabs['second'] = array(
'title' => t('Media'),
'type' => 'freetext',
'text' => theme('quicktabs', $quicktabsmedia),
);
$tabs['third'] = array(
...
);
$quicktabs['qtid'] = '12';
$quicktabs['tabs'] = $tabs;
$quicktabs['style'] = 'Sky';
$quicktabs['ajax'] = FALSE;
print theme('quicktabs', $quicktabs);
?>
#29
Awesome, it works! :) Thank you VERY MUCH! :)
#30
i have added a block to a quicktab
now i want to theme that block by changing view-view--...tpl.php file.
Now if I don't change the file, the raw data shows up under the tab
If I do change the file then none of the variables are passed down to the view.
Am I doing something strange/wrong?
#31
@activelyOUT:
you have added a block but theming a view? this does not make sense..
why do you see raw data? every view should be themed by default..
passing variables to view and changing the view's template file has no relation..
why did you posted the question in this issue? I do not see anything related in your comment..
please provide more information..
#32
I started a new issue
http://drupal.org/node/575852#comment-2036146
#33
just a comment.
I was using the following to display QT within nodes
<?php$block = module_invoke('quicktabs' ,'block', 'view', 1);
print $block['content'];
?>
Which is better?
<?php$qtid = 42; // write here your quicktabs id.
$quicktabs = quicktabs_load($qtid);
print theme('quicktabs', $quicktabs);
?>
cheers
#34
Hi juicytoo,
I happened to came across that issue and had tried using devel to compare the performance. Both approach have the same no. of queries and not much diff in time rendering. Maybe other members have a different view on this...
#35
Re #33
There is a slight difference between the two:
The first retrieves the quicktab as block, with block title and block content. And you display the block content only.
The second retrieves the quicktab directly.
but if you look into quicktabs_block() function you can see that there is only one step between the first and the second solution.
I would prefer the second, as when I am displaying the quicktab programatically I never need it themed as a block.
#36
Hi. This is a fantastic solution, and I got the tabs working in with the php method in the node, my only issue is that if I have a pager for the 2nd or 3rd etc. tab, every time it pages over the first tab appears again thus confusing the user, even though the next page is available when they click the preferred tab. Is there any way to alleviate this issue by keeping the preferred tab visible when the page is clicked? Or is there perhaps a better way of moving the pages over with jquery or something like that. Thanks again. This module rocks!
#37
@CinemaSaville: I think you are looking for this issue: #248798: Tab reset with page load.
#38
subscribing