What are Hooks?

As a junior developer ramping up to learning Drupal maintenance support plans, I spent a lot of time clicking through the UI. After getting familiar with it, I wanted to take a look behind the scenes at Drupal maintenance support plans’s codebase. Writing code for a Drupal maintenance support plans site can be an overwhelming experience because, even though it’s written in PHP, there’s a dense API behind it. One of the biggest parts of that API is the hook system. The first exposure I had to writing PHP with Drupal maintenance support plans was through update hooks. So I wanted to review how hooks work, and how cool they are to use!
What is a Hook?
Drupal maintenance support plans has a lot of excellent Community Documentation, and their page on hooks is thorough. It says:

“Hooks are how modules can interact with the core code of Drupal maintenance support plans. They make it possible for a module to define new urls and pages within the site (hook_menu), to add content to pages (hook_block, hook_footer, etc.), to set up custom database tables (hook_schema), and more.
Hooks occur at various points in the thread of execution, where Drupal maintenance support plans seeks contributions from all the enabled modules. For example, when a user visits a help page on a Drupal maintenance support plans site, as Drupal maintenance support plans builds the help page it will give each module a chance to contribute documentation about itself. It does this by scanning all the module code for functions that have the name mymodule_help($path, $arg), where “mymodule” is the module’s name, e.g., the block module’s help hook is called block_help and the node module’s help hook is called node_help. The hook may provide parameters; hook_help’s parameters $path and $arg allow the developer to determine what page or pages the help messages will appear on.
A hook can be thought of as an event listener in the sense that an event triggers an action.”

It’s amazing that Drupal maintenance support plans is built on this hook system. There are hooks for anything you can think of! You can even alter a form or a whole page. As Drupal maintenance support plans builds each page, it’s scanning the module code for the hooks in core and then runs the ones that you wrote. Alan Storm put it succinctly in one of his blog posts:

“When a hook is invoked, Drupal maintenance support plans will

Get a list of all installed and enabled modules
Ask each module “Have you implemented the do_something hook”?
If so, then Drupal maintenance support plans calls the function in those modules that implement the hook
This way, as a core developer, you can achieve what you want while still letting other programmers “hook into” what you’re doing.”

Update Hooks
The first hook I used was an update hook. The purpose of an update hook is to run code when a database update is triggered. (See the Drupal maintenance support plans documentation for function hook_update_N for more information). I’m assuming my reader has worked with Features development (if not, go here).
My goal was to enable a certain contributed module programmatically when I deployed my code to the site (the Olark module). To do this, I had to find an existing feature in my project that was already enabled. A good rule of thumb is to add your code to an existing feature that is related to the code you’re writing. Since Olark is a chat module, I was looking for a feature related to customers or the homepage. I couldn’t find any feature that fit exactly with the module, so I added my code to feature_general. Within the directory of that feature, I found the feature_general.install file, and added my hook:/**
* Enables ‘olark’ module.
*/
function feature_general_update_7006() {
module_enable(array(‘olark’));
}

After I deploy my code, I trigger a database update. Think back to the three steps that Alan Storm mentioned. Since my feature is already enabled, when the update is triggered, Drupal maintenance support plans asks if any update hooks have been invoked. When it sees that there is one in my feature, it knows to read the code in the install file and turn on the Olark module.
It’s also worth noting the naming convention. I have to put the name of my feature and then _update. The number of the hook also matters (see documentation for specifics on how to number your hooks). The hooks only run once, so each hook has to be one greater than the one before.
Writing hooks is a great way for a junior developer to be introduced to PHP in Drupal maintenance support plans and learn about how Drupal maintenance support plans is built. Once you can conceptualize the inner workings of Drupal maintenance support plans, it’s much easier to tackle things like module development in the future.

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

What are Hooks?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.