Professional in Drupal web development, theme designing, consultation and training


Segalanya mengenai Drupal

[Snippet] Drupal 7 DB Query

10 May 2014 - 09:35 pm

Drupal 7 implemented database abstraction layer which built up on top with PDO. This make Drupal 7 in one step forward more faster for querying data from database compare to Drupal 6. Below are the snippets that can really useful for drowling custom data from database.

$uid = 1;
$result = db_query('SELECT n.nid, n.title, n.created
FROM {node} n WHERE n.uid = :uid', array(':uid' => $uid));

// Fetch next row as a stdClass object.
$record = $result->fetchObject(); 

// Fetch next row as an associative array.
$record = $result->fetchAssoc();

// Fetch data from specific column from next row
// Defaults to first column if not specified as argument
$data = $result->fetchColumn(1); // Grabs the title from the next row

// Retrieve all records into an indexed array of stdClass objects.

// Retrieve all records as stdObjects into an associative array
// keyed by the field in the result specified.
// (in this example, the title of the node)

// Retrieve a 2-column result set as an associative array of field 1 => field 2.
// Also good to note that you can specify which two fields to use
// by specifying the column numbers for each field
$result->fetchAllKeyed(0,2); // would be nid => created
$result->fetchAllKeyed(1,0); // would be title => nid

// Retrieve a 1-column result set as one single array.
// Column number can be specified otherwise defaults to first column

// Count the number of rows

[Snippet] The right way rendering field in Drupal 7

29 Apr 2014 - 07:27 am

The best part in Drupal 7, it was totally a massive upgrade from Drupal 6 by introducing of entity concept. This is really awseome since we do not depend on "nodes" concept anymore. Supporting the entity, more and less in D7 we could expand used of Field API which provide custom field design through GUI.

Its not stop there. For themers, we able to print and render any field from any entity bundle directly into theme files without sacrifice write up SQL query code. By using node_load(), we load everything node data for nid targeting.

The best practices to render specific field is using field_get_items(). After that pass this array into field_view_value() to get the raw value and sanitize them before rendering.

$node = node_load($nid);
$field = field_get_items('node',$node,'field_name');
$output = field_view_value('node',$node,'field_name',$field[0]);
print render($output);

This method make the render value aware towards language tag. Direct calling raw value like this:

$value = $node->field_name['und'][0]['safe_value'];

is not a best practice!

[Snippet] Pass multiple filter values in Drupal Views

28 Apr 2014 - 10:31 pm

One the most powerful function in Drupal Views is contextal filter. This function able to the Views module to filter the result based on argument specific in contextual filter item. However, the problem with this function is it unable to support multiple filter by "OR" conditions. The best solution to counter this problem is by altering its query before Views rendering the results.

By creating custom module, we leveraging hook_views_query_alter().

function my_module_views_query_alter(&$view, &$query) {

This will shows all query that available in all views. So, we specific by add condition to target specific views.

if ($view->current_display == 'my_current_display') { ... }

Then, by adding query altering for an array:

foreach ($nids as $nid){
  $query->where[1]['conditions'][] = array(
   'field' => 'node.nid',
   'value' => $nid,
   'operator' => '=',

After that we add some more query altering:

$query->where[1]['type'] = 'OR';

Save and clear your cache.

[Snippet] Menus get duplicate in Admin Menu for Drupal 7

19 Mar 2014 - 07:12 pm

During developing web using Drupal 7, sometimes we caught with duplication of menus when using Admin Menu. This problem usually happen when we accidently move or modify administration menu to other menu blocks. Actually it shoudn't happen because Drupal was designed to able handle any modification make to any menu.

However when someone with inexperience with Drupal system, installing unstable modules that interfere with Drupal menu system could leads this problem happened unexpected.

For solution:

We need to log into database either through bash or using GUI like PHPMyAdmin.

Then, ran this query:

DELETE FROM `menu_links` WHERE `menu_links`.`module` = 'admin_menu'

After that ran this query:

DELETE FROM `menu_links` WHERE `menu_links`.`module` = 'system'

These two query will delete all menus under admin menu. You will face blank menu if try to refresh any page.

After that go to "admin/build/modules".

Here Drupal will rebuilt all menu system again based on default configuration.

If it still didn't work, repeat this step again but instead go to "admin/build/modules", run directly in web browser bar, "". This step will force Drupal to refresh all default configuration and modules installed.

Lastly clear all Drupal cache manually.

Good luck.

[Snippet] Getting all permission list index based on roles

09 Mar 2014 - 02:32 pm

Drupal handle permission through roles base assigning to user. One user could apply multiple roles. Each of roles specified various permission define in hook_permission(). To get list of index for all permission for one user, we could used this snippet.

  $roles = array(3 => 'Project Manager');
  $roles = $user->roles;
  $permissions= user_role_permissions($roles);

Then by using user_access, we can define custom arguments like below:

if(user_access("create project content")) {
 print '<a href="/node/add/project">New Project</a>';