views_handler_field_machine_name.inc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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
  12. * options. If omitted, the options 'Yes' and 'No' will be used.
  13. * - options arguments: An array of arguments to pass to the options callback.
  14. */
  15. class views_handler_field_machine_name extends views_handler_field {
  16. /**
  17. * @var array Stores the available options.
  18. */
  19. public $value_options;
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function get_value_options() {
  24. if (isset($this->value_options)) {
  25. return;
  26. }
  27. if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
  28. if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
  29. $this->value_options = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
  30. }
  31. else {
  32. $this->value_options = call_user_func($this->definition['options callback']);
  33. }
  34. }
  35. else {
  36. $this->value_options = array();
  37. }
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function option_definition() {
  43. $options = parent::option_definition();
  44. $options['machine_name'] = array('default' => FALSE, 'bool' => TRUE);
  45. return $options;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function options_form(&$form, &$form_state) {
  51. parent::options_form($form, $form_state);
  52. $form['machine_name'] = array(
  53. '#title' => t('Output machine name'),
  54. '#description' => t('Display field as machine name.'),
  55. '#type' => 'checkbox',
  56. '#default_value' => !empty($this->options['machine_name']),
  57. );
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function pre_render(&$values) {
  63. $this->get_value_options();
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function render($values) {
  69. $value = $values->{$this->field_alias};
  70. if (!empty($this->options['machine_name']) || !isset($this->value_options[$value])) {
  71. $result = check_plain($value);
  72. }
  73. else {
  74. $result = $this->value_options[$value];
  75. }
  76. return $result;
  77. }
  78. }