Sep 30, 2014

FormStateInterface, and drupal_valid_path()

As we move into Drupal 8, alpha 15, a few more API changes have come along for the Optimizely module.

(1)  For objects whose class implements interface FormStateInterface, you can no longer index into the object as though it were an array as in D7.

Instead, there are a number of methods to get and set values. In our case, we only need to retrieve values from simple form text fields.

So, for example,

function validateForm(array &$form, FormStateInterface $form_state)
{
  $proj_code = $form_state['values']['optimizely_project_code'];


is replaced with,

function validateForm(array &$form, FormStateInterface $form_state)
{
  $proj_code = $form_state->getValue('optimizely_project_code');


This change broke the code in a lot of the methods for validating and for submitting forms.


(2)  Regarding drupal_valid_path(), the function has been removed. So,

  if (drupal_valid_path($path, TRUE) == FALSE) {

is replaced with,

  if (\Drupal::pathValidator()->isValid($path) == FALSE) {


Sources:

Replace instances of deprecated drupal_valid_path with PathValidator
https://www.drupal.org/node/2303361






2 comments:

  1. When you say "the function has been removed" do actually mean moved / refactored / renamed?

    drupal_valid_path($path, TRUE)
    has become:
    \Drupal::pathValidator()->isValid($path)

    ReplyDelete
    Replies
    1. It's more accurate to say drupal_valid_path() has been replaced. The function takes an optional second argument to indicate wildcards -- I don't think that functionality is necessarily present in the new PathValidator class.

      Delete