views_handler_field_machine_name.inc

Definition of views_handler_field_machine_name.

File

handlers/views_handler_field_machine_name.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_machine_name.
  5. */
  6. /**
  7. * Field handler whichs allows to show machine name content as human name.
  8. * @ingroup views_field_handlers
  9. *
  10. * Definition items:
  11. * - options callback: The function to call in order to generate the value options. If omitted, the options 'Yes' and 'No' will be used.
  12. * - options arguments: An array of arguments to pass to the options callback.
  13. */
  14. class views_handler_field_machine_name extends views_handler_field {
  15. /**
  16. * @var array Stores the available options.
  17. */
  18. var $value_options;
  19. function get_value_options() {
  20. if (isset($this->value_options)) {
  21. return;
  22. }
  23. if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
  24. if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
  25. $this->value_options = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
  26. }
  27. else {
  28. $this->value_options = call_user_func($this->definition['options callback']);
  29. }
  30. }
  31. else {
  32. $this->value_options = array();
  33. }
  34. }
  35. function option_definition() {
  36. $options = parent::option_definition();
  37. $options['machine_name'] = array('default' => FALSE, 'bool' => TRUE);
  38. return $options;
  39. }
  40. function options_form(&$form, &$form_state) {
  41. parent::options_form($form, $form_state);
  42. $form['machine_name'] = array(
  43. '#title' => t('Output machine name'),
  44. '#description' => t('Display field as machine name.'),
  45. '#type' => 'checkbox',
  46. '#default_value' => !empty($this->options['machine_name']),
  47. );
  48. }
  49. function pre_render(&$values) {
  50. $this->get_value_options();
  51. }
  52. function render($values) {
  53. $value = $values->{$this->field_alias};
  54. if (!empty($this->options['machine_name']) || !isset($this->value_options[$value])) {
  55. $result = check_plain($value);
  56. }
  57. else {
  58. $result = $this->value_options[$value];
  59. }
  60. return $result;
  61. }
  62. }