• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

handlers/views_handler_field_serialized.inc

00001 <?php
00002 
00008 class views_handler_field_serialized extends views_handler_field {
00009 
00010   function option_definition() {
00011     $options = parent::option_definition();
00012     $options['format'] = array('default' => 'unserialized');
00013     $options['key'] = array('default' => '');
00014     return $options;
00015   }
00016 
00017 
00018   function options_form(&$form, &$form_state) {
00019     parent::options_form($form, $form_state);
00020 
00021     $form['format'] = array(
00022       '#type' => 'select',
00023       '#title' => t('Display format'),
00024       '#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.'),
00025       '#options' => array(
00026         'unserialized' => t('Full data (unserialized)'),
00027         'serialized' => t('Full data (serialized)'),
00028         'key' => t('A certain key'),
00029       ),
00030       '#default_value' => $this->options['format'],
00031     );
00032     $form['key'] = array(
00033       '#type' => 'textfield',
00034       '#title' => t('Which key should be displayed'),
00035       '#default_value' => $this->options['key'],
00036       '#dependency' => array('edit-options-format' => array('key')),
00037     );
00038   }
00039 
00040   function options_validate(&$form, &$form_state) {
00041     // Require a key if the format is key.
00042     if ($form_state['values']['options']['format'] == 'key' && $form_state['values']['options']['key'] == '') {
00043       form_error($form['key'], t('You have to enter a key if you want to display a key of the data.'));
00044     }
00045   }
00046 
00047   function render($values) {
00048     $value = $values->{$this->field_alias};
00049 
00050     if ($this->options['format'] == 'unserialized') {
00051       return check_plain(print_r(unserialize($value), TRUE));
00052     }
00053     elseif ($this->options['format'] == 'key' && !empty($this->options['key'])) {
00054       $value = (array) unserialize($value);
00055       return check_plain($value[$this->options['key']]);
00056     }
00057 
00058     return $value;
00059   }
00060 }

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1