Skip to main content
Start of main content.

Drupal Development

Object-oriented page callbacks for Drupal 7

In Drupal we use object-oriented page and form callbacks to ease our programmning burden This is a nice improvement that allows us to encapsulate the functionality of one or many page callbacks into objects, with all the benefits that brings. Is it possible for us to us object-oriented page callbacks in Drupal 7? With a few tricks, yes it is. This article shows you how.

This is part of a continuing series of using Drupal 8 programming techniques in Drupal 7.

Drupal consultant

Drupal 8 Now: PSR-0 code in Drupal 7

Drupal 8 embraces modern PHP with all the trimmings, shedding the baggage of supporting earlier PHP versions and embracing the new object-oriented features.

One such feature is namespaced objects and the PSR-0 standard for autoloader compatability.

But if you know your project will run on a recent version of PHP, there's no reason you can't write your custom modules using PSR-0 now, in Drupal 7

Come sprint with us on Drupal 8 Contrib at Drupal South

Friday February 14th is the DrupalSouth Code Sprint, and PreviousNext are descending en masse to Wellington, New Zealand, to participate.

As a team we've been discussing what we'd like to sprint on. We've collectively agreed that the sprint would be an opportune time to work on porting some of our favourite contrib modules to Drupal 8.

Read on to find out our plans and how you can get involved.

Dynamic Routes in Drupal 8 with a RouteSubscriber

Previously I have demonstrated how to create a new route controller in Using Drupal 8's new route controllers then how to restrict access to it in Controlling Access to Drupal 8 Routes with Access Checks. But that's not where the fun ends!

What about when we need to create a route dynamically. For example, if we need to create routes for content types that we don't know will exist in advance?  In Drupal 7, we created dynamic routes with a foreach loop in hook_menu(). In Drupal 8, we can do all this and more with a RouteSubscriber.

Introducing the EntityFormController

Drupal 8 comes with many new concepts. A lot of work has gone into expanding on Entities and forms. One such concept is the EntityFormController. The EntityFormController is a controller class for the management of forms for entities. Say for example your module Foo defines an entity Bar. Say you want to provide a form for this entity so that users can set some properties or field values. In the past you would have created a hook_menu item to a drupal_get_form callback. In Drupal 8 we do it a better way. We define a class which implements the EntityFormController. The base class provides some pre-built entity goodness, and we can extend the class to add whatever we need.

Senior Developer

Controlling Access to Drupal 8 Routes with Access Checks

In the previous post, I looked at how to put together a basic route controller in Drupal 8, and restrict access by specifying permissions. But there are my situations where basic permissions aren't enough.

In Drupal 7 we had procedural access callbacks. In Drupal 8 we now have AccessCheck services.

This post takes you through how to use the new AccessCheck interface to provide a custom access checker for your routes.

Using Drupal 8's new route controllers

As part of the Web Services and Context Core Initiative, traditional procedural page callbacks were converted to shiny new Object Oriented route controllers. 

In this post, we cover the basics of creating a route controller, and how to pass in dependencies using dependency injection.

In following posts, we'll look at how to convert Drupal 7 custom access callbacks, to the new AccessCheckInterface, as well as dynamic routes.

Understanding Drupal 8's Modal API and Dialog Controller

It is common knowledge that Drupal 8 contains Views module, thanks to the work from the Views in Drupal Core (VDC) initiative.

Our contribution to the VDC initiative was working on abstracting Views UI's modal pattern into a generic modal API in core.

Recently one of the coolest pieces of this API was committed to core.

You can now load any content in a modal simply by adding a class and an attribute to any link.

Sound cool? Read on to learn more.