Domanda

Ho iniziato ad imparare la personalizzazione Drupal e sto cercando di creare un campo molto semplice personalizzato per Drupal.

Ho provato a seguire diversi tutorial, ma quando installo il campo (apparentemente senza problemi) non viene visualizzato nell'elenco dei campi. Ma se provo a guardare il codice sorgente, il mio campo è con attributo "nascosto".

In realtà ho sviluppato 2 file, il file di info e il modulo_file.

Qui il codice per il modulo:

<?php
/**
 * @pricefield.module
 * add a price field.
 *
 */
 /**
 * Implements hook_field_formatter_info().
 */
function pricefield_field_formatter_info() {
  return array(
    'pricefield_custom_type' => array( //Machine name of the formatter
      'label' => t('Price'),
      'field types' => array('text'), //This will only be available to text fields
      'settings'  => array( //Array of the settings we'll create
        'currency' => '$', //give a default value for when the form is first loaded        
      ),            
    ),
  );
}
/**
 * Implements hook_field_formatter_settings_form().
 */
function pricefield_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  //This gets the view_mode where our settings are stored
  $display = $instance['display'][$view_mode];
  //This gets the actual settings
  $settings = $display['settings'];
  //Initialize the element variable
  $element = array();
  //Add your select box
  $element['currency'] = array(
    '#type'           => 'textfield',                           // Use a select box widget
    '#title'          => 'Select Currency',                   // Widget label
    '#description'    => t('Select currency used by the field'), // Helper text
    '#default_value'  => $settings['currency'],              // Get the value if it's already been set    
  );
  return $element;
}
/**
 * Implements hook_field_formatter_settings_summary().
 */
function pricefield_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('The default currency is: @currency ', array(
    '@currency'     => $settings['currency'],    
  )); // we use t() for translation and placeholders to guard against attacks
  return $summary;
}
/**
 * Implements hook_field_formatter_view().
 */
function pricefield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array(); // Initialize the var
  $settings = $display['settings']; // get the settings
  $currency = $settings['currency']; // Get the currency  
  foreach ($items as $delta => $item) {
    $price = $item['safe_value']; // Getting the actual value
  }  
  if($price==0){
      $element[0] = array('#markup' => 'Free');
  } else {
      $element[0] = array('#markup' => $currency.' '.$price);
  }
  return $element;
}
?>
.

Non sono sicuro se il problema è un file di installazione mancante. Ho provato a guardarli molti, ma sono così diversi. Non capisco come aggiungere il mio campo personalizzato al database (penso che sia necessario). Devo fare una query? O devo usare qualche funzione.

Ho bisogno di creare un metodo mymodule_install? O in tal caso è necessario solo il mymodule_field_schema? (Guardando il diverso modulo di base, alcuni di loro implementano solo quella funzione, ma altri implementano un metodo Insatll, e non il campo_schema).

Quindi, ad esempio se voglio aggiungere il mio campo personalizzato che sarà una stringa, e necessita solo di una casella di testo ciò che ho ancora bisogno di fare, per avere il mio campo disponibile su Drupal?

Fondamentalmente non ho bisogno di un nuovo widget per il mio campo personalizzato, voglio usare il solito widget di testo già disponibile in Drupal.

È stato utile?

Soluzione

Se ti capisco bene devi implementare hook_field_widget_info_alter() e dica Drupal che il widget di Textfield può essere utilizzato dal campo:

function pricefield_field_widget_info_alter(&$info) {
  // 'pricefield' will be whatever the machine name of your field is
  $info['text_textfield']['field types'][] = 'pricefield';
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top