Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @pricefield.module
- * add a price field.
- *
- */
- /**
- * Implements hook_field_formatter_info().
- */
- function pricefield_field_info() {
- return array(
- 'pricefield_format' => 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
- ),
- 'default_widget' => 'pricefield_field',
- 'default_formatter' => 'pricefield_format',
- ),
- );
- }
- /**
- * 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;
- }
- function pricefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0, $element){
- if ($instance['widget']['type'] == 'pricefield_field') {
- return $element + array(
- '#type' => 'textfield',
- '#size' => 6,
- );
- }
- }
- function pricefield_widget_info(){
- return array(
- 'percentage_field' => array(
- 'label' => t('Percentage field'),
- 'field types' => array('percentage'),
- ),
- );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement