Aggiungere campo al form per creare un nuovo contenuto

3 contenuti / 0 new
Ultimo contenuto
Aggiungere campo al form per creare un nuovo contenuto

Quando creo un nuovo contenuto per il modulo che sto sviluppando ho bisogno che venga mostrato un campo text aggiuntivo in cui inserire un'informazione da salvare nel DB.

Non voglio farlo a mano da Structure -> Content Types perché dovrà direttamente essere disponibile alle persone che vorranno usare il mio modulo, non dovranno aver bisogno di farlo a mano.

Per fare questa cosa sto provando ad usare Field API, ma per ora senza successo, non sono neanche sicuro che sia la strada giusta.

Vorrei che il campo aggiuntivo appaia sotto al campo "CSReport title" che vedete in questa immagine
csr

Il codice che sto usando al momento è il seguente, cosa c'è che non va?

<?php
function csreport_field_info()
{
    return array(
       
'csr_url' => array(
           
'label' => t('URL'),
           
'description' => t('URL a cui inviare i dati del questionario.'),
           
'settings' => array('max_length' => 255, 'required' => TRUE),
           
'instance_settings' => array('text_processing' => 0),
           
'default_widget' => 'text_textfield',
           
'default_formatter' => 'text_default',
        ),
    );
}
function
csreport_field_schema($field) {
   
$columns = array(
       
'csr_url' => array(
           
'type' => 'text',
           
'size' => 'big',
           
'not null' => FALSE,
        ),
    );
   
$indexes = array( 'csr_url' => array('csr_url') );
    return array(
'columns' => $columns, 'indexes' => $indexes );
}
function
csreport_field_formatter_info() {
    return array(
           
'text_default' => array(
           
'label' => t('Default'),
           
'field types' => array('text', 'text_long', 'text_with_summary'),
        ),
    );
}
function
csreport_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
   
$element = array();
   
$settings = $display['settings'];
    switch (
$display['type']) {
        case
'sample_field_formatter_simple':
           
// Common case: each value is displayed individually in a sub-element
            // keyed by delta. The field.tpl.php template specifies the markup
            // wrapping each value.
           
foreach ($items as $delta => $item) {
               
$element[$delta] = array('#markup' => $settings['some_setting'] . $item['value']);
            }
            break;
    }
    return
$element;
}
function
csreport_field_widget_info() {
    return array(
           
'text_textfield' => array(
                   
'label' => t('Text field'),
                   
'field types' => array('text'),
                   
'settings' => array('size' => 60),
                   
'behaviors' => array(
                           
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
                           
'default value' => FIELD_BEHAVIOR_DEFAULT,
                    ),
            ),
    );
}
function
csreport_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
   
$element += array(
           
'#type' => $instance['widget']['type'],
           
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
    );
    return array(
'value' => $element);
}
?>

Drupal Version:

Meggis (non verificato)
Ritratto di Anonimo

La riga di codice - case 'sample_field_formatter_simple': - nell'hook field_formatter_view() non si avvera mai perchè il caso 'sample_field_formatter_simple' non è definita la chiave dell'aray multidimensionale 'default_formatter' nell'hook _field_info()
Potrebbe esserti utile il modulo https://www.drupal.org/project/examples
Ciao

Ciao Unison,
come suggerisce Meggis puoi vedere come gestire i content type e relativi campi da modulo dagli esempi per sviluppatori nel sottomodulo node_example di cui trovi la documentazione ed il sorgente qui
Un'altra alternativa interessante e che ti permetterebbe di creare il tuo modulo senza scrivere codice è Features che ti permetterebbe di esportare le configurazioni e le definizioni dei tipi di contenuto e relativi campi