Advertisement
Guest User

Untitled

a guest
Mar 13th, 2012
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @pricefield.module
  4.  * add a price field.
  5.  *
  6.  */
  7.  
  8.  /**
  9.  * Implements hook_field_formatter_info().
  10.  */
  11. function pricefield_field_info() {
  12.   return array(
  13.     'pricefield_format' => array( //Machine name of the formatter
  14.       'label' => t('Price'),
  15.       'field types' => array('text'), //This will only be available to text fields
  16.       'settings'  => array( //Array of the settings we'll create
  17.         'currency' => '$', //give a default value for when the form is first loaded        
  18.       ),            
  19.       'default_widget' => 'pricefield_field',
  20.       'default_formatter' => 'pricefield_format',
  21.     ),
  22.   );
  23. }
  24.  
  25. /**
  26.  * Implements hook_field_formatter_settings_form().
  27.  */
  28. function pricefield_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  29.   //This gets the view_mode where our settings are stored
  30.   $display = $instance['display'][$view_mode];
  31.   //This gets the actual settings
  32.   $settings = $display['settings'];
  33.   //Initialize the element variable
  34.   $element = array();
  35.   //Add your select box
  36.   $element['currency'] = array(
  37.     '#type'           => 'textfield',                           // Use a select box widget
  38.     '#title'          => 'Select Currency',                   // Widget label
  39.     '#description'    => t('Select currency used by the field'), // Helper text
  40.     '#default_value'  => $settings['currency'],              // Get the value if it's already been set    
  41.   );
  42.   return $element;
  43. }
  44.  
  45. /**
  46.  * Implements hook_field_formatter_settings_summary().
  47.  */
  48. function pricefield_field_formatter_settings_summary($field, $instance, $view_mode) {
  49.   $display = $instance['display'][$view_mode];
  50.   $settings = $display['settings'];
  51.   $summary = t('The default currency is: @currency ', array(
  52.     '@currency'     => $settings['currency'],    
  53.   )); // we use t() for translation and placeholders to guard against attacks
  54.   return $summary;
  55. }
  56.  
  57. /**
  58.  * Implements hook_field_formatter_view().
  59.  */
  60. function pricefield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  61.   $element = array(); // Initialize the var
  62.   $settings = $display['settings']; // get the settings
  63.   $currency = $settings['currency']; // Get the currency  
  64.   foreach ($items as $delta => $item) {
  65.     $price = $item['safe_value']; // Getting the actual value
  66.   }
  67.  
  68.   if($price==0){
  69.       $element[0] = array('#markup' => 'Free');
  70.   } else {
  71.       $element[0] = array('#markup' => $currency.' '.$price);
  72.   }
  73.   return $element;
  74. }
  75.  
  76. function pricefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta = 0, $element){
  77.     if ($instance['widget']['type'] == 'pricefield_field') {
  78.         return $element + array(
  79.             '#type' => 'textfield',
  80.             '#size' => 6,
  81.         );
  82.     }
  83. }
  84.  
  85. function pricefield_widget_info(){
  86.     return array(
  87.     'percentage_field' => array(
  88.     'label' => t('Percentage field'),
  89.     'field types' => array('percentage'),
  90.         ),
  91.     );
  92. }
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement