8 Awesome Drupal Snippets I Wish I Knew Before

19 Mar · by Tim Kamanin · 4 min read

Today I want to share with you 8 great Drupal snippets that blew my mind. I found these browsing Dropbucket dot org - Drupal snippets repository.

There are lots of cool Drupal snippets being shared and stored at dropbucket.org but I found these to be the most new and interesting to me. I do really wish I knew about before. But Drupal is full of surprises and there are loads of tricks which don't know about, even if you've spent lots of years working with The Big Drop :) Okay, let's start. Snippets aren't sorted in any order of preference:

1. Find which modules implement specific hook

This is a drush trick which will return list of modules which implement given hook:

drush ev "print_r(module_implements('menu'))"

Submitted by Pierco, you can find this snippet here: http://dropbucket.org/node/135

2. Automatic Drush site aliases

This is another Drush related snippet. Actually it is a Drush module which automatically creates Drush site aliases for your Drupal multi-site environment.

$aliases = array();
$drupal = '/var/www/drupal';

// Automatic alias for each Drupal site
$site = new DirectoryIterator($drupal . '/sites');
while ($site->valid()) {
  // Look for directories containing a 'settings.php' file
  if ($site->isDir() && !$site->isDot() && !$site->isLink()) {
if (file_exists($site->getPathname() . '/settings.php')) {
  // Add site alias
  $basename = $site->getBasename();
  $aliases[$basename] = array(
'uri' => $basename,
'root' => $drupal,
  );
}
  }
  $site->next();
}

// Get all site aliases
$all = array();
foreach ($aliases as $name => $definition) {
  $all[] = '@' . $name;
}

// 'All' alias group
$aliases['all'] = array(
  'site-list' => $all,
);

Credits go to BWPanda, you can find this snippet here: http://dropbucket.org/node/84

3. Dev site settings.php tweaks

Some standard configuration ($conf) variables which can be added for dev sites. Placing these configs in settings.php will enable lot's of dev-related functions by default:

$conf['file_public_path'] = '/absolute/path/to/public/files/directory';
$conf['file_private_path'] = '/absolute/path/to/private/files/directory';
$conf['file_temporary_path'] = '/absolute/path/to/tmp/directory';
$conf['securepages_enable'] = FALSE;
$conf['devel_enable'] = TRUE;
$conf['reroute_email_enable'] = TRUE;
$conf['cache'] = FALSE; //page cache
$conf['block_cache'] = FALSE; //block cache
$conf['preprocess_css'] = FALSE; //optimize css
$conf['preprocess_js'] = FALSE; //optimize javascript
error_reporting(-1);
$conf['error_level'] = 2;
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

Submitted by ultimike, you can find this snippet here: http://dropbucket.org/node/216

4. Change taxonomy term path

This snippet let's you to change default 'taxonomy/term/tid' path for any vocabulary! Ubercool!

function MY_MODULE_entity_info_alter(&$entity_info) {
  $entity_info['taxonomy_term']['uri callback'] = 'MY_MODULE_taxonomy_term_uri';
}

function MY_MODULE_taxonomy_term_uri($term) {
  switch ($term->vocabulary_machine_name) {

case 'vocabulary_one':
  return array(
'path' => 'vocabulary-one/' . $term->tid,
  );
break;

case 'vocabulary_two':
  return array(
'path' => 'vocabulary-two/' . $term->tid,
  );
break;

default:
  return array(
'path' => 'taxonomy/term/' . $term->tid,
  );
break;
  }
}

Big thanks to malcolm for sharing, you can find this snippet here: http://dropbucket.org/node/92

5. Easy way to hide a field group including children

This snippet let's you hide any fieldgroup and its children for any form:

function MODULE_form_node_form_alter(&$form, &$form_state) {
  if (!user_access('admin_role_name')) {
field_group_hide_field_groups($form, array('group_for_admin_name'));
  }
}

Submitted by Netlooker, you can find this snippet here: http://dropbucket.org/node/130 Next two snippets do the same thing but leveraging different approaches:

6. Remove all markup of a field

Strip off field's markup on node_view level. Useful when you want to "clean" lots of fields at a time:

/**
 * Implements hook_node_view_alter().
 */
function YOUR_THEME_node_view_alter(&build) {
  unset($build['YOUR_FIELD_TO_EDIT']['#theme']);
}

Submitted by piouPiouM, you can find this snippet here: http://dropbucket.org/node/231

7. Clean all markup of a field with "nomarkup" theme function

Theme level approach. Useful when you want to clean a particular field in a particular template.

/**
 * Implements HOOK_theme().
 */
function YOUR_THEME_NAME_theme(){
  return array(
'nomarkup' => array (
  'render element' => 'element',
 ),
  );
}

/**
 * Cleans all markup of a field.
 */
function theme_nomarkup($variables) {
  $output = '';
  foreach ($variables['items'] as $delta => $item) {
$output .=  drupal_render($item);
  }
  return $output;
}

Then you can use this theme function in your .tpl file to get clean content from a field:

<?php
  $content['field_NAME']['#theme'] = "nomarkup";
  // Print a clean from markup field
  print render($content['field_NAME']); 
?>

You can find this snippet here: http://dropbucket.org/node/230 And as the dessert, here is my favorite Drupal snippet:

8. Cache regenerator

This snippet will regenerate your cached pages by visiting them one by one which is done thanks to xmlsitemap module and wget, look how simple it is:

#!/bin/bash
# Drupal cron
wget -O - -o /dev/null http://example.com/cron.php?cron_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Visit every page from your sitemap.xml
wget --quiet http://example.com/sitemap.xml --output-document - | egrep -o "http://example.com/[^<]+" | wget -q --delete-after -i -

Thanks to Komendant Melanżu for sharing! You can find this snippet here: http://dropbucket.org/node/241 So there were 8 Awesome Drupal snippets I wish I knew before :) Hope you like them and that at least one of them is news for you.

Come and joing growing community at Dropbucket.org where Drupal folks share and store great snippets!

Comments

Required for comment verification



pedro

Nice ones! If you've got devel installed, you can just do

drush hook menu
Reply · 4 years, 8 months ago
Phil Norton

Good post, I particularly like the dev settings.php tip :) I think something has gone wrong with the last snipped though. It looks like your input filter has turned the url's into links, which have then been printed as HTML.

Reply · 4 years, 8 months ago