uc_product_handler_field_weight.inc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @file
  4. * Views handler: Product weight field.
  5. */
  6. /**
  7. * Returns a formatted weight value to display in the View.
  8. */
  9. class uc_product_handler_field_weight extends views_handler_field_numeric {
  10. /**
  11. * Overrides views_handler::option_definition().
  12. */
  13. function option_definition() {
  14. $options = parent::option_definition();
  15. $options['format'] = array('default' => 'uc_weight');
  16. return $options;
  17. }
  18. /**
  19. * Overrides views_handler::options_form().
  20. */
  21. function options_form(&$form, &$form_state) {
  22. parent::options_form($form, $form_state);
  23. $options = $this->options;
  24. $form['format'] = array(
  25. '#title' => t('Format'),
  26. '#type' => 'radios',
  27. '#options' => array(
  28. 'uc_weight' => t('Ubercart weight'),
  29. 'numeric' => t('Numeric'),
  30. ),
  31. '#default_value' => $options['format'],
  32. '#weight' => 1,
  33. );
  34. // Change weight and dependency of the previous field on the parent numeric ones
  35. $weight = 2;
  36. foreach (array('set_precision', 'precision', 'decimal', 'separator', 'prefix', 'suffix') as $field) {
  37. if (isset($form[$field]['#dependency'])) {
  38. $form[$field]['#dependency'] += array('radio:options[format]' => array('numeric'));
  39. $form[$field]['#dependency_count'] = count($form[$field]['#dependency']);
  40. }
  41. else {
  42. $form[$field]['#dependency'] = array('radio:options[format]' => array('numeric'));
  43. }
  44. $form[$field]['#weight'] = ++$weight;
  45. }
  46. }
  47. /**
  48. * Overrides views_handler_field::render().
  49. */
  50. function render($values) {
  51. if ($this->options['format'] == 'numeric') {
  52. return parent::render($values);
  53. }
  54. if ($this->options['format'] == 'uc_weight') {
  55. return uc_weight_format($values->{$this->field_alias}, $values->{$this->aliases['weight_units']});
  56. }
  57. }
  58. }