| hook_boot() | hook_init() |
|---|---|
| hook_boot() will executes even on cached pages | hook_init() will not executes on cached pages |
| hook_boot() is called before modules or most include files are loaded into memory (1.e., This hook will called while Drupal is still in bootstrap mode). | hook_init(),is called After all modules are loaded into memory. |
| It happens while Drupal is still in bootstrap mode. | It happens after bootstrap mode. |
Showing posts with label Drupal 7. Show all posts
Showing posts with label Drupal 7. Show all posts
Monday, January 21, 2019
Difference between hook_boot and hook_init Drupal
Friday, April 21, 2017
Drupal 7 SQL cheat sheet
I'm currently developing a new Drupal 7 application (technically built as a new Drupal module), and as I'm building the application, I've made notes on database queries I've made.
The approach I'm taking is to use the following Drupal SQL query functions. As you can see from the function names, there are now different Drupal functions for each query type:
- SELECT - use db_query
- INSERT - use db_insert
- UPDATE - use db_update
- DELETE - use db_delete
Note that you can also execute some of these queries with the drupal_write_record function, but I've been having problems getting that to work, so I'm using these functions instead.
Given that background, here is my Drupal 7 SQL cheat sheet, with SQL SELECT, INSERT, UPDATE, and DELETE examples.
Drupal 7 SQL SELECT example - one row returned
Here's one way to execute a Drupal 7 SQL SELECT statement with the db_query function:
$q = "SELECT * FROM {projects} WHERE id = :id AND user_id = :uid";
$project = db_query($q, array(':id' => $id, ':uid' => $uid))
->fetchObject();
# returns an object with fields corresponding to the names
# of your database table, which you access like
# $project->name, $project->description, and so on.
Because I'm limiting this query by the 'id' field, I know I'm only going to get one record back. (In fact it may fail to find any records, but I'm trying to keep this simple.)
As a better example of how db_query() returns an object, here's another example:
# make sure the user owns the project id
$q = "SELECT user_id FROM {projects} WHERE id = :project_id";
$result = db_query($q, array(':project_id' => $project_id))->fetchObject();
echo 'user_id is ' . $result->user_id;
As you can see, I treat $result as an object after the query, knowing that it will have a field named user_id, because that's the name of the database table field I'm asking for in my query.
Drupal 7 SQL SELECT example - multiple rows returned
Here's how you issue a Drupal 7 SQL SELECT query with db_query and handle the results when you are expecting many rows to be returned:
$q = "SELECT * FROM {projects} WHERE uid = :uid";
$result = db_query($q, array(':uid' => $uid));
foreach ($result as $row) {
// loop through your result set, working with $row here
}
Drupal 7 SQL INSERT with db_insert
How to execute a SQL INSERT with db_insert:
$id = db_insert('projects')
->fields(array(
'user_id' => $uid,
'project_type' => $project_type,
'name' => $name,
'description' => $description,
'last_updated' => $timestamp,
'date_created' => $timestamp
))
->execute();
With a SQL INSERT like this, the db_insert function returns the value of the SERIAL (auto_increment) field, which is very nice.
Drupal 7 SQL UPDATE with db_update
How to execute a Drupal 7 SQL UPDATE with db_update:
db_update('projects')
->fields(array(
'project_count_type' => $project_count_type,
'name' => $name,
'description' => $description,
'last_updated' => $timestamp,
)
)
->condition('id', $id)
->execute();
Drupal 7 SQL DELETE with db_delete
How to execute a Drupal 7 SQL DELETE with db_delete:
db_delete('projects')
->condition('id', $id)
->execute();
Drupal 7 SQL cheat sheet - Summary
As you have seen, you use these Drupal 7 functions for the corresponding query types:
- SELECT - use db_query
- INSERT - use db_insert
- UPDATE - use db_update
- DELETE - use db_delete
I hope these Drupal 7 SQL query examples have been helpful. I'll try to keep updating this cheat sheet as I learn more.
Sunday, January 22, 2017
Drupal Console commands
$ drupal list
Drupal Console version 0.6.5
Available commands:
cr Rebuild and clear all site caches.
drush Run drush from console.
help Displays help for a command
list Lists commands
self-update Update the console to latest version.
cache
cache:rebuild Rebuild and clear all site caches.
config
config:debug Show the current configuration.
container
container:debug Displays current services for an application.
generate
generate:command Generate commands for the console.
generate:controller Generate & Register a controller
generate:entity:config Generate a new "EntityConfig"
generate:entity:content Generate a new "EntityContent"
generate:form:config Generate a new "ConfigFormBase"
generate:module Generate a module.
generate:plugin:block Generate a plugin block
generate:plugin:imageeffect Generate image effect plugin.
generate:plugin:rest:resource Generate plugin rest resource
generate:service Generate service
migrate
migrate:debug Display current migration available for the application
migrate:execute Execute a migration available for application
module
module:debug Display current modules available for application
module:download Install module or modules in the application
module:install Install module or modules in the application
module:uninstall Install module or modules in the application
router
router:debug Displays current routes for the application
router:rebuild Rebuild routes for the application
Source : http://enzolutions.com/articles/2015/01/25/what-is-drupal-console-for-me/
Source : http://enzolutions.com/articles/2015/01/25/what-is-drupal-console-for-me/
Sunday, January 1, 2017
Drupal 7 HTTPS Setup
Change the Base URL in Settings.php
$base_url = 'https://domainname.com';
and add the code in .htaccess
# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Clear the cache and check it
$base_url = 'https://domainname.com';
and add the code in .htaccess
# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Clear the cache and check it
Subscribe to:
Posts (Atom)
Difference between hook_boot and hook_init Drupal
hook_boot() hook_init() hook_boot() will executes even on cached pages hook_init() will not executes on cached pages hook_boot() is ...
-
Hello Developers, Laravel 5 Implemented routes as sepeately, previously we have in app/Http/routes.php in Laravel 4. In Laravel 5 maint...
-
# Revert changes to modified files. git reset --hard # Remove all untracked files and directories. (`-f` is `force`, `-d` is `remove di...