#nwxtech6 mickaël deffontaine - drupal api form

27
Drupal Form API #NWX Tech 6

Upload: normandie-web-xperts

Post on 31-Jul-2015

73 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: #nwxtech6 Mickaël Deffontaine - Drupal API Form

Drupal Form API

#NWX Tech 6

Page 2: #nwxtech6 Mickaël Deffontaine - Drupal API Form

QUI SUIS-JE ?

• Développeur / Intégrateur Drupal

• www.laborouge.com

• twitter.com/laborouge

2

Page 3: #nwxtech6 Mickaël Deffontaine - Drupal API Form

LE FORMULAIRE

• 1ère interactivité

• Besoin récurrent

3

Page 4: #nwxtech6 Mickaël Deffontaine - Drupal API Form

DRUPAL API FORM

• Utilisation de tableau

• Drupal se charge du reste

• Compatibilité des thèmes

• Compatibilité des modules

• PHP, AJAX & JavaScript

• Sécurité, fiabilité et accessibilité

• Site Multilingue

4

Page 5: #nwxtech6 Mickaël Deffontaine - Drupal API Form

GÉNÉRATION DU FORMULAIRE

drupal_get_form()

hook_form()

hook_form_validate()

hook_form_submit()

5

Page 6: #nwxtech6 Mickaël Deffontaine - Drupal API Form

API EN DÉTAILS

api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7

6

Page 7: #nwxtech6 Mickaël Deffontaine - Drupal API Form

UNE API RICHE

api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7

7

Page 8: #nwxtech6 Mickaël Deffontaine - Drupal API Form

FORM ELEMENTS

• Textfield / Textarea

• Password

• Select / Radios / Checkboxes

• Date

• Managed file

• Hidden

• Fieldset

• Submit

8

Page 9: #nwxtech6 Mickaël Deffontaine - Drupal API Form

PRINCIPALES PROPRIÉTÉS

• #type • #title • #description • #options • #default_value • #size • #maxlength • #attributes • #required • #weight • #prefix • #suffix

9

Page 10: #nwxtech6 Mickaël Deffontaine - Drupal API Form

HOOK_FORM()

• Construction du formulaire

• $form[]

• hook_form_alter

10

function hook_form() {

}

Page 11: #nwxtech6 Mickaël Deffontaine - Drupal API Form

EXEMPLE - TEXTFIELD

$form['mail'] = array(

'#type' => 'textfield',

'#title' => t('Mail'),

'#size' => 60,

'#maxlength' => 128,

'#required' => TRUE,

);

11

Page 12: #nwxtech6 Mickaël Deffontaine - Drupal API Form

EXEMPLE - SELECT

$form['job'] = array( '#type' => 'select', '#title' => t('Job'), '#options' => array( 'Developper' => t('Developper'), 'Web Designer' => t('Web Designer '), ), '#default_value' => t('Select a job...'), '#prefix' => '<div class="apiform clearfix">' '#suffix' => '</div>', );

12

Page 13: #nwxtech6 Mickaël Deffontaine - Drupal API Form

EXEMPLE - SUBMIT

$form['submit'] = array(

'#type' => 'submit',

'#value' => t('Send'),

'#attributes' => array(

'title' => t('Send'),

),

);

13

Page 14: #nwxtech6 Mickaël Deffontaine - Drupal API Form

FINALISATION DU FORMULAIRE

$form['#validate'][] = 'hook_validate';

return $form;

14

Page 15: #nwxtech6 Mickaël Deffontaine - Drupal API Form

HOOK_VALIDATE()

• Validation du contenu saisi

15

function hook_validate(&$form, &$form_state) {

}

Page 16: #nwxtech6 Mickaël Deffontaine - Drupal API Form

EXEMPLE – FORM_SET_ERROR

If(!valid_email_address($form_state['values']['mail'])) {

form_set_error('mail', t('Your email is not valid !!!'));

}

16

Page 17: #nwxtech6 Mickaël Deffontaine - Drupal API Form

EXEMPLE – FORM_SET_VALUE

If(empty ($form_state['values']['job'])) {

form_set_value($form['job'], t('Empty field'),

$form_state);

}

17

Page 18: #nwxtech6 Mickaël Deffontaine - Drupal API Form

HOOK_FORM_SUBMIT()

• Soumission des données

18

function hook_submit($form, &$form_state) {

}

Page 19: #nwxtech6 Mickaël Deffontaine - Drupal API Form

EXEMPLE

variable_set('mail', $form_state['values']['mail']);

variable_set('job', $form_state['values']['job']);

drupal_set_message(t('Congratulation !!!'));

drupal_mail('monformulaire', 'envoie_admin', $form_state['values'], TRUE);

19

Page 20: #nwxtech6 Mickaël Deffontaine - Drupal API Form

HOOK_MAIL()

• Génération d’un mail

20

function hook_mail(&$key, &$message) {

}

Page 21: #nwxtech6 Mickaël Deffontaine - Drupal API Form

RÉCUPÉRATION DES DONNÉES

$mail = variable_get('mail', 'Mail');

$job = variable_get('job', 'Job');

21

Page 22: #nwxtech6 Mickaël Deffontaine - Drupal API Form

GÉNÉRATION DU MAIL

switch ($key) {

case 'envoie_admin':

$message['to'] = '[email protected]';

$message['subject'] = t('Formulaire rempli');

$message['body'][1] = t('E-mail : ') . $mail;

$message['body'][2] = t('Job : ') . $job;

break;

}

22

Page 23: #nwxtech6 Mickaël Deffontaine - Drupal API Form

CAS CLIENT

• Formulaire de contact

• Réponse à une offre d’emploi

• Front-office & Back-office

23

Page 24: #nwxtech6 Mickaël Deffontaine - Drupal API Form

INTEROPÉRABILITÉ

• Mime Mail

• CAPTCHA

• Token

• Tous les modules Drupal

24

Page 25: #nwxtech6 Mickaël Deffontaine - Drupal API Form

LES ALTERNATIVES

• Webform : drupal.org/project/webform

25

Page 26: #nwxtech6 Mickaël Deffontaine - Drupal API Form

CONTACT

• www.laborouge.com

• twitter.com/laborouge

[email protected]

26

Page 27: #nwxtech6 Mickaël Deffontaine - Drupal API Form

QUESTIONS ?

27