Making tabs in a template file with php

Tuesday, May 26, 2015 - 21:59

I had this issue last week, my boss came to me and me to split up the fields between two separate tabs on a template file.

The extra challenge was that we could not edit the module and only the .tpl file.

I decided to write out two html tabs and specify a query in the link

<?php // here is the code for making tabs?>

 <div id="my-tabset" class="drupal-tabs tabs">
     <ul class="clear-block ui-tabs-nav tabs tabs primary">
      <li><a href="?tab1">Section</a></li>
         <li><a href="?tab2">Guidance</a></li>
           </ul>
</div>

I decalred a query as the link inside of my unordered list.

Next I put split up the feilds

<?php // tab 1>
 <?php
if ($_SERVER['QUERY_STRING'] == "tab1" || $_SERVER['QUERY_STRING'] == "") {
print ($variables['my_module']['my_feild_1'])
}
?>

 

Here I wrote that if query string = tab 1 OR if it is empty, write feild 1

<?php // tab 2>
 <?php
if ($_SERVER['QUERY_STRING'] == "tab2" ) {
print ($variables['my_module']['my_feild_2'])
}
?>

Here I wrote if the query is tab 2, then write feild 2. What is great about this method is that it refreshes the page and appends the querry to the exisiting url. While this may not be the best solution in all cirumstances, it will work when the module files can not be edited..

Just to sum up, here is the entire code.

 <div id="my-tabset" class="drupal-tabs tabs">
     <ul class="clear-block ui-tabs-nav tabs tabs primary">
      <li><a href="?tab1">Section</a></li>
         <li><a href="?tab2">Guidance</a></li>
           </ul>
</div>
<?php // tab 1>
 <?php
if ($_SERVER['QUERY_STRING'] == "tab1" || $_SERVER['QUERY_STRING'] == "") {
print ($variables['my_module']['my_feild_1'])
}
?>

<?php // tab 2>
 <?php
if ($_SERVER['QUERY_STRING'] == "tab2" ) {
print ($variables['my_module']['my_feild_2'])
}
?>