Project

General

Profile

Paste
Download (2.12 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_serialized.inc @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_serialized.
6
 */
7

    
8
/**
9
 * Field handler to show data of serialized fields.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_serialized extends views_handler_field {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20
    $options['format'] = array('default' => 'unserialized');
21
    $options['key'] = array('default' => '');
22
    return $options;
23
  }
24

    
25
  /**
26
   * {@inheritdoc}
27
   */
28
  public function options_form(&$form, &$form_state) {
29
    parent::options_form($form, $form_state);
30

    
31
    $form['format'] = array(
32
      '#type' => 'select',
33
      '#title' => t('Display format'),
34
      '#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.'),
35
      '#options' => array(
36
        'unserialized' => t('Full data (unserialized)'),
37
        'serialized' => t('Full data (serialized)'),
38
        'key' => t('A certain key'),
39
      ),
40
      '#default_value' => $this->options['format'],
41
    );
42
    $form['key'] = array(
43
      '#type' => 'textfield',
44
      '#title' => t('Which key should be displayed'),
45
      '#default_value' => $this->options['key'],
46
      '#dependency' => array('edit-options-format' => array('key')),
47
    );
48
  }
49

    
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function options_validate(&$form, &$form_state) {
54
    // Require a key if the format is key.
55
    if ($form_state['values']['options']['format'] == 'key' && $form_state['values']['options']['key'] == '') {
56
      form_error($form['key'], t('You have to enter a key if you want to display a key of the data.'));
57
    }
58
  }
59

    
60
  /**
61
   * {@inheritdoc}
62
   */
63
  public function render($values) {
64
    $value = $values->{$this->field_alias};
65

    
66
    if ($this->options['format'] == 'unserialized') {
67
      return check_plain(print_r(unserialize($value), TRUE));
68
    }
69
    elseif ($this->options['format'] == 'key' && !empty($this->options['key'])) {
70
      $value = (array) unserialize($value);
71
      return check_plain($value[$this->options['key']]);
72
    }
73

    
74
    return check_plain($value);
75
  }
76

    
77
}