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

Current Drupal Modules Used

In Drupal, modules (a.k.a. plugins, extensions, whatsoever) have been of great help in developing a site. It makes it easier for developers to achieve any feature in a given website. So, I'd like to share some Drupal modules that I am currently using and hopefully would also help others using Drupal:

Node Teaser - Allows users to specify a teaser that would be displayed when it is needed. Usually teasers are limited characters that are taken out from the body of a node or a content of a page. This module provides a separate teaser field.

Node Words - Allows users to manually specify a meta description and keywords for a page. A new field is created for users to input the description and keywords. Though, keywords and the number of characters for a description are limited.

Article - The module creates a hierarchy of index pages from an index page that contains all terms to index pages that contains the nodes marked to each terms. The URL of the main index page which contains all terms would be /article/0. The module lets you specify which vocabulary you would like it to generate pages for. Only drawback is, since Drupal creates an index page of nodes for terms (URL is /taxonomy/term/#)

Path Redirect - The module allows users to specify a redirection using HTTP redirect status for a specific URL. So, let's say you would like a page that was created with Drupal be redirected to another page, you could use this module. It also has options such as the page you want redirected goes to a page with a '?' or a "#", this module also allows you to specify that.

Global Redirect - Since when you specify aliases for pages, Drupal creates two URLs that contains the same content. Usually when you create a page and specify an alias for that page, visitors are able to access the same content using either /node/# or /alias. What this module does is redirect a visitor when he/she accesses a content using the URL /node/# to /alias. And, there's no need to do any configuration because once it is installed, it just do its thing. One drawback, however. Global Redirect, I believe, was not designed to work with Drupal installations in a subfolder. So, you only need to modify the module's .module file to make it work.

Print - The module creates a printer-friendly version page of the current node. It creates a link at the bottom (by default) of the page that says "Printer-friendly version". There is an option wherein you can choose in which section you would like to display the link. The module uses the URL /print/node_number that displays the printer-friendly page of the node.

Views - is, by far, the most powerful module that I believe I am currently using. The Views module allows admins to have control over it i.e., make it not visible to anonymous users, choose amongst various options on how it would display nodes belonging to a term, clone a view to save time, etc. Basically, its purpose is to create a directory that contains nodes depending on how you would like them to be sorted out - by titles followed by teasers, sorted alphabetically, directory contains nodes that belong to this and that term, etc.

taxonomy_node_get_terms in Drupal

Like what I've said in my previous post, I've been experimenting with the CMS Drupal. And there was this time that I got tired of adding a very simple HTML code in every node that I insert in Drupal. So, I was thinking instead of putting the code in each of them, why not create some sort of block that works like it.

The function of the block is to display a text that links back to the taxonomy terms the node belongs to. At first, I was thinking of using PHP functions to do that i.e., retrieve the term ids the node belongs to, create a link using HTML code, etc. But then, there was an easier way to do it using just two functions:

The documentation from the official site of Drupal would clearly explain how these methods work. Anyways, I have used the l function to create the links and taxonomy_node_get_terms to get the terms the node belongs to without minding the vocabulary. Now all I have to do is create some simple PHP functions to make my block work. Here's my code:

<?php
if (arg(0) == 'node' && arg(1)) {
$categories = taxonomy_node_get_terms(arg(1));
foreach ($categories as $category) {
print l('Back to ' . $category->name,
'taxonomy/term/' . $category->tid,
array('title' => $category->name)) . '<br //>';
}
}
?>

Code is quite self explanatory I guess. arg(0) checks if the first argument in the URL is a node. arg(1) checks if the second argument (w/c is the node id) in the URL is set. So, therefore the first statement checks if the page is a node and if a node id is set. I've used taxonomy_node_get_terms(arg(1)) to get the node id of the current node and then stored the array to $categories. After that, I do a foreach statement to get the terms. You'll be able to view the values in the array by var_dump'ing $categories. I only need to retrieve the term id and category name. In other words, I only need $category->tid and $category->name. The statement 'taxonomy/term/' . $category->tid gets the URL of the term. print l was used to create the link.

Adding Drupal Custom Functions

I have been experimenting Drupal for I think a month now. One problem that I have encountered with Drupal is not allowing to have HTML tags on the Title field when creating a new content. Drupal filters HTML tags for some reason, I guess. Anyways, I wouldn't want Drupal to do this so I searched for a solution and eventually was able to find one. There was actually a posted solution in the official website of Drupal which you can find here. The first function replaces the bb codes to html tags. While the second function strips off the bb codes.

The first thing I did was create the function on the file page.tpl.php which was, of course, a file located at the themes directory. What I did was use the bb code function to the variable $head and the strip function to $head_title. I also opened the node.tpl.php, declared the strip function, and used it to the $head variables once again. Everything was fine with the page.tpl.php file except for the node.tpl.php file. It had the error message that the strip function was already declared. It was then I found out that Drupal is calling the tpl files many times and that it was not advisable to declare functions in those files. I also found out that a certain file also from the themes folder is called by Drupal automatically. And this was the template.php file. So what I did is just declared the function over that file and voila! I have my own functions. Anyways, the template.php has a php start tag (<?php) but no end tag. This was intentional and not accidental. They say all Drupal files are like this. This is how my template.php looked like:


<?php
function returnTag($text) {
$bbcode = array(
'/\[br\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
);

$htmlcode = array(
'<br //>',
"<a href=\"$1\">$2</a>",
);
return preg_replace($bbcode, $htmlcode, $text);
// else return $text;
}

function stripTag($text) {
$bbcode = array(
'/\[br\]/is',
'/\[url\=(.*?)\]/is', '/\[\/url\]/is');
return preg_replace($bbcode, '', $text);
}

If you wish to add more bb codes, you can add regular expressions in the array.

This is actually a good way to not mess with the main theme files and just add your own functions on a specific file. If you think that you can only call these functions within the themes files, sad to say you're wrong. You can actually call these functions anywhere in the Drupal files (e.g. modules). But many say (including me), it's not advisable to do this -- I'm referring to messing around with the codes of other files of Drupal (e.g. modules) and adding your functions. Better to create some modules (which I don't know how to do yet) to alter the modules' functionalities.