Make Drupal 7 Development Fun
In the Drupal support world, working on Drupal 7 sites is a necessity. But switching between Drupal 7 and Drupal 8 development can be jarring, if only for the coding style.
Fortunately, I’ve got a solution that makes working in Drupal 7 more like working in Drupal 8. Use this three-part approach to have fun with Drupal 7 development:
- Apply Xautoload to keep your PHP skills fresh, modern, and compatible with all frameworks and make your code more reusable and maintainable between projects.
- Use the Drupal Libraries API to use third-party libraries.
- Use the Composer template to push the boundaries of your programming design patterns.
Applying Xautoload
Xautoload is simply a module that enables PSR-0/4 autoloading. Using Xautoload is as simple as downloading and enabling it. You can then start using use
and namespace
statements to write object-oriented programming (OOP) code.
For example:
xautoload.info
name = Xautoload Example
description = Example of using Xautoload to build a page
core = 7.x package = Midcamp Fun
dependencies[] = xautoload:xautoload
xautoload_example.module
<?php
use Drupal\xautoload_example\SimpleObject;
function xautoload_example_menu() {
$items['xautoload_example'] = array(
'page callback' => 'xautoload_example_page_render',
'access callback' => TRUE,
);
return $items;
}
function xautoload_example_page_render() {
$obj = new SimpleObject();
return $obj->render();
}
src/SimpleObject.php
<?php
namespace Drupal\xautoload_example;
class SimpleObject {
public function render() {
return array(
'#markup' => "<p>Hello World</p>",
);
}
}
Enabling and running this code causes the URL /xautoload_example to spit out “Hello World”.
You’re now ready to add in your own OOP!
Using Third-Party Libraries
Natively, Drupal 7 has a hard time autoloading third-party library files. But there are contributed modules (like Guzzle) out there that wrap third-party libraries. These modules wrap object-oriented libraries to provide a functional interface. Now that you have Xautoload in your repertoire, you can use its functionality to autoload libraries as well.
I’m going to show you how to use the Drupal Libraries API module with Xautoload to load a third-party library. You can find examples of all the different ways you can add a library in xautoload.api.php. I’ll demonstrate an easy example by using the php-loremipsum library:
1. Download your library and store it in sites/all/libraries. I named the folder php-loremipsum.
2. Add a function implementing hook_libraries_info
to your module by pulling in the namespace from Composer. This way, you don’t need to set up all the namespace rules that the library might contain.
function xautoload_example_libraries_info() {
return array(
'php-loremipsum' => array(
'name' => 'PHP Lorem Ipsum',
'xautoload' => function ($adapter) {
$adapter->composerJson('composer.json');
}
)
);
}
3. Change the page render function to use the php-loremipsum library to build content.
use joshtronic\LoremIpsum;
function xautoload_example_page_render() {
$library = libraries_load('php-loremipsum');
if ($library['loaded'] === FALSE) {
throw new \Exception("php-loremipsum didn't load!");
}
$lipsum = new LoremIpsum();
return array(
'#markup' => $lipsum->paragraph('p'),
);
}
Note that I needed to tell the Libraries API to load the library, but I then have access to all the namespaces within the library. Keep in mind that the dependencies of some libraries are immense. You’ll very likely need to use Composer from within the library and commit it when you first start out. In such cases, you might need to make sure to include the Composer autoload.php
file.
Another tip: Abstract your libraries_load()
functionality out in such a way that if the class you want already exists, you don’t call libraries_load()
again. Doing so removes libraries as a hard dependency from your module and enables you to use Composer to load the library later on with no more work on your part. For example:
function xautoload_example_load_library() {
if (!class_exists('\joshtronic\LoremIpsum', TRUE)) {
if (!module_exists('libraries')) {
throw new \Exception('Include php-loremipsum via composer or enable libraries.');
}
$library = libraries_load('php-loremipsum');
if ($library['loaded'] === FALSE) {
throw new \Exception("php-loremipsum didn't load!");
}
}
}
And with that, you’ve conquered the challenge of using third-party libraries!
Setting up a New Site with Composer
Speaking of Composer, you can use it to simplify the setup of a new Drupal 7 site. Just follow the instructions in the Readme for the Composer Template for Drupal Project. From the command line, run the following:
composer create-project drupal-composer/drupal-project:7.x-dev <YOUR SITE DIRECTORY> --no-interaction
This code gives you a basic site with a source repository (a repo that doesn’t commit contributed modules and libraries) to push up to your Git provider. (Note that migrating an existing site to Composer involves a few additional considerations and steps, so I won’t get into that now.)
If you’re generating a Pantheon site, check out the Pantheon-specific Drupal 7 Composer project. But wait: The instructions there advise you to use Terminus to create your site, and that approach attempts to do everything for you—including setting up the actual site. Instead, you can simply use composer create-project
to test your site in something like Lando. Make sure to run composer install
if you copy down a repo.
From there, you need to enable the Composer Autoload module , which is automatically required in the composer.json you pulled in earlier. Then, add all your modules to the require portion of the file or use composer require drupal/module_name
just as you would in Drupal 8.
You now have full access to all the Packagist libraries and can use them in your modules. To use the previous example, you could remove php-loremipsum from sites/all/libraries, and instead run composer require joshtronic/php-loremipsum
. The code would then run the same as before.
Have fun!
From here on out, it’s up to your imagination. Code and implement with ease, using OOP design patterns and reusable code. You just might find that this new world of possibilities for integrating new technologies with your existing Drupal 7 sites increases your productivity as well.
The post Make Drupal 7 Development Fun appeared first on Four Kitchens.