views_handler_field_serialized.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_serialized.
  5. */
  6. /**
  7. * Field handler to show data of serialized fields.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_serialized extends views_handler_field {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function option_definition() {
  16. $options = parent::option_definition();
  17. $options['format'] = array('default' => 'unserialized');
  18. $options['key'] = array('default' => '');
  19. return $options;
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function options_form(&$form, &$form_state) {
  25. parent::options_form($form, $form_state);
  26. $form['format'] = array(
  27. '#type' => 'select',
  28. '#title' => t('Display format'),
  29. '#description' => t('How should the serialized data be displayed. You can choose a custom array/object key or a print_r on the full output.'),
  30. '#options' => array(
  31. 'unserialized' => t('Full data (unserialized)'),
  32. 'serialized' => t('Full data (serialized)'),
  33. 'key' => t('A certain key'),
  34. ),
  35. '#default_value' => $this->options['format'],
  36. );
  37. $form['key'] = array(
  38. '#type' => 'textfield',
  39. '#title' => t('Which key should be displayed'),
  40. '#default_value' => $this->options['key'],
  41. '#dependency' => array('edit-options-format' => array('key')),
  42. );
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function options_validate(&$form, &$form_state) {
  48. // Require a key if the format is key.
  49. if ($form_state['values']['options']['format'] == 'key' && $form_state['values']['options']['key'] == '') {
  50. form_error($form['key'], t('You have to enter a key if you want to display a key of the data.'));
  51. }
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function render($values) {
  57. $value = $values->{$this->field_alias};
  58. if ($this->options['format'] == 'unserialized') {
  59. return check_plain(print_r(unserialize($value), TRUE));
  60. }
  61. elseif ($this->options['format'] == 'key' && !empty($this->options['key'])) {
  62. $value = (array) unserialize($value);
  63. return check_plain($value[$this->options['key']]);
  64. }
  65. return check_plain($value);
  66. }
  67. }