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.


Drupal 7: Ensure Default Display Tab Visibility in Views UI Edit Form

13 Mar, 2024
This code snippet utilizes hook_form_alter to ensure that the 'Default' display tab remains visible in the Views UI edit form of Drupal 7, allowing users to easily access and modify default display...

Drupal 7: Print all hook_menu items

12 Mar, 2024
In Drupal 7, there isn't a specific function provided by the core API to directly fetch all hook_menu() items from the database. However, you can achieve this by querying the menu_router table as...

User How to define your own custom hook_watchdog in Drupal 7

12 Dec, 2023
To define a custom hook_watchdog in Drupal 7, you'll need to create a custom module. The hook_watchdog function allows you to log messages to the watchdog database table, which is useful for...

Render html of a view in Drupal 7

23 Nov, 2023
Programmatically rendering a view in Drupal involves a structured series of steps. Initially, the target view is loaded using the views_get_view() function, specifying its machine name and the...

Delete view programmatically in Drupal 7

18 Nov, 2023
There is a function called views_delete_view, that does just that. The only thing is that views_delete_view() function in Drupal 7 takes a view object as its argument, not a machine name.

Drupal 7: Add a filter programmatically to view

27 Oct, 2023
How to add a filter programmatically to a view in Drupal 7 using the hook_views_pre_view alter hook. This is useful when you want to modify the behaviour of a view by adding a filter condition...

Different theme for anonymous user in drupal 7

26 Oct, 2023
in Drupal 7, you can achieve the goal of applying a different theme to anonymous users using Role Theme Switcher module. "Role Theme Switcher" allows you to set different themes for different user...

Entity View Alter in Drupal 7

5 Aug, 2023
In Drupal 7, you can use the hook_entity_view_alter() function to alter the rendering of entities before they are displayed. This hook allows you to modify the display of entity content without...

Create New Custom Display Drupal 7

5 Aug, 2023
To define a new custom display for an entity in Drupal 7, you'll need to create a custom module and implement hook_entity_info_alter().

Executing a Single Value Query in Drupal 7

30 Jul, 2023
To retrieve a single value from the database in Drupal 7, you can use the db_query() function along with the appropriate SQL query and utilize the fetchField() method to obtain the desired result.

Implement relationship in hook_views_data in Drupal 7

27 Jun, 2023
In Drupal 7, you can use the hook_views_data hook to define custom tables and relationships for Views. When implementing relationships, you need to define the relationship key in your hook_views_data...

Use hook menu show admin theme in Drupal 7

27 Mar, 2023
In Drupal 7, you can use the hook_menu() function to define a menu item and specify its callback function. To show a specific admin theme for a menu item, you can use the theme callback parameter in...

Is the current page an admin page in Drupal 7

8 Feb, 2023
It's preferable to determine if the page is an admin page by checking the active theme, as it provides a comprehensive solution, rather than relying on the URL path. This approach assumes that the...

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.

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.

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.

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...

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.

Drupal 7 commerce add product variation types programmatically

12 Mar, 2018
Example for how to create product variation types programmatically.

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...

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...

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...

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...

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.

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...

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.

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...

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...

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.

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.

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...

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.

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...

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.

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.

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...

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.

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.

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.

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...

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...

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...

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.

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.

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 /...

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.

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.

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://"...

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.

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.

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.

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).

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.

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.

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.

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...

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.

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.

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.

Creating views programmatically in Drupal 7

20 May, 2013
Creating views view in your module is really simple.

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.

Translate taxonomy terms in Drupal 7

3 Apr, 2013
How to translate localized taxonomy terms in Drupal 7.

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.

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.

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.

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...

Localize taxonomy term in Drupal 7

1 Dec, 2012
This snippet shows you how to Localize taxonomy term in Drupal 7.

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.

Convert plain text URLs into links in Drupal

23 Nov, 2012
How to turn plain text URLs into active links in Drupal.

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.

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.

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...

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...

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...

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...

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.

Show Drupal 404 page programmatically.

25 Jun, 2012
Show Drupal 404 page programmatically. Just return this function from your page function.

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).

Translate block title in Drupal 7

2 May, 2012
Translate block title in Drupal 7 using this theme hook.

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...

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.

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.

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.

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.

Remove language links from node links in Drupal 7

9 Feb, 2012
How to remove language links from node links in Drupal 7

Change file name after saving in Drupal 7

26 Jan, 2012
How to change file name programmatically after file is saved in Drupal 7.

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.

Remove views view filter programmatically in Drupal

16 Jan, 2012
How to remove views view filter programmatically in Drupal.

Get languages in Drupal 7

11 Jan, 2012
How to get all active languages as array in Drupal 7.

Language select field in Drupal 7

19 Dec, 2011
This code snippet is about creating languages form field in Drupal 7 using field api.

Add field to entity programmatically in Drupal 7

13 Dec, 2011
How you can add field to an entity programmatically in Drupal 7.

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.

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.

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...

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.

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.

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.

Get taxonomy term depth in Drupal 7

8 Sep, 2011
How to get taxonomy term depth or level in Drupal 7.

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.

Get taxonomy vocabulary id by machine name in Drupal

20 Aug, 2011
How to get taxonomy vocabulary id(vid) by machine name in Drupal.

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.

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.

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.

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.

Custom admin menu, page shows child menus in Drupal

8 Aug, 2011
This menus page shows child menus under that menu item.

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).

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.

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.

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...

Menu delimiters in Drupal

22 Jul, 2011
Menu delimiters or separators in Drupal. Tested on Drupal version 6.

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.

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.

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.

JavaScript in Drupal 7

17 Jul, 2011
JavaScript libraries, basics and examples for Drupal 7. JavaScript is provided using the Library API.

Creating roles programmatically in Drupal 7

16 Jul, 2011
This little snippet shows you how to create roles programmatically in Drupal 7.

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.

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.

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.

Get nodes by content type in Drupal

23 Jun, 2011
How to get node ids of certain content type in Drupal.

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.

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.

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.

Drupal - Alter Node Form

12 Jun, 2011
How to use hook_form_alter for altering node forms.

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).

Add taxonomy terms in Drupal 7

14 May, 2011
How to add taxonomy terms programmatically in Drupal 7.

Drupal get comment form

10 May, 2011
How to place Drupal node comment form to anywhere in your module or theme.

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....

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.

Drupal Snippet: "Automatic alias" checkbox checked in default

17 Apr, 2011
First find this line: $form['path']['pathauto_perform_alias'] = array( Then add: '#value' => 1,

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.

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...

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.

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.

Get Full/Absolute URL in Drupal

11 Mar, 2011
How to get full/absolute url of the page you are currently visiting.

Include External JavaScript Files to Drupal

22 Feb, 2011
This Drupal API tutorial shows you how you can include external javascript files.

IE Only CSS Files in Drupal

29 Dec, 2010
How to make Internet Explorer only CSS files for Drupal themes.

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.

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.