Nov 28, 2014

Beta 1 --> Beta 3: No module code changes !!

Running the Optimizely module under Drupal 8, beta 3, both manual and automated testing showed no loss of functionality or run-time errors. This is the first time that migrating to a new release of D8 did not require any code changes to the module.

All I had to do was bump up its version number. The reason I made a new release of the module is to show that it has been tested and conforms to the new D8 beta. My current convention is to incorporate the D8 version as part of the module version, like so:  8.x-2.15-beta3.

Nov 19, 2014

All blog posts have been updated for Beta 1

I have completed a review of all posts in this blog with respect to Drupal 8, beta 1, to check whether they are up to date.

Where there have been significant changes, rather than edit the original content, at the bottom of the article I added brief notes and links to newer posts that describe updates that have taken effect.

In one or two cases, there were enough code changes to warrant a fresh sample of code in a new posting.

The result is that this collection of articles remains reasonably up-to-date and accurate as we move into the beta releases of D8.

Happy Drupaling.

Nov 12, 2014

Implementing a form, D8 beta 1

I wrote an earlier post about the rudiments of how to implement a simple form for Drupal 8. There have been several related API changes since then, but that post has a number of notes that are still accurate and useful.

The original post is at   You really should read that post first, then come back to this one for the current code.

In this posting, I am only providing an update of the code sample for the class that defines the form. You may want to read the earlier post as well.

Again, this is  only an extract from the class definition. To keep it short and to the point, I've omitted parts of the bodies of the methods as well as boilerplate comments. 

Code changes from the original post are in boldface.

namespace Drupal\optimizely;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class AccountInfoForm extends FormBase {

  public function getFormID() {
    return 'optimizely_account_info';

  public function buildForm(array $form, 
                            FormStateInterface $form_state) {    
    $form['optimizely_id'] = array(      
      '#type' => 'textfield',
      '#title' => t('Optimizely ID Number'),
      '#size' => 60,
      '#maxlength' => 256,
      '#required' => TRUE,
    $form['actions'] = array('#type' => 'actions', );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Submit',

    return $form;

  public function validateForm(array &$form,
                               FormStateInterface $form_state) {
    $oid = $form_state->getValue('optimizely_id');

    if (!preg_match('/^\d+$/', $oid)) {

        t('Your Optimizely ID should be numeric.'));    }

  public function submitForm(array &$form, 
                             FormStateInterface $form_state) {
    // Code to update the database ...

    //    . . . . .

    drupal_set_message(t('The default project ...'),
    // Redirect back to projects listing.

Related Posts.

Drupal 8: alpha 13 --> alpha 14

FormStateInterface, and drupal_valid_path()

Nov 9, 2014

hook_permission() has disappeared

As of Beta 1 and possibly a bit earlier, hook_permission() is gone. In its place, there is yet another YAML file.

Drupal 7 optimizely.module file,

  function optimizely_permission() {
    return array(
      'administer optimizely' => array(
        'title' => t('Administer Optimizely module'),
        'description' =>
          t('Administer access to everything in module'),
        'restrict access' => TRUE,

Drupal 8 optimizely.permissions.yml file,

  administer optimizely:
    title: 'Administer Optimizely module'
    description: 'Administer access to everything in module'
    restrict access: true

Exactly the same pieces of information are provided in the new definition as in the old. However, there are no calls to the t() function for translation purposes. In the source article, there is a comment that "We can then make sure static permissions run strings through t()", so it sounds like the intent is for t() to be called automatically as part of the processing of the file.

Obsolete post on hook_permission() is at


Defining permissions via $module.permissions.yml

Nov 8, 2014

Beta 1 released, more changes

It's great news that Drupal 8 Beta 1 was released. It implies that the API's have reached a reasonably stable (but not immutable) state.

In moving from the final alpha 15 to beta 1, I stumbled across the following.

(1)  The url() function has been removed. In its place, you can use \Drupal::url() which takes a route name, not a path. For example, instead of


use this call,  where optimizely.settings is a route that is defined in the module's .routing.yml file.


Where the route takes parameters, pass a second argument to url() that is a keyed array which provides the names and values of those parameters. For example, for the following route,

    path: /admin/config/system/optimizely/delete/{oid}
    # The rest of the route definition ...

you can make a call such as,

  \Drupal::url('optimizely.delete.oid', array('oid' => $oid))

Caveat: you cannot use this function in hook_install(). Doing so will cause the install to bail out and leave things in a weird state that will likely leave you befuddled and frustrated. I got the generic, unhelpful message: The website has encountered an error. Please try again later.

So in hook_install() I removed the links in a message that is displayed upon completion of the installation. Some plain explanatory text is provided  instead.

(2)  The l() function takes a Url object as its second argument instead of a path. It is now in the Drupal class. Instead of

  l(t('Cancel'), 'admin/config/system/optimizely')

use something like this,

  \Drupal::l(t('Cancel'), new Url('optimizely.settings'))

However, same caveat as above. You can't use this function in hook_install() or it will bomb out and leave the install in a partially completed state. Not good.


Drupal 8.0.0 beta 1 released

Remove most remaining url() calls