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 http://optimizely-to-drupal-8.blogspot.com/2014/05/implementing-forms.html   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)) {
      $form_state->setErrorByName(

        'optimizely_id',
        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 ...'),
                       'status');    
    // Redirect back to projects listing.
    $form_state->setRedirect('optimizely.listing');
    return;
  }
}
 

Related Posts.

Drupal 8: alpha 13 --> alpha 14
http://optimizely-to-drupal-8.blogspot.com/2014/08/drupal-8-alpha-13-alpha-14.html

FormStateInterface, and drupal_valid_path()
http://optimizely-to-drupal-8.blogspot.com/2014/09/formstateinterface-and-drupalvalidpath.html

2 comments:

  1. Is this the use of Interfaces that we were talking about?

    ReplyDelete
    Replies
    1. Yep, the FormStateInterface arguments in this code are examples of the use of interfaces that we were talking about and of that kind of abstraction.

      The called functions don't know what is the actual class of the object that is passed in for the arg. They only need to know that the class implements that interface and conforms to its intent.

      Here are three of the many methods defined by FormStateInterface.

      interface FormStateInterface {
         public function &get($property);
         public function set($property, $value);
         public function has($property);
      }

      The methods do not have any bodies. It's up to each concrete class to implement them. There are copious comments about the signature of each method and what it is supposed to do.

      Delete