Aug 6, 2014

Drupal 8: alpha11 --> alpha13: AliasManager, hook_help(), getCancelRoute()

In upgrading the Optimizely module from Drupal 8, alpha 11 to alpha 13, I had to make the following changes. Yep, the D8 APIs have not been frozen!

  -----
The class Drupal\Core\Path\AliasManager has had two of its methods renamed.

getPathAlias() --> getAliasByPath()
getSystemPath() --> getPathByAlias()

For the earlier post about using the Alias Manager, see  http://optimizely-to-drupal-8.blogspot.com/2014/06/function-drupallookuppath-has-been.html

  -----
The signature for hook_help() has changed.

alpha 11:  hook_help($path, $arg)

alpha 13:  hook_help($route_name, $route_match)

In other words, the first parameter is no longer a url path. Instead, it is the name of a route as defined in the module's .routing.yml file or possibly as defined elsewhere.

Example:
function optimizely_help($route_name, $route_match) {
  switch ($route_name) {

    case 'help.page.optimizely':
      return t('Optimizely is a third party service ...');

    case 'optimizely.listing':
      return t('... A listing of the Optimizely ...');
    // Other cases ...
  }
}

Note the special route in the first case of the switch statement. It corresponds to path admin/help#optimizely for the site's general help pages.

The obsolete post on hook_help() is at  http://optimizely-to-drupal-8.blogspot.com/2014/05/hookhelp-is-same-as-in-d7.html hook_help() is no longer unchanged from Drupal 7.

  -----
Fatal error: Call to a member function toRenderArray() on a non-object in  ...\opti\core\lib\Drupal\Core\Form\ConfirmFormHelper.php on line 44

This error message came up when I clicked on a link to bring up a delete confirmation form.

After looking at the code in class ConfirmFormHelper and checking how it is used by other core classes, I changed the return value of getCancelRoute() in my own class DeleteForm like this.

Before. 

  public function getCancelRoute() {
    return array('route_name' => 'optimizely.listing');
  }


After. 

use Drupal\Core\Url;

  public function getCancelRoute() {
    return new Url('optimizely.listing');

  }

For the earlier post about creating a delete confirmation form, see http://optimizely-to-drupal-8.blogspot.com/2014/07/building-delete-confirmation-form-with.html


Sources:

function hook_help()
https://api.drupal.org/api/drupal/core!modules!system!system.api.php/function/hook_help/8

Creating a content entity type in Drupal 8
http://jmolivas.com/creating-a-content-entity-type-in-drupal-8
"Call to a member function toRenderArray() on a non-object"

Releases for Drupal core
https://www.drupal.org/node/3060/release?api_version[]=7234

1 comment: