Showing posts with label Drupal. Show all posts
Showing posts with label Drupal. Show all posts

Tuesday, July 27, 2010

Drupal Customization - Add New tab to a page

In a Drupal content page, when the user is logged in, on the top of the page there are "View" and "Edit" link buttons for the user to view and edit the content of the page. Now we want to add a "New" link button to allow the user create a new page of this type. This can be done this way.

In "themes -> [theme folder] -> page.tpl.php", we have these two lines:

<?php if ($title): print '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. $title .'</h2>'; endif; ?>
<?php if ($tabs): print '<ul class="tabs primary">'. $tabs . '</ul></div>'; endif; ?>

The first line is for the title. The second line is for the tabs "View" and "Edit". So we change the second line to be:

<?php if ($tabs): print '<ul class="tabs primary">'. $tabs . print_custom_tab($node) . '</ul></div>'; endif; ?>

And define the print_custom_tab() function at the end of the page:
<?php
function print_custom_tab($node) {
// Use this to limit the "New" tab to certain page types.
//if (substr($node->type, 0) == "[page type name]")
{
return "<li><a href='?q=node/add/" . str_replace("_", "-", $node->type) . "'>New</a></li>";
}
}
?>

From the $node we can also get $node->uid and other variables associated with node, and can do many other things this way.

Alternatively or in a cleaner way, there is an Add another module that do this. It needs to be configured on what page type and when to show. The configuration is done in "Administer -> Site configuration -> Add another".

Friday, June 18, 2010

Adding PHP snippets through the Drupal user interface

Enable PHP input filter: Go to Administer -> Site building -> Modules, and check PHP Filter under core - optional. Now when create new content, a PHP option will appear under input formats - select this to paste a PHP code snippet. This opens a powerful door to creating dynamic content on a Drupal site.

See Drupal: A beginner's guide to using snippets.

Often a PHP snippet is written inside a drupal module block. What if you want to access Drupal database from an external PHP file? Below is an example. See here for reference.

<?php
// Root path of drupal site, can be obtained using getcwd().
$path = "/web/1/www.hawaii.edu/drupalc/";
chdir($path);

// include needed files
include_once('includes/bootstrap.inc');
include_once('includes/database.inc');
//include_once('includes/database.mysql.inc'); // Disable this for drupal 6.

// Launch drupal start: configuration and database bootstrap
conf_init();
drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

// Page start. Output page as an Excel file download.
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"excel_download.xls\"");
header("Pragma: no-cache");
header("Expires: 0");

// table header
echo "<table border='1'>";
db_query("SELECT * FROM users"); // access to database.
// more processing ...
echo "</table>";

?>

Wednesday, February 3, 2010

Drupal - CCK and VIEW

Use CCK and VIEW modules of Drupal:
http://learn.awakenedvoice.com/2007/07/30/drupal-cck-and-views-tutorial/
http://www.apaddedcell.com/creating-a-custom-home-page-in-drupal-using-views

- Make view column header sortable.

In Basic settings, style "table" has this option: click on the "*" to its right, in the style options panel, check "Sortable" checkbox to the fields that you want to be sortable.

- Add more fields to profile.

Default profile contains only account name and email. You can add more by using the Profiles module in Administer -> User management -> profiles.

- Show two fields in one column (The following is copied from this link):

By default, each field is its own column. However, you can place multiple fields in the same column. To do this, pick which field you want to represent the column, then pick another field and set the 'column' value to that field. You can place as many fields as you like in a single column, but only the main field in a column can be click-sorted.

This way, you can allow the user to enter first name and last name separately at registration, then show full name in a view.

Blog Archive

Followers