views_handler_field_serialized.inc

Definition of views_handler_field_serialized.

File

handlers/views_handler_field_serialized.inc
View source
  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. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['format'] = array('default' => 'unserialized');
  15. $options['key'] = array('default' => '');
  16. return $options;
  17. }
  18. function options_form(&$form, &$form_state) {
  19. parent::options_form($form, $form_state);
  20. $form['format'] = array(
  21. '#type' => 'select',
  22. '#title' => t('Display format'),
  23. '#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.'),
  24. '#options' => array(
  25. 'unserialized' => t('Full data (unserialized)'),
  26. 'serialized' => t('Full data (serialized)'),
  27. 'key' => t('A certain key'),
  28. ),
  29. '#default_value' => $this->options['format'],
  30. );
  31. $form['key'] = array(
  32. '#type' => 'textfield',
  33. '#title' => t('Which key should be displayed'),
  34. '#default_value' => $this->options['key'],
  35. '#dependency' => array('edit-options-format' => array('key')),
  36. );
  37. }
  38. function options_validate(&$form, &$form_state) {
  39. // Require a key if the format is key.
  40. if ($form_state['values']['options']['format'] == 'key' && $form_state['values']['options']['key'] == '') {
  41. form_error($form['key'], t('You have to enter a key if you want to display a key of the data.'));
  42. }
  43. }
  44. function render($values) {
  45. $value = $values->{$this->field_alias};
  46. if ($this->options['format'] == 'unserialized') {
  47. return check_plain(print_r(unserialize($value), TRUE));
  48. }
  49. elseif ($this->options['format'] == 'key' && !empty($this->options['key'])) {
  50. $value = (array) unserialize($value);
  51. return check_plain($value[$this->options['key']]);
  52. }
  53. return $value;
  54. }
  55. }