I started learning drupal customization, and i'm trying to create a very simply custom field for drupal.
I tried to follow several tutorial, but when i install the field (apparently without problem) it doesn't appear into the field list. But if i try to look at the source code, my field is with "hidden" attribute.
Actually i developed 2 files, the info file, and the module_file.
Here the code for module:
<?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;
}
?>
I'm not sure if the problem is a missing installation file. I tried to look at several of them but they are so different. I don't understand how to add my custom field to the database (i think it is needed). I must do a query? Or i must use some function.
I need to make a mymodule_install method? Or in that case is needed only the mymodule_field_schema? (looking at different basic module, some of them implement only that function, but other implement an insatll method, and not the field_schema).
So for example if i want to add my custom field that will be a string, and it need only a textbox what i still need to do, in order to have my field available on drupal?
Basically i don't need a new widget for my custom field, i want to use the usual Text Widget already available in drupal.