Drupal 7

Drupal is a free and open source Content Management System (CMS) and Content Management Framework (CMF) written in PHP. It's use as a back-end system varies from personal blogs to corporate, political, and government sites.

snippet - Tables content to exclude on DB export in Drupal 7
24 Jul, 2018
Exclude content from these tables, to get clean db backup(backup_migrate module usage). You can even use git to store your database, when you don't export cache tables.
snippet - Migrate Field Collection in Drupal 7
25 Jun, 2018
You can use migrate to import field collections. It has full support. You just need to define the host entity, where you have defined the field collection.
snippet - Load a file by uri in Drupal 7
16 May, 2018
This snippet shows you how to load a file by uri. Basically this option is available with file_load_mulitple only.
snippet - Field group alter to add html in Drupal 7
4 May, 2018
Field group lets you do some amazing forms. Still when you have empty element or the form just is too long and needs some custom modifications, you can then use prefix and suffix from form api to...
snippet - Delete all terms from all vocabularies in Drupal 7
26 Mar, 2018
Delete all taxonomy terms from all vocabularies in Drupal 7. This is ran by Drush, but you can run it from wherever Drupal is initialised.
snippet - Drupal 7 commerce add product variation types programmatically
12 Mar, 2018
Example for how to create product variation types programmatically.
snippet - Drupal 7: Wrapping group by content in Views
24 Feb, 2016
You can use "group by" in views to group data by a field. When you start writing css for it, or even use accordion. You need to wrap the rows into a container. This can be achieved by creating a...
snippet - Change line item price programmatically in Drupal 7
29 Nov, 2015
Changing line item price is not as easy as you could think. It can seem that the price has changed, but there are multiple places where you have to actually change it. Especially when you compute the...
snippet - Check if current user has node edit permission in Drupal 7
19 Nov, 2015
Just a snippet that shows how to check if current user has edit permissions for some node in Drupal 7. Using node_access function like in the example you are also able to check permissions for...
snippet - Add .tpl.php files to your module in Drupal 7
9 Nov, 2015
This nifty little snippet will scan your module for tpl.php files. Include template files from your module. Override default template in custom module. I usually have a single module where I keep my...
snippet - Uninstall modules programmatically in Drupal 7
13 Jul, 2015
Snippet shows how to uninstall modules programmatically. This is not deleting the files, but doing a full uninstall.
snippet - Get GET parameters in Drupal 7
23 Mar, 2015
Getting URL query parameters array where unwanted elements are removed. For that you can use drupal_get_query_parameters function. In default q parameter will be removed. That parameter includes...
snippet - Post data to external url in Drupal 7
4 Mar, 2015
Sending data using POST to external url should be done by using drupal_http_request.
snippet - Edit field output before it is rendered in Drupal 7 using hook
18 Feb, 2015
Edit field output after the field module has performed the operation. This gives you possibility to alter the field output before rendered in your module. This works with display suite and other...
snippet - Drupal 7 - file_save entity wrapper file uri
10 Feb, 2015
Since this snippet includes a lot of information the title is simply a bunch of keywords. Basically we are using file uri to determine if it exists and is managed or needs to be saved as new managed...
snippet - Get values / Rendering of fields in Drupal 7
5 Feb, 2015
The idea would be that you don't take any values directly from node_load result. You can either use entity wrapper(is not a part of this tutorial) or field get and view functions.
written tutorial - Entity metadata wrapper (entity_metadata_wrapper) in Drupal 7
3 Feb, 2015
Entity API provides wrapper classes that make dealing with the values of an entity's properties and fields easier. This post is to make the information easy to learn by examples.
snippet - Load menu item first child in Drupal 7
24 Oct, 2014
This is how you can load menu items children. Also the example / snippet is about how you can get the first child, but the menu has to be in a single language tough. It takes access and being hidden...
snippet - Get a single query result as string in Drupal 7
30 Sep, 2014
This snippet shows you how to get a single query result as string in Drupal 7, using select from database abstraction layer.
snippet - Get node menu item (mlid) by node id (nid) in Drupal 7
25 Aug, 2014
There are many functions for menus, but since Drupal is not hierarchical but is modular, that's why menu doesn't have node requirement and the opposite way around. So this snippet shows how to get...
snippet - PHP array of iso country list
28 May, 2014
PHP array of iso country list. This list includes Kosovo (KV) and Democratic People's Republic of Korea (KP). That I found hard to find from somewhere else.
snippet - Create contextual links in Drupal 7
1 May, 2014
This snippet shows you how to create contextual links in Drupal 7. If you don't know what contextual links are then those are the links in front end that come up from the ratchet.
snippet - Install Drupal using Drush
29 Apr, 2014
How to install any version of Drupal using Drush the Drupal Shell. You can replace the version name with the version you like. Also feel free to replace words in caps to get an installation that you...
written tutorial - Rebuild menu in Drupal 7
24 Mar, 2014
Sometimes menu gets messed up and all the new links go on the admin menus first level. Or you have other reasons to rebuild menu. You also need devel module to do this.
written tutorial - Create custom pane programmatically for Panels in Drupal 7
24 Mar, 2014
This shows code examples of what I had to do to create custom pane programmatically for Panels in Drupal 7. In this example I create new pane where you can pick your node and display it.
snippet - Use Drupal to run queries on other databases.
14 Mar, 2014
First you need to add the connection information of the other database. This way you can use drupal database abstraction layer for altering any database.
written tutorial - Group views by first letter in Drupal
10 Mar, 2014
This tutorial describes how you can group the results of a view by first letter of their title. This might not suit for you in two cases, first if you need to order them by ascii not utf-8, secondly...
snippet - Migrate image from URL in Drupal 7
7 Mar, 2014
There is a module called migrate that enables you to migrate content to Drupal. This snippet shows you how to migrate image from URL to Drupal 7. You have to get file name from the importing source...
snippet - Alter node breadcrumbs by hook form your module in Drupal 7
26 Feb, 2014
I had a problem with path breadcrumbs where I couldn't get taxonomy therms through commerce product reference. So I had to disable the path breadcrumb for the node and do it manually. I didn't want...
snippet - Views header, footer, before and after using hook_views_pre_render() in Drupal 7
21 Feb, 2014
Add content to view header, footer, before or after using hook_views_pre_render() in Drupal 7. Using hook_views_pre_render gives you the opportunity to use values from executed views query.
snippet - Change field instance programmatically in Drupal7
20 Feb, 2014
Change or update field instance programmatically in Drupal 7. For more information check out Field CRUD API in drupal.org.
snippet - Use hook_page_build() instead of hook_init() to add CSS or JS to every page.
2 Jan, 2014
Use hook_page_build() instead of hook_init() to add CSS or JS to every page. The reason is that hook_init() runs on every request that goes to Drupal (AJAX requests, private file requests, boost /...
snippet - Change date format for better exposed filters in Drupal 7
15 Nov, 2013
This snippet shows you how to change date format for better exposed filters in Drupal 7. I was not able to use strtotime with the default setting.
snippet - Install module programmatically in Drupal 7
14 Nov, 2013
Installing module programmatically in Drupal 7 can be achieved with just one step, using module_enable function.
snippet - Converts URLs in text into links in Drupal 7
5 Nov, 2013
Converts URLs in text into links in Drupal 7 automatically. Three types of "links" get converted. URLs like http://example.com, e-mail addresses like name@example.com and urls without the "http://"...
snippet - Get taxonomy vid by machine name Drupal 7
7 Oct, 2013
This snippet shows how to get taxonomy vocabulary id (vid) by machine name of taxonomy vocabulary Drupal 7.
snippet - Using image_scale_and_crop in Drupal 7
3 Oct, 2013
This snippet shows you an example of using image_scale_and_crop function to scale and crop image as in image styles.
snippet - Useful hooks and functions in Drupal 7
2 Sep, 2013
List of useful hooks and functions in Drupal 7. You can find here something new or just bookmark it for your reference.
written tutorial - Change or add file icons to Drupal 7
21 Aug, 2013
Change or add file icons to Drupal 7. First you have to define your mime type of a file, if it isn't done already. Then add icon URL to that mime type (16x16).
snippet - How to create time field in Drupal 7
8 Aug, 2013
This example shows you how to create time field using date module and Field API in Drupal.
snippet - Alter filter in views exposed filters using form_alter in Drupal 7
30 Jul, 2013
This example shows how to alter filter in views exposed filters using form_alter in Drupal 7.
snippet - Customize Views RSS Feed Title in Drupal
14 Jul, 2013
This snippet shows you how to customize views RSS feed title in Drupal. This example shows you how to create title for taxonomy feed, so that the title would be the term with it's parents.
snippet - Use colorbox programmatically in Drupal
12 Jul, 2013
How to use colorbox module programmatically. Colorbox module provides with loading inline content, explained here: https://drupal.org/node/1971564. Since that didn't work for me, I tok a different...
written tutorial - Exclude current node from view
12 Jul, 2013
This tutorial shows you how to exclude current node from view. For an example, if you need a block that shows related content by taxonomy then you want to exclude current node from the list.
snippet - Create lists using theme through render in Drupal 7
7 Jun, 2013
This snippets shows you how to create lists the right way using theme_item_list.
snippet - Add body class programmatically from your module in Drupal 7
28 May, 2013
This simple snippet shows you how to add body class programmatically from your module in Drupal 7.
snippet - Creating views programmatically in Drupal 7
20 May, 2013
Creating views view in your module is really simple.
snippet - Form API -  Skip form validation for button in Drupal 7
5 Apr, 2013
Sometimes you have to make button that skips form field validations. For an example Cancel button. This snippets shows how to skip form validation for button. Works in Drupal 7.
snippet - Translate taxonomy terms in Drupal 7
3 Apr, 2013
How to translate localized taxonomy terms in Drupal 7.
snippet - Change page title programmatically in Drupal 7
19 Feb, 2013
How to override page title in your theme, after all modules have done their jobs.
snippet - Remove form elements in Drupal
8 Feb, 2013
This snippet shows how to remove form elements in Drupal. I am using hook_form_alter and access parameter from form api.
snippet - Drupal - Change page titles in template.php
27 Jan, 2013
Changing page titles in your theme. There is also hook_page_title_alter hook. If for some reason you still want to do it your theme, then this is how.
snippet - Views query alter - use select in where clause
23 Jan, 2013
How to use select query as where clause in Drupal views module query alter. You can't use hook_views_query_alter, because it places single quotes around the value. So you can't use query instead of...
snippet - Localize taxonomy term in Drupal 7
1 Dec, 2012
This snippet shows you how to Localize taxonomy term in Drupal 7.
snippet - Change Drupal calendar events color.
28 Nov, 2012
You can only change event colors by content type or taxonomy terms in Calendar view. More advanced coloring requires some programming.
snippet - Convert plain text URLs into links in Drupal
23 Nov, 2012
How to turn plain text URLs into active links in Drupal.
snippet - Using jQuery Datepicker in Drupal
25 Oct, 2012
This snippet shows how to use jQuery datepicker in Drupal. Drupal already includes jQuery and jQuery UI in its core. So you just have to make sure it's loaded and then you can use it in your scripts.
snippet - Field API email field in Drupal
30 Aug, 2012
Creating email field. The field is still textfield, you just have to make validation for the field.
written tutorial - Create your own template files ( tpl.php ) in Drupal 7
23 Aug, 2012
Creating your own template files that you can use in your modules in Drupal 7. Template files are for separating HTML from PHP. This tutorial shows you how to create and use tpl.php files. Template...
snippet - Programmatically create menu link language relation in Drupal 7
22 Aug, 2012
Programmatically create menu link language relation in Drupal 7. This example takes a certain taxonomy vocabulary by id and creates language relations to menu links by terms relations. You might need...
snippet - Get Years of nodes in Drupal 7
21 Aug, 2012
This query gets node created dates and selects all years of nodes that has been made. Also there are filters: node.status equals 1 is published filter, node.type is content type filter and node...
snippet - Make sure your cron runs daily in Drupal
14 Aug, 2012
How to make sure your cron task runs once per day in Drupal? You can set crontab once per day, but you still might want to run cron manually every now and then. Also you might have to change the cron...
snippet - Create table using theme table in Drupal
12 Jul, 2012
Create table using theme table in Drupal. This is the simplest example of creating table. In the code I added link to the documentation. You can do things like add classes, caption, colgroups etc.
snippet - Show Drupal 404 page programmatically.
25 Jun, 2012
Show Drupal 404 page programmatically. Just return this function from your page function.
snippet - Add node to nodequeue in Drupal 5-7
2 May, 2012
How to add node to nodequeue using a little code. You can make it automatic using Rules and PHP filter module(in core that adds option to add PHP code to textarea).
snippet - Translate block title in Drupal 7
2 May, 2012
Translate block title in Drupal 7 using this theme hook.
written tutorial - SQL query examples in Drupal 7
20 Apr, 2012
Drupal SQL query examples, includes regular query and select, insert, update and insert or update using abstraction layer. Explanations of different data return types like fetchAssoc, fetchAll and...
snippet - Page title for taxonomy term pages in Drupal
6 Apr, 2012
How to fix page titles for taxonomy term pages. It creates title from term hierarchy. You have to remove the views title override.
snippet - Get or load taxonomy term(s) in Drupal 7
6 Apr, 2012
Load taxonomy term object by name or tid. Also how to get term parents from top to bottom.
snippet - Term name in right language in Drupal using i18n
13 Mar, 2012
How to get term name in right language in Drupal? Internationalization (i18n) module is used for localization.
written tutorial - Drupal 7 multi-site on localhost (XAMPP or Windows example)
3 Mar, 2012
How I set up my multi-site in localhost so that my different sites would be in different sub-folders. I did it on my windows machine.
snippet - Remove language links from node links in Drupal 7
9 Feb, 2012
How to remove language links from node links in Drupal 7
snippet - Change file name after saving in Drupal 7
26 Jan, 2012
How to change file name programmatically after file is saved in Drupal 7.
snippet - Filter feeds log by Importer in Drupal 7
23 Jan, 2012
First you need to create a filter to your views. Add "Feeds log: Importer id" filter to All entries display / tab. Then this code snippet alters the form so there won't be textfield, but selection.
snippet - Remove views view filter programmatically in Drupal
16 Jan, 2012
How to remove views view filter programmatically in Drupal.
snippet - Get languages in Drupal 7
11 Jan, 2012
How to get all active languages as array in Drupal 7.
snippet - Language select field in Drupal 7
19 Dec, 2011
This code snippet is about creating languages form field in Drupal 7 using field api.
snippet - Add field to entity programmatically in Drupal 7
13 Dec, 2011
How you can add field to an entity programmatically in Drupal 7.
snippet - Add body classes in Drupal 7
8 Dec, 2011
Adding body classes in Drupal 7. Just a little code snippet that can be useful in some cases.
snippet - Display Entity Construction Kit (ECK) form programmatically
29 Nov, 2011
How to display eck form anywhere you wish, just few lines of PHP can make you a big head ache.
written tutorial - Redirect based on country in Drupal 7
15 Nov, 2011
Redirect users to their main language first visit. I needed it only on the front page, but you can simply remove the if statement if you need the functionality on every page. The language detection...
written tutorial - Create ajax forms in Drupal 7
23 Oct, 2011
How to create forms that are submitted via ajax. To understand this tutorial you have to have a basic knowledge of Drupal programming and its form api.
snippet - Change breadcrumb in Drupal
19 Sep, 2011
To change breadcrumb in module you can use two functions. You can change breadcrumbs separators or the whole logic using theme_breadcrumb in your templates template.php file.
snippet - Embed views view in Drupal 6 - 7
18 Sep, 2011
This function embeds views view in Drupal. This function doesn't display the title of the view.
snippet - Get taxonomy term depth in Drupal 7
8 Sep, 2011
How to get taxonomy term depth or level in Drupal 7.
snippet - Get vid by vocabulary name in Drupal 6
8 Sep, 2011
How to get vid - vocabulary id by vocabulary name in Drupal 6. You can get it by using this little function.
snippet - Get taxonomy vocabulary id by machine name in Drupal
20 Aug, 2011
How to get taxonomy vocabulary id(vid) by machine name in Drupal.
snippet - Set page title for view in Drupal
20 Aug, 2011
Sometimes it's hard to set the title you want for your views. So you have to do this programmatically in your module. This snippet shows you how.
snippet - Drupal 7 views prerender
17 Aug, 2011
How to edit views content before rendering the view. This is what you need in your Drupal 7 module.
snippet - Hide or remove /node page in Drupal
9 Aug, 2011
How to hide default /node page from viewers. It's useful to do this if you don't use the page. People might access to some unwanted places.
snippet - WYSIWYG module, br instead of p tags in Drupal 7
8 Aug, 2011
How to make enter insert br tag instead of p tag. Works when using WYSIWYG module and ckeditor or tinymce.
snippet - Custom admin menu, page shows child menus in Drupal
8 Aug, 2011
This menus page shows child menus under that menu item.
snippet - Change search page title in Drupal
7 Aug, 2011
This snippet shows how to change page title on search page. For this example the title will be the content of search box(search string).
snippet - Drupal include module file
4 Aug, 2011
How to include module file to anywhere. For an example if you want to spread up your functions in different files for structure and readability then this is the right way of including module files.
snippet - Print level 2 sub-menu of main menu in Drupal 7
29 Jul, 2011
How to print sub-menus or any level menus in Drupal 7. You get only sub-menus of the current page you are on.
snippet - Remove css from theme in Drupal
27 Jul, 2011
I used theme hook called theme_css_alter. There is an array where are the css files. Foreach loop to go through them and unset ones that shouldn't be there. This one goes to your themes template.php...
snippet - Menu delimiters in Drupal
22 Jul, 2011
Menu delimiters or separators in Drupal. Tested on Drupal version 6.
snippet - Group search results by content type in Drupal 7
21 Jul, 2011
By placing this code to your themes template.php file, you get search results grouped by node/content type.
snippet - Add block to anywhere in Drupal 7
19 Jul, 2011
This snippet shows you how to load a blocks content programmatically. You can use this way to display a block anywhere you want.
snippet - Redirect form submit in Drupal 7
18 Jul, 2011
This snippet shows how to redirect form after submitting and saving in Drupal 7. You can also use Rules module for this.
written tutorial - JavaScript in Drupal 7
17 Jul, 2011
JavaScript libraries, basics and examples for Drupal 7. JavaScript is provided using the Library API.
snippet - Creating roles programmatically in Drupal 7
16 Jul, 2011
This little snippet shows you how to create roles programmatically in Drupal 7.
snippet - Adding custom content types in Drupal 7
16 Jul, 2011
Creating content types programmatically in Drupal 7. If you add this code to install hook then this way you can define your own content types in a module for an example.
snippet - Save file or image programmatically in Drupal 7
10 Jul, 2011
Save file or image programmatically. First get file from directory or URL. Then save the image and assign it to the field you wish.
snippet - Drupal language switcher theme
7 Jul, 2011
How to create custom language switcher. You can write your own tags and classes to get completely custom language switcher.
snippet - Get nodes by content type in Drupal
23 Jun, 2011
How to get node ids of certain content type in Drupal.
snippet - Add dependencies to Drupal modules
21 Jun, 2011
To add add dependencies to Drupal modules you have to open your modules .info file. Add new line "dependencies[] = module_machine_name" and you are done.
snippet - Order Views Programmatically in Drupal
19 Jun, 2011
How to order views programmatically. You might need to do this on special occasions. This hook initializes before query is built so it goes for all data, not just current page.
snippet - Create Drupal Page Programmatically
17 Jun, 2011
This snippet is a code from my module that just makes a simple menu item into my admin menu in Drupal 7. I am using menu_hook function to achieve this.
snippet - Drupal - Alter Node Form
12 Jun, 2011
How to use hook_form_alter for altering node forms.
snippet - Get node path alias in Drupal
15 May, 2011
How to get node path alias in Drupal. All you need to know is node id(nid).
snippet - Add taxonomy terms in Drupal 7
14 May, 2011
How to add taxonomy terms programmatically in Drupal 7.
snippet - Drupal get comment form
10 May, 2011
How to place Drupal node comment form to anywhere in your module or theme.
snippet - ColorBox for Ajax loaded content in Drupal 7
22 Apr, 2011
ColorBox for images is loaded on first load, so all images loaded by ajax are colorBox free. To enable ColorBox for ajax loaded images you have to scan the html for images after load is complete....
snippet - Enable or disable comments using node_save in Drupal 7
21 Apr, 2011
You can enable or disable comments programmatically. Get node using node_load, set comment value and save node.
snippet - Drupal Snippet:
17 Apr, 2011
First find this line: $form['path']['pathauto_perform_alias'] = array( Then add: '#value' => 1,
written tutorial - Getting Taxonomy Terms Names in Drupal 7
6 Apr, 2011
This tutorial shows how to get taxonomy terms names in Drupal 7. First how to get all taxonomy terms, how to get categories by a parent and with a little effort, you can turn it into a treeview.
written tutorial - Create Nodes programmatically using node_save in Drupal 7
17 Mar, 2011
I needed to get some xml data and generate nodes out of them. This shows how I created nodes programmatically from my module in Drupal 7. I also suggest looking into creating a node using entity...
written tutorial - Get Drupal 7 Profile Fields with profile2_load
14 Mar, 2011
How to get profile fields in Drupal 7. Profile fields are made with Profile2 module.
written tutorial - How to Create Drupal Module
12 Mar, 2011
This beginners tutorial shows how to create a durpal module. We will set up a module, create a menu item and a "Hello World" page.
written tutorial - Get Full/Absolute URL in Drupal
11 Mar, 2011
How to get full/absolute url of the page you are currently visiting.
written tutorial - Include External JavaScript Files to Drupal
22 Feb, 2011
This Drupal API tutorial shows you how you can include external javascript files.
written tutorial - IE Only CSS Files in Drupal
29 Dec, 2010
How to make Internet Explorer only CSS files for Drupal themes.
written tutorial - Custom Node Layout in Drupal
6 Dec, 2010
This tutorial shows you how to make a custom node layout. Which is a custom placement of node fields. Also we do it without any coding.
written tutorial - Drupal Tutorial: Multiple Nodes on One Page
30 Nov, 2010
This tutorial shows how to show multiple nodes on one page. Just like front page but not with a checkbox but different content type. Views are used for this.