Creazione di custom element in un form
Ho trovato un tutorial per creare un elemento personalizzato per drupal 6 al seguente link:
http://www.akchauhan.com/create-drupal-form-elements-like-date-element/
Ed ho tentato di adattare il codice per drupal 7 nel seguente modo:
<?php
function calendartry2_element_info() {
return array(
'height' => array(
'#input' => TRUE,
'#process' => array('height_process'),
'#element_validate' => array('height_validate'),
),);
}
function height_process($element) {
// Assigning default values to element. You can override these values by passing your own values using "#default_value".
if (empty($element['#value'])) {
$element['#value'] = array(
'feet' => '-',
'inch' => '-',
);
}
$element['#tree'] = TRUE;
$parents = $element['#parents'];
$parents[] = 'feet';
$element['feet'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['feet'],
'#options' => array('-' => '-', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8',),
'#suffix' => t('ft.'),
);
$parents = $element['#parents'];
$parents[] = 'inch';
$element['inch'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['inch'],
'#options' => array('-' => '-', '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6',
'7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11'),
'#suffix' => t('in.'),
);
return $element;
}
function height_validate($element) {
if (trim($element['#value']['feet']) == '-' || trim($element['#value']['inch']) == '-') {
form_error($element, t('The specified height is invalid.'));
}
}
function theme_height($element) {
return theme('form_element', $element, '<div class="container-inline">'. $element['#children'] .'</div>');
}
function calendartry2_theme() {
return array(
'height' => array(
'variables' => array('element' => NULL))
);
}
?>inserendo nella funzione hook_menu() il seguente codice per poter richiamare l'oggetto:
<?php
// Create element of 'type' height
$form['altezza'] = array(
'#type' => 'height',
'#title' => t('Height'),
//'#required' => TRUE,
'#default_value' => array('feet' => '-', 'inch' => '-'),
);
?>Drupal 7 non mi rileva nessun errore ma non mi visualizza comunque nulla. Avete qualche consiglio da darmi?
Grazie in anticipo.

Risposte
io personalmente non ho ben
io personalmente non ho ben capito cosa vorresti fare.
di mio so solo che esistono diversi moduli per creare campi personalizzati per i form.
sicuro di aver controllato bene la disponibilità nell'infinita sezione Modules di drupal.org?
Grazie per la risposta. si ho
Grazie per la risposta.
si ho controllato.
In pratica il campo personalizzato consiste in due select che permettono all'utente di selezionare l'altezza.
Il problema è che, allo stato attuale, una select si trova su un rigo e un'altra al rigo successivo.
Voglio inserirle sullo stesso rigo, ed evidentemente c'è qualche problema nelle funzioni theme_height e calendartry2_theme.
Potete aiutarmi?
Ciao, prova a mettere
Ciao, prova a mettere l'elemento che hai creato non nell'hook_menu ma in una funzione che restituisce un form. Ti posto un esempio:
<?php//hook_menu
function nomemodulo_menu(){
$items['element-test'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('nomemodulo_test_form'),
'access arguments' => array('access content'),
);
return $items;
}
//costruzione form
function nomemodulo_test_form(){
// tuo elemento
$form['altezza'] = array(
'#type' => 'height',
'#title' => t('Height'),
'#default_value' => array('feet' => '-', 'inch' => '-'),
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
return $form;
}
?>
Vedi:
http://api.drupal.org/api/drupal/modules--system--system.api.php/functio...
e
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
Grazie per la risposta. Dopo
Grazie per la risposta.
Dopo varie prove è apparso l'elemento custom (l'altezza) dopo che ho 'staccato' e 'riattaccato' il modulo (perdonate i termini). Tuttavia i due elementi di tipo select vengono mostrati su due righe diverse. Il codice è il seguente:
<?php
function calendartry2_element_info() {
return array(
'height' => array(
'#input' => TRUE,
'#title' => t('Ora di un evento'),
'#process' => array('height_process'),
'#element_validate' => array('height_validate'),
'#theme' => array('height'),
),);
}
function height_process($element) {
// Assigning default values to element. You can override these values by passing your own values using "#default_value".
if (empty($element['#value'])) {
$element['#value'] = array(
'feet' => '-',
'inch' => '-',
);
}
$element['#tree'] = TRUE;
$parents = $element['#parents'];
$parents[] = 'feet';
$element['feet'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['feet'],
'#options' => array('-' => '-', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8',),
'#suffix' => t('ft.'),
);
$parents = $element['#parents'];
$parents[] = 'inch';
$element['inch'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['inch'],
'#options' => array('-' => '-', '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6',
'7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11'),
'#suffix' => t('in.'),
);
return $element;
}
function height_validate($element) {
if (trim($element['#value']['feet']) == '-' || trim($element['#value']['inch']) == '-') {
form_error($element, t('The specified height is invalid.'));
}
}
function theme_height($element) {
return theme('form_element', $element, '<div>'. $element['#children'] .'</div>');
}
function calendartry2_theme() {
return array(
'height' => array(
'variables' => array('element' => NULL),
)
);
}
?>
mentre nella funzione hook_menu() troviamo:
<?php$form['altezza'] = array(
'#type' => 'height',
'#title' => t('Height'),
'#required' => TRUE,
'#default_value' => array('feet' => '-', 'inch' => '-'),
);
?>
Quindi il problema si riduce ad una domanda:
Come faccio a mostrare due elementi di tipo select sulla stessa riga?
Ragionando il problema si riscontra analizzando due funzioni:
calendartry2_theme() e theme_height() dove calendartry2 è il nome del modulo e height è il nome dell'elemento.
Tramite la documentazione online ho scoperto che non c'è nessun tema chiamato 'form_element' nè in drupal 6 nè 7.
Ho cercato anche di utilizzare il tema 'table' ma senza risualtati. Qualche idea?
Grazie in anticipo
Ciao, potresti aggiungere
Ciao, potresti aggiungere '#theme_wrappers' al tuo elemento 'height' così ad esempio
<?php...
'#type' => 'height'
'#theme_wrappers' => array('nomemodulo_theme_element'),
...
?>
poi registri la funzione di theming e la implementi aggiungendo un wrapper attorno all'elemento con classe "container-inline":
<?phpfunction nomemodulo_theme(){
return array (
'nomemodulo_theme_element' => array(
'render element' => 'element',
),
);
}
function theme_nomemodulo_theme_element($variables){
$variables['element']['#children'] = '<div class="container-inline">'. $variables['element']['#children'] .'</div>';
return theme('form_element', $variables['element']);
}
?>
tale classe css è presente in system.css (chiaramente non deve essere sovrascritta da altre regole presenti nel tema) e renderà "inline" i label e div "figli" dell'elemento.
Spero ti sia utile.
Mi dispiace ma non appena
Mi dispiace ma non appena inserisco le modifiche che mi hai consigliato l'elemento custom scompare:
Il codice è il seguente:
<?php
function calendartry2_element_info() {
return array(
'height' => array(
'#input' => TRUE,
'#process' => array('height_process'),
'#element_validate' => array('height_validate'),
'#theme_wrappers' => array('nomemodulo_theme_element'),
),);
}
function height_process($element) {
// Assigning default values to element. You can override these values by passing your own values using "#default_value".
if (empty($element['#value'])) {
$element['#value'] = array(
'feet' => '-',
'inch' => '-',
);
}
$element['#tree'] = TRUE;
$parents = $element['#parents'];
$parents[] = 'feet';
$element['feet'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['feet'],
'#options' => array('-' => '-', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8',),
'#suffix' => t('ft.'),
);
$parents = $element['#parents'];
$parents[] = 'inch';
$element['inch'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['inch'],
'#options' => array('-' => '-', '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6',
'7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11'),
'#suffix' => t('in.'),
);
return $element;
}
function height_validate($element) {
if (trim($element['#value']['feet']) == '-' || trim($element['#value']['inch']) == '-') {
form_error($element, t('The specified height is invalid.'));
}
}
function calendartry2_theme(){
return array (
'nomemodulo_theme_element' => array(
'render element' => 'element',
),
);
}
function theme_calendartry2_theme_element($variables){
drupal_set_message("Funzione chiamata");
$variables['element']['#children'] = '<div class="container-inline">'. $variables['element']['#children'] .'</div>';
return theme('form_element', $variables['element']);
}
?>
mentre nell'hook_menu() è presente:
<?php// Create element of 'type' height
$form['altezza'] = array(
'#type' => 'height',
'#title' => t('Height'),
'#required' => TRUE,
'#default_value' => array('feet' => '-', 'inch' => '-'),
'#theme_wrappers' => array('calendartry2_theme_element'),
);
?>
Ho fatto qualche errore?
Ciao, prova a sostituire le
Ciao, prova a sostituire le occorrenze di "nomemodulo" con il nome del modulo che hai creato che penso sia "calendartry2", ripulire la cache e riprovare.
Perdona il mio errore da
Perdona il mio errore da copia-incolla. Effettuando le giuste modifiche, l'elemento scompare dal form. Al contrario, eliminando le funzioni che determinano il tema, l'elemento compare ma su due righe diverse.
Il codice è il seguente:
<?php
function calendartry2_element_info() {
return array(
'height' => array(
'#input' => TRUE,
'#process' => array('height_process'),
'#element_validate' => array('height_validate'),
),);
}
function height_process($element) {
// Assigning default values to element. You can override these values by passing your own values using "#default_value".
if (empty($element['#value'])) {
$element['#value'] = array(
'feet' => '-',
'inch' => '-',
);
}
$element['#tree'] = TRUE;
$parents = $element['#parents'];
$parents[] = 'feet';
$element['feet'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['feet'],
'#options' => array('-' => '-', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8',),
'#suffix' => t('ft.'),
);
$parents = $element['#parents'];
$parents[] = 'inch';
$element['inch'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['inch'],
'#options' => array('-' => '-', '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6',
'7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11'),
'#suffix' => t('in.'),
);
return $element;
}
function height_validate($element) {
if (trim($element['#value']['feet']) == '-' || trim($element['#value']['inch']) == '-') {
form_error($element, t('The specified height is invalid.'));
}
}
function calendartry2_theme(){
return array (
'calendartry2_theme_element' => array(
'render element' => 'element',
),
);
}
function theme_calendartry2_theme_element($variables){
drupal_set_message("Funzione chiamata");
$variables['element']['#children'] = '<div class="container-inline">'. $variables['element']['#children'] .'</div>';
return theme('form_element', $variables['element']);
}
?>
Nella funzione hook_form() abbiamo:
<?php$form['altezza'] = array(
'#type' => 'height',
'#title' => t('Height'),
'#required' => TRUE,
'#default_value' => array('feet' => '-', 'inch' => '-'),
'#theme_wrappers' => array('calendartry2_theme_element'),
);
?>
Ripulisco la cache del browser ogni volta. Ho notato che ci sono anche tabella, nella base di dati di Drupal chiamate Cache. Devo effettuare qualche modifica anche lì?
Come sempre ti ringrazio per la tua disponibilità.
Ringrazio sentitamente
Ringrazio sentitamente l'utente blackice78 per la sua disponibilità. Il codice che ho postato nel mio ultimo post è CORRETTO.
Non funzionava fino ad ora perchè occorre 'staccare' e 'riattaccare' il modulo.
Ciao, per "pulire la cache"
Ciao, per "pulire la cache" in effetti intendevo quella di Drupal (dalla pagina admin/config/development/performance) per ricostruire menù e registro del tema, non quella del tuo browser.
A presto.