Upload
normandie-web-xperts
View
73
Download
1
Embed Size (px)
Citation preview
Drupal Form API
#NWX Tech 6
QUI SUIS-JE ?
• Développeur / Intégrateur Drupal
• www.laborouge.com
• twitter.com/laborouge
2
LE FORMULAIRE
• 1ère interactivité
• Besoin récurrent
3
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
GÉNÉRATION DU FORMULAIRE
drupal_get_form()
hook_form()
hook_form_validate()
hook_form_submit()
5
API EN DÉTAILS
api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7
6
UNE API RICHE
api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7
7
FORM ELEMENTS
• Textfield / Textarea
• Password
• Select / Radios / Checkboxes
• Date
• Managed file
• Hidden
• Fieldset
• Submit
8
PRINCIPALES PROPRIÉTÉS
• #type • #title • #description • #options • #default_value • #size • #maxlength • #attributes • #required • #weight • #prefix • #suffix
9
HOOK_FORM()
• Construction du formulaire
• $form[]
• hook_form_alter
10
function hook_form() {
}
EXEMPLE - TEXTFIELD
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('Mail'),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
);
11
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
EXEMPLE - SUBMIT
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send'),
'#attributes' => array(
'title' => t('Send'),
),
);
13
FINALISATION DU FORMULAIRE
$form['#validate'][] = 'hook_validate';
return $form;
14
HOOK_VALIDATE()
• Validation du contenu saisi
15
function hook_validate(&$form, &$form_state) {
}
EXEMPLE – FORM_SET_ERROR
If(!valid_email_address($form_state['values']['mail'])) {
form_set_error('mail', t('Your email is not valid !!!'));
}
16
EXEMPLE – FORM_SET_VALUE
If(empty ($form_state['values']['job'])) {
form_set_value($form['job'], t('Empty field'),
$form_state);
}
17
HOOK_FORM_SUBMIT()
• Soumission des données
18
function hook_submit($form, &$form_state) {
}
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
HOOK_MAIL()
• Génération d’un mail
20
function hook_mail(&$key, &$message) {
}
RÉCUPÉRATION DES DONNÉES
$mail = variable_get('mail', 'Mail');
$job = variable_get('job', 'Job');
21
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
CAS CLIENT
• Formulaire de contact
• Réponse à une offre d’emploi
• Front-office & Back-office
23
INTEROPÉRABILITÉ
• Mime Mail
• CAPTCHA
• Token
• Tous les modules Drupal
24
LES ALTERNATIVES
• Webform : drupal.org/project/webform
25
QUESTIONS ?
27