~spreadubuntu/spreadubuntu/devel-drupal6

« back to all changes in this revision

Viewing changes to modules/cck/includes/views/handlers/content_handler_field.inc

  • Committer: ruben
  • Date: 2009-06-08 09:38:49 UTC
  • Revision ID: ruben@captive-20090608093849-s1qtsyctv2vwp1x1
SpreadUbuntu moving to Drupal6. Based on ubuntu-drupal theme and adding our modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id: content_handler_field.inc,v 1.1.2.14 2009/03/18 19:20:52 yched Exp $
 
3
 
 
4
/**
 
5
 * @file
 
6
 * The subclass adds basic field and formatter info,
 
7
 * for field-specific subclasses to use if they need to.
 
8
 *
 
9
 * Fields could extend this class if they want field and formatter handling
 
10
 * but don't want the multiple value grouping options created by
 
11
 * content_handler_field_multiple.
 
12
 */
 
13
class content_handler_field extends views_handler_field_node {
 
14
  var $content_field;
 
15
 
 
16
  function construct() {
 
17
    parent::construct();
 
18
    $this->content_field = content_fields($this->definition['content_field_name']);
 
19
  }
 
20
 
 
21
  function init(&$view, $options) {
 
22
    $field = $this->content_field;
 
23
    parent::init($view, $options);
 
24
    if ($field['multiple']) {
 
25
      $this->additional_fields['delta'] = 'delta';
 
26
    }
 
27
    // Make sure we grab enough information to build a pseudo-node with enough
 
28
    // credentials at render-time.
 
29
    $this->additional_fields['type'] = array('table' => 'node', 'field' => 'type');
 
30
    $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
 
31
    $this->additional_fields['vid'] = array('table' => 'node', 'field' => 'vid');
 
32
  }
 
33
 
 
34
  function options(&$options) {
 
35
    parent::options($options);
 
36
    $field = $this->content_field;
 
37
 
 
38
    // Override views_handler_field_node's default label
 
39
    $options['label'] = '';
 
40
    $options['label_type'] = 'widget';
 
41
    $options['format'] = 'default';
 
42
  }
 
43
 
 
44
  /**
 
45
   * Provide formatter option.
 
46
   */
 
47
  function options_form(&$form, &$form_state) {
 
48
    parent::options_form($form, $form_state);
 
49
    // TODO: do we want the 'link to node' checkbox ?
 
50
    // That's usually formatters business...
 
51
 
 
52
    $field = $this->content_field;
 
53
    $options = $this->options;
 
54
 
 
55
    $form['label_type'] = array(
 
56
      '#title' => t('Label'),
 
57
      '#type' => 'radios',
 
58
      '#options' => array(
 
59
        'none' => t('None'),
 
60
        'widget' => t('Widget label (@label)', array('@label' => $field['widget']['label'])),
 
61
        'custom' => t('Custom'),
 
62
      ),
 
63
      '#default_value' => $options['label_type'],
 
64
      '#weight' => 2,
 
65
    );
 
66
    $form['label'] = array(
 
67
      '#title' => t('Custom label'),
 
68
      '#type' => 'textfield',
 
69
      '#default_value' => $options['label'],
 
70
      '#process' => array('views_process_dependency'),
 
71
      '#dependency' => array('radio:options[label_type]' => array('custom')),
 
72
      '#weight' => 3,
 
73
     );
 
74
 
 
75
    $field_types = _content_field_types();
 
76
    $formatters = array();
 
77
    if (is_array($field_types[$field['type']]['formatters'])) {
 
78
      foreach ($field_types[$field['type']]['formatters'] as $name => $info) {
 
79
        $formatters[$name] = $info['label'];
 
80
      }
 
81
    }
 
82
    $form['format'] = array(
 
83
      '#title' => t('Format'),
 
84
      '#type' => 'select',
 
85
      '#options' => $formatters,
 
86
      '#required' => TRUE,
 
87
      '#default_value' => $options['format'],
 
88
      '#weight' => 4,
 
89
    );
 
90
  }
 
91
 
 
92
 
 
93
  /**
 
94
   * Make sure some value is stored as a label.
 
95
   *
 
96
   * Don't use t(), since Views' views_handler_field already has
 
97
   * $this->options['label'] marked as a translatable field.
 
98
   *
 
99
   * @see http://drupal.org/node/285470
 
100
   */
 
101
  function options_submit($form, &$form_state) {
 
102
    switch ($form_state['values']['options']['label_type']) {
 
103
      case 'none':
 
104
        $form_state['values']['options']['label'] = '';
 
105
        break;
 
106
      case 'widget':
 
107
        $form_state['values']['options']['label'] = $this->content_field['widget']['label'];
 
108
        break;
 
109
    }
 
110
  }
 
111
 
 
112
  /**
 
113
   * @TODO
 
114
   * Now that we save the label in the submit process above we could
 
115
   * get rid of this function. Leave it here for now to be sure the
 
116
   * label works for fields that haven't been updated since this
 
117
   * change was made, since $this->options['label'] will be missing a
 
118
   * value until it is updated in the view.
 
119
   *
 
120
   * Don't use t(), since Views' views_handler_field already has
 
121
   * $this->options['label'] marked as a translatable field.
 
122
   */
 
123
  function label() {
 
124
    $field = $this->content_field;
 
125
    switch ($this->options['label_type']) {
 
126
      case 'none':
 
127
        return '';
 
128
      case 'widget':
 
129
        return $field['widget']['label'];
 
130
      default:
 
131
        return $this->options['label'];
 
132
    }
 
133
  }
 
134
 
 
135
  /**
 
136
   * Return DIV or SPAN based upon the field's element type.
 
137
   */
 
138
  function element_type() {
 
139
    if (isset($this->definition['element type'])) {
 
140
      return $this->definition['element type'];
 
141
    }
 
142
    // TODO Figure out exactly when to return a div or a <span>. Any field
 
143
    // that ever needs to be shown inline in Views UI. It needs to return
 
144
    // a div for textareas to prevent wrapping a <span> around a <p>.
 
145
    // Earl says we need to be sure that other fields we don't know
 
146
    // about won't end up wrapping a span around a block-level element.
 
147
    if ($this->content_field['widget']['type'] == 'text_textarea') {
 
148
      return 'div';
 
149
    }
 
150
    else {
 
151
      return 'span';
 
152
    }
 
153
  }
 
154
 
 
155
  function options_validate($form, &$form_state) { }
 
156
 
 
157
  /**
 
158
   * Provide text for the administrative summary
 
159
   */
 
160
  function admin_summary() {
 
161
    // Display the formatter name.
 
162
    $field = $this->content_field;
 
163
    $field_types = _content_field_types();
 
164
    if (isset($field_types[$field['type']]['formatters'][$this->options['format']])) {
 
165
      return t($field_types[$field['type']]['formatters'][$this->options['format']]['label']);
 
166
    }
 
167
  }
 
168
 
 
169
  function render($values) {
 
170
    $field = $this->content_field;
 
171
    $options = $this->options;
 
172
    $db_info = content_database_info($field);
 
173
 
 
174
    // Build a pseudo-node from the retrieved values.
 
175
    $node = drupal_clone($values);
 
176
    $node->type = $values->{$this->aliases['type']};
 
177
    $node->nid = $values->{$this->aliases['nid']};
 
178
    $node->vid = $values->{$this->aliases['vid']};
 
179
    // Some formatters need to behave differently depending on the build_mode
 
180
    // (for instance: preview), so we provide one.
 
181
    $node->build_mode = NODE_BUILD_NORMAL;
 
182
 
 
183
    $item = array();
 
184
    foreach ($db_info['columns'] as $column => $attributes) {
 
185
      $item[$column] = $values->{$this->aliases[$attributes['column']]};
 
186
    }
 
187
 
 
188
    $item['#delta'] = $field['multiple'] ?  $values->{$this->aliases['delta']} : 0;
 
189
 
 
190
    return $this->render_link(content_format($field, $item, $options['format'], $node), $values);
 
191
  }
 
192
 
 
193
}
 
 
b'\\ No newline at end of file'