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

  url('/admin/config/system/optimizely/settings')

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

  \Drupal::url('optimizely.settings')

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,

  optimizely.delete.oid:
    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.


Sources:

Drupal 8.0.0 beta 1 released
https://www.drupal.org/drupal-8.0.0-beta1

Remove most remaining url() calls
https://www.drupal.org/node/2340251


2 comments:

  1. I don't remember url() in use in the D7 version of the Optimizely module. Perhaps you mean l() rather than url()?
    https://api.drupal.org/api/drupal/includes%21common.inc/function/l/7

    ReplyDelete
    Replies
    1. You're right, there's only one use of url() in the D7 code, and it's in the implementation of hook_help(), whereas the l() function is used in a number of places.

      It looks like I had earlier introduced several uses of url() myself while refactoring the theming code to work with D8.

      Delete