D7 - Modulo e Node Type - Creare elemento list_text

4 contenuti / 0 new
Ultimo contenuto
D7 - Modulo e Node Type - Creare elemento list_text

Salve,
ho Drupal 7,
sto creando un nuovo Node Type, per farlo ho scelto di scrivere un modulo per problemi di organizzazione del mio progetto.

Ho bisogno di aiuto per definire un campo di tipo list_text:

    function _mdulo_installed_fields() {
       $t = get_t();
       return array(
          'modulo_materiale' => array(
             'field_name' => 'modulo_materiale',
             'label'         => $t('Materiale'),
             'type'          => 'list_text',
          ),
       );
    }

Come definisco le voci e i valori del menu a tendina?

Grazie per l'aiuto

Per creare un nuovo tipo di contenuto via codice devi utilizzare le Form API.

Sempre nel sito api.drupal.org c'è una quick start guide sull'utilizzo di queste api e esiste anche il modulo examples che contiene del codice di esempio appunto relativamente alla creazione di nodi e di form.

Ho trovato questo, devo ancora probarlo però:

field_create_field(
  array(
    'field_name' => 'my_field_name',
    'type' => 'list_text',
    'settings' => array(
      'allowed_values' => array('yes' => t('Yes'), 'no' => t('No')),
    ),
  )
);
field_create_instance(
  array(
    'field_name' => 'my_field_name',
    'entity_type' => 'node',
    'bundle' => 'page',
    'required' => TRUE,
    'default_value' => array('yes'),
  )
);

ciao, io son riuscito attraverso il seguente codice da inserire nella funzione MIOMODULO_install(), l'unica cosa che non riesco a far andare sono però i valori di default:

<?php
  $default_notification_field_name
= "field_notification_defaults2";
  if(!
field_info_field($default_notification_field_name))   { // check if the field already exists.
       
field_create_field(
            array(
               
'field_name' => $default_notification_field_name,
               
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
               
'type' => 'list_text',
               
'settings' => array(
                   
'allowed_values' => array(
                       
'group' => t('Groups to which you subscribe'),
                       
'content' => t('Authored content'),
                       
'comment' => t('Replies to your comments')
                    )
                )
            )
        );
       
field_create_instance(
            array(
               
'field_name' => $default_notification_field_name,
               
'entity_type' => 'user',
               
'bundle' => 'user',
               
'label' => t('Default settings for notifications'),
               
'description' => t('Select the ​​default notification options for the groups to which you subscribe, the contents you send and the replies to (your) comments. These default options can then be modified on a per-group, content or comment bases.'),
               
'required' => FALSE,
               
'widget' => array(
                   
'type' => 'options_buttons',
                ),
               
'default_value' => array('content','comment')
            )
        );
  }
?>