Text Size
Sunday, May 20, 2012
Get a look down
Drupal >> Displaying items by tag: Drupal
Displaying items by tag: Drupal
Here's how you can get the first N elements of an array, it's so simple with the use of array_slice :
1
2
3
4
5
6
7
8
9
10
<?php
// Array
$myArray = array("bird", "dog", "cat", "horse", "lizard");
 
// Get the 2 first elements
$output = array_slice($myArray, 0, 2);
 
// Result
// This will give an array with elements : "bird", "dog"
?>
Published in Code snippets
Sunday, 08 January 2012 21:47

Passing variables to custom module template

Among the best practices in developing a Drupal module, the use of templates is placed high on the list, this allows us to respect the MVC pattern (or at least a small part of it).

Published in Drupal
Thursday, 08 September 2011 09:35

How to change Drupal's admin path

All Drupal's based websites come's with the same administration path /admin, so if someone know that your site is built with Drupal, he can easily access the admin access page. This path can be changed easily with this little configuration. Just place this code snippet in your settings.php file, and change the admin word with the one you wan't :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Change mywebpanel with the word you need
// Admin page can be seen with the new link : http://www.mywebsite.com/mywebpanel
function custom_url_rewrite($op, $result, $path) {
 
  if ($op == 'alias') {
    if (preg_match('|^admin(/{0,1}.*)|', $path, $matches)) {
      return 'config'. $matches[1];
    }
  }
 
  if ($op == 'source') {
    if (preg_match('|^mywebpanel(/{0,1}.*)|', $path, $matches)) {
      return 'admin'. $matches[1];
    }
  }
 
  return $result;
 
}
Published in Code snippets

You can check by this little snippet if the actual user visiting your website is logged in or not. This snippet can be used anywhere (modules, themes, blocks..)

1
2
3
4
5
6
7
8
9
<?php
  global $user;
  if ($user->uid) {
    // User is logged in. Do something.
  }
  else {
    // User in anonymous. Do something.
  }
?>
Published in Code snippets

Here's how you can make a string translatable in your javascript files loaded in Drupal.

1
2
3
4
5
6
7
8
// This little code will append a <div> to the body.
// The <div> contains a translatable string "Hello world!"
// You can translate this string easily in the Back office
// of your website by browsing this url : 
// http://you.website.here/admin/build/translate/search
// and search for Hello world!
// PS : it's a case sensitive input
$('<div>'+Drupal.t("Hello world!")+'</div>').appendTo('body');
Published in Code snippets

You can use imagecache directly in your module, invoke a preset, and apply it on an uploaded image with this code snippet :

1
2
3
4
5
6
7
8
9
10
// $preset: the name of your preset
// $image['filepath']: the path to your file. E.g: sites/default/files/my_picture.jpg
// $alt: the alt of your <img> tag
// $title: the title of your <img> tag
// $attributes: your can add attributes to the <img> tag. E.g: $attributes = array('class' => 'mypic')
//          will add a class 'mypic' to your image.
print theme('imagecache', $preset, $image['filepath'], $alt, $title,  $attributes); 
 
// E.g:
print theme('imagecache', 'my_custom_thumb_preset', 'sites/deafult/files/my_picture.jpg', t('My picture alt'), t('My picture title')); 
Published in Code snippets
Tuesday, 19 July 2011 13:48

How to override radios theme in Drupal

You can override the default radios theme by inserting this little code in your template.php file. You can find the default theme function in form.inc in the core folder : includes.

PS: this code is applicable on all form elements, just copy/paste the initial theme function from form.inc (the initial theme function name for our example is theme_radios() ) into your template.php, and rename the function E.g : theme_radios() => your_theme_name_radios()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function etinker_radios($element) {
  $class = 'form-radios';
  if (isset($element['#attributes']['class'])) {
    $class .= ' ' . $element['#attributes']['class'];
  }
  $element['#children'] = (!empty($element['#children']) ? $element['#children'] : '');
  if ($element['#title'] || $element['#description']) {
    unset($element['#id']);
    return theme('form_element', $element, $element['#children']);
  }
  else {
    return $element['#children'];
  }
}
Published in Code snippets

With this little code, you can assign many roles to a specified user :

1
2
3
4
5
6
7
8
9
10
// Here's an example of how to assign two roles to a user
$usr = user_load($uid); // $uid is the user ID
$roles = array(
	'uid' => $uid,
	'roles' => array(
		10 => 'Supervisor', // 10 is the role ID
		17 => 'Author'
		)
	);
user_save($usr, $roles);
Published in Code snippets
Here's a little code snippet that gives you the possibility to get the base path of your drupal sites in javascript :
1
2
3
$(document).ready(function() {
	alert(Drupal.settings.basePath);
});
Published in Code snippets
Usefull tricks