Blog
Working with a Database in Drupal 7 - Lesson 9 - UPDATE Requests
Update queries should always use the query builder. Different databases have specific handlers for LOB (Large Object, such as TEXT
in MySQL) and BLOB (Binary Large Object) fields, so an abstraction layer is required for individual database drivers to implement these specifics.
Update queries must start with the db_update()
function:
Working with Database in Drupal 7 - Lesson 10 - Delete Requests (DELETE)
Delete queries should use the query builder. They begin with the db_delete()
function:
<?php $query = db_delete('node', $options); ?>
This delete query will remove records from the node
table. Note that you do not need to wrap the table name in curly braces—Drupal's query builder handles that automatically. Delete queries use a Fluent API, meaning all methods (except execute()
) return the query object itself, just like update and insert queries.
Working with Database in Drupal 7 - Lesson 11 - MERGE Queries
Merge queries are a special hybrid type of database query. While the syntax for these queries was defined in SQL 2003, few databases actually support it. However, most databases offer an alternative implementation using specific syntax. The merge query builder in Drupal abstracts the concept of a merge query into an object structure, so it can be compiled appropriately for each database based on its own syntax.
Working with a Database in Drupal 7 - Lesson 12 - Query Conditions (WHERE, HAVING, LIKE)
The WHERE clause in a query allows you to select only those records that meet certain conditions—for example, nodes created no earlier than two weeks ago, or taxonomy terms containing the word “Drupal”. In SQL, we use WHERE and HAVING to specify conditions in SELECT, UPDATE, and DELETE queries. In Drupal’s dynamic queries, there's a built-in mechanism for handling query conditions, and it works the same across SELECT, UPDATE, and DELETE operations.
Conditional Expressions Concept
Conditions are represented as special expressions that define constraints.
Creating a module on Drupal, quick start
Let's start with api.drupal.org. Open the page for the hook_node_validate() hook, which is triggered before a node is saved:
http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_validate/7
Hooks allow us to inject our custom code into the normal Drupal process—adding validations, data fields, form elements, etc.
What does a Drupal 7 module consist of?
Before we start creating our module, I want to share a bit more about the capabilities of the Drupal API. The API provides powerful tools for working with taxonomy, nodes, users, and database input/output. To allow communication between modules and the Drupal core—or between different modules—Drupal provides a hook system. A hook is a callback: when code execution reaches a hook, our module’s function is included in the execution flow. This allows us to process user data, menus, taxonomy, and content types.
Visit:
Drupal 7 hook_block_info() and hook_block_view() display information in the block
In the previous lesson, we created a module for Drupal 7. In this lesson, we will continue to enhance our module. We’ll add a block output using hook_block_info()
and hook_block_view()
. This block will display the latest users on the site, each with a link to their profile page.
Let's start with a description of hook_block_info():
Defines all the blocks created by the module.
This hook tells Drupal which blocks the module will display and can also describe block configuration options.
Hook_menu Drupal 7 creating pages via module
In the previous lesson, we looked at how to display information from the database using the Drupal API, specifically the hooks hook_block_info() and hook_block_view(). In this lesson, we will display pages using the hook_menu() hook to connect a page to other parts of Drupal such as menus, the translation module, templates, and more.
hook_permission Drupal 7 access permissions for different roles
In previous lessons, we displayed pages and blocks in various places, and we also used the properties 'access arguments' and 'access callback' to set access restrictions for pages. In this lesson, we’ll create a more flexible way to manage access rights to operations via administration pages. For this, we’ll use hook_permission() (in Drupal 6, it was hook_perm()).
hook_permission()
Defines permissions for users.
Form API Drupal 7 creating forms on Drupal
In previous lessons, we got acquainted with the hooks hook_block_info(), hook_block_view(), hook_menu(), hook_permission(), and now we can programmatically create as many pages and blocks as we need. In this lesson, we’ll explore the Drupal 7 Form API for creating forms. We'll build a form for administering module functionality and try to use as many of the hooks we've already learned to solidify our skills.
To start, we will display 3 blocks. I believe this won’t be difficult for you.