Aug 22, 2014

Drupal 8: alpha 13 --> alpha 14

The story of Drupal 8 continues as it moves through alpha releases. Here are a number of changes I had to make for our module when I upgraded from alpha 13 to alpha 14.

  - - - - -
Function module_exists() has been removed. 

Instead, use \Drupal::moduleHandler()->moduleExists().

The second source article below lists a bunch of Module/hook system functions that are being removed. This leads me to believe that there may be a lot of other core hooks that are currently deprecated which will also disappear by the time the first core beta rolls out.

  - - - - -
The signatures for buildForm(), validateForm(), and submitForm() in class FormBase have changed.

Old method signatures.

  function buildForm(array $form, array &$form_state)
  function validateForm(array &$form, array &$form_state)
  function submitForm(array &$form, array &$form_state)

New signatures.

  use Drupal\Core\Form\FormStateInterface;

  function buildForm(array $form, FormStateInterface $form_state)
  function validateForm(array &$form, FormStateInterface $form_state)
  function submitForm(array &$form, FormStateInterface $form_state)


Interestingly enough, you can index into the $form_state parameter as though it is still an array even though its type is now that of an interface. For example, the following code remains unchanged.

    $oid = $form_state['values']['optimizely_oid'];

It turns out that in PHP you can iterate through the visible properties of an object using statements such as foreach.

Update as of alpha 15: you can no longer treat the $form_state param like an array. See the post  http://optimizely-to-drupal-8.blogspot.com/2014/09/formstateinterface-and-drupalvalidpath.html

  - - - - -
Method ConfirmFormBase::getCancelRoute() has been renamed to getCancelUrl()The method returns an object of class Url as previously documented.

  - - - - -
Method FormBuilder::setErrorByName() has been moved to FormStateInterface::setErrorByName() and has a different signature.

Old signature:

  function setErrorByName($name, array &$form_state, $message = '')

New signature:

  function setErrorByName($name, $message = '');

This function can be called in some methods by using the $form_state param, which is now typed as FormStateInterface. For example,

  function validateForm(array &$form, 
                        FormStateInterface $form_state) { 

    $form_state->setErrorByName('optimizely_project_code',
        $this->t('The Optimizely Account ID must be set.'));

}

  - - - - -
To redirect from a form, use FormStateInterface::setRedirect().

Previously, one way to indicate redirection after a form was submitted and processed was, for example:

  $form_state['redirect_route']['route_name'] = 'optimizely.listing';

Now redirection is provided by:

  $form_state->setRedirect('optimizely.listing');

where $form_state is typed as a FormStateInterface. 

  - - - - -
Method TestBase::randomName() has been renamed back to randomMachineName().

  - - - - -
The PHP fileinfo extension is required. The Drupal 8 installer will check for this and issue an error if the extension is not enabled.

  - - - - -

Sources:

Replace module_exists with \Drupal::moduleHandler()->moduleExists
https://www.drupal.org/node/2116375

Module/hook system functions replaced with module_handler service
https://www.drupal.org/node/1894902

Object Iteration
http://php.net/manual/en/language.oop5.iterations.php

No comments:

Post a Comment