FieldPluginBase.php 58.3 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\field\FieldPluginBase.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
namespace Drupal\views\Plugin\views\field;
9

10
use Drupal\views\Plugin\views\HandlerBase;
11
use Drupal\views\Plugin\views\display\DisplayPluginBase;
12
use Drupal\views\ResultRow;
13
use Drupal\views\ViewExecutable;
14

merlinofchaos's avatar
merlinofchaos committed
15
16
17
18
19
20
21
22
/**
 * @defgroup views_field_handlers Views field handlers
 * @{
 * Handlers to tell Views how to build and display fields.
 *
 */

/**
23
 * Indicator of the renderText() method for rendering a single item.
merlinofchaos's avatar
merlinofchaos committed
24
25
26
27
28
 * (If no render_item() is present).
 */
define('VIEWS_HANDLER_RENDER_TEXT_PHASE_SINGLE_ITEM', 0);

/**
29
 * Indicator of the renderText() method for rendering the whole element.
merlinofchaos's avatar
merlinofchaos committed
30
31
32
33
34
 * (if no render_item() method is available).
 */
define('VIEWS_HANDLER_RENDER_TEXT_PHASE_COMPLETELY', 1);

/**
35
 * Indicator of the renderText() method for rendering the empty text.
merlinofchaos's avatar
merlinofchaos committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 */
define('VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY', 2);

/**
 * Base field handler that has no options and renders an unformatted field.
 *
 * Definition terms:
 * - additional fields: An array of fields that should be added to the query
 *                      for some purpose. The array is in the form of:
 *                      array('identifier' => array('table' => tablename,
 *                      'field' => fieldname); as many fields as are necessary
 *                      may be in this array.
 * - click sortable: If TRUE, this field may be click sorted.
 *
 * @ingroup views_field_handlers
 */
52
abstract class FieldPluginBase extends HandlerBase {
53

merlinofchaos's avatar
merlinofchaos committed
54
55
56
  var $field_alias = 'unknown';
  var $aliases = array();

57
58
59
60
61
62
63
  /**
   * The field value prior to any rewriting.
   *
   * @var mixed
   */
  public $original_value = NULL;

merlinofchaos's avatar
merlinofchaos committed
64
65
66
67
68
69
70
71
  /**
   * @var array
   * Stores additional fields which get's added to the query.
   * The generated aliases are stored in $aliases.
   */
  var $additional_fields = array();

  /**
72
   * Overrides Drupal\views\Plugin\views\HandlerBase::init().
merlinofchaos's avatar
merlinofchaos committed
73
   */
74
75
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
merlinofchaos's avatar
merlinofchaos committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

    $this->additional_fields = array();
    if (!empty($this->definition['additional fields'])) {
      $this->additional_fields = $this->definition['additional fields'];
    }

    if (!isset($this->options['exclude'])) {
      $this->options['exclude'] = '';
    }
  }

  /**
   * Determine if this field can allow advanced rendering.
   *
   * Fields can set this to FALSE if they do not wish to allow
   * token based rewriting or link-making.
   */
93
  protected function allowAdvancedRender() {
merlinofchaos's avatar
merlinofchaos committed
94
95
96
97
98
99
    return TRUE;
  }

  /**
   * Called to add the field to a query.
   */
100
  public function query() {
101
    $this->ensureMyTable();
merlinofchaos's avatar
merlinofchaos committed
102
103
    // Add the field.
    $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
104
    $this->field_alias = $this->query->addField($this->tableAlias, $this->realField, NULL, $params);
merlinofchaos's avatar
merlinofchaos committed
105

106
    $this->addAdditionalFields();
merlinofchaos's avatar
merlinofchaos committed
107
108
109
110
111
112
113
114
115
116
117
118
  }

  /**
   * Add 'additional' fields to the query.
   *
   * @param $fields
   * An array of fields. The key is an identifier used to later find the
   * field alias used. The value is either a string in which case it's
   * assumed to be a field on this handler's table; or it's an array in the
   * form of
   * @code array('table' => $tablename, 'field' => $fieldname) @endcode
   */
119
  protected function addAdditionalFields($fields = NULL) {
merlinofchaos's avatar
merlinofchaos committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    if (!isset($fields)) {
      // notice check
      if (empty($this->additional_fields)) {
        return;
      }
      $fields = $this->additional_fields;
    }

    $group_params = array();
    if ($this->options['group_type'] != 'group') {
      $group_params = array(
        'function' => $this->options['group_type'],
      );
    }

    if (!empty($fields) && is_array($fields)) {
      foreach ($fields as $identifier => $info) {
        if (is_array($info)) {
          if (isset($info['table'])) {
139
            $table_alias = $this->query->ensureTable($info['table'], $this->relationship);
merlinofchaos's avatar
merlinofchaos committed
140
141
          }
          else {
142
            $table_alias = $this->tableAlias;
merlinofchaos's avatar
merlinofchaos committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
          }

          if (empty($table_alias)) {
            debug(t('Handler @handler tried to add additional_field @identifier but @table could not be added!', array('@handler' => $this->definition['handler'], '@identifier' => $identifier, '@table' => $info['table'])));
            $this->aliases[$identifier] = 'broken';
            continue;
          }

          $params = array();
          if (!empty($info['params'])) {
            $params = $info['params'];
          }

          $params += $group_params;
157
          $this->aliases[$identifier] = $this->query->addField($table_alias, $info['field'], NULL, $params);
merlinofchaos's avatar
merlinofchaos committed
158
159
        }
        else {
160
          $this->aliases[$info] = $this->query->addField($this->tableAlias, $info, NULL, $group_params);
merlinofchaos's avatar
merlinofchaos committed
161
162
163
164
165
166
167
168
        }
      }
    }
  }

  /**
   * Called to determine what to tell the clicksorter.
   */
169
  public function clickSort($order) {
merlinofchaos's avatar
merlinofchaos committed
170
171
172
173
    if (isset($this->field_alias)) {
      // Since fields should always have themselves already added, just
      // add a sort on the field.
      $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
174
      $this->query->addOrderBy(NULL, NULL, $order, $this->field_alias, $params);
merlinofchaos's avatar
merlinofchaos committed
175
176
177
178
179
    }
  }

  /**
   * Determine if this field is click sortable.
180
181
182
183
   *
   * @return bool
   *   The value of 'click sortable' from the plugin definition, this defaults
   *   to TRUE if not set.
merlinofchaos's avatar
merlinofchaos committed
184
   */
185
  public function clickSortable() {
186
    return isset($this->definition['click sortable']) ? $this->definition['click sortable'] : TRUE;
merlinofchaos's avatar
merlinofchaos committed
187
188
189
190
191
  }

  /**
   * Get this field's label.
   */
192
  public function label() {
merlinofchaos's avatar
merlinofchaos committed
193
194
195
196
197
198
199
200
201
    if (!isset($this->options['label'])) {
      return '';
    }
    return $this->options['label'];
  }

  /**
   * Return an HTML element based upon the field's element type.
   */
202
  public function elementType($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
merlinofchaos's avatar
merlinofchaos committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    if ($none_supported) {
      if ($this->options['element_type'] === '0') {
        return '';
      }
    }
    if ($this->options['element_type']) {
      return check_plain($this->options['element_type']);
    }

    if ($default_empty) {
      return '';
    }

    if ($inline) {
      return 'span';
    }

    if (isset($this->definition['element type'])) {
      return $this->definition['element type'];
    }

    return 'span';
  }

  /**
   * Return an HTML element for the label based upon the field's element type.
   */
230
  public function elementLabelType($none_supported = FALSE, $default_empty = FALSE) {
merlinofchaos's avatar
merlinofchaos committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
    if ($none_supported) {
      if ($this->options['element_label_type'] === '0') {
        return '';
      }
    }
    if ($this->options['element_label_type']) {
      return check_plain($this->options['element_label_type']);
    }

    if ($default_empty) {
      return '';
    }

    return 'span';
  }

  /**
   * Return an HTML element for the wrapper based upon the field's element type.
   */
250
  public function elementWrapperType($none_supported = FALSE, $default_empty = FALSE) {
merlinofchaos's avatar
merlinofchaos committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    if ($none_supported) {
      if ($this->options['element_wrapper_type'] === '0') {
        return 0;
      }
    }
    if ($this->options['element_wrapper_type']) {
      return check_plain($this->options['element_wrapper_type']);
    }

    if ($default_empty) {
      return '';
    }

    return 'div';
  }

  /**
   * Provide a list of elements valid for field HTML.
   *
   * This function can be overridden by fields that want more or fewer
   * elements available, though this seems like it would be an incredibly
   * rare occurence.
   */
274
  public function getElements() {
merlinofchaos's avatar
merlinofchaos committed
275
276
    static $elements = NULL;
    if (!isset($elements)) {
277
      // @todo Add possible html5 elements.
278
279
280
281
      $elements = array(
        '' => t(' - Use default -'),
        '0' => t('- None -')
      );
282
      $elements += \Drupal::config('views.settings')->get('field_rewrite_elements');
merlinofchaos's avatar
merlinofchaos committed
283
284
285
286
287
288
289
290
    }

    return $elements;
  }

  /**
   * Return the class of the field.
   */
291
  public function elementClasses($row_index = NULL) {
merlinofchaos's avatar
merlinofchaos committed
292
293
    $classes = explode(' ', $this->options['element_class']);
    foreach ($classes as &$class) {
294
      $class = $this->tokenizeValue($class, $row_index);
295
      $class = drupal_clean_css_identifier($class);
merlinofchaos's avatar
merlinofchaos committed
296
297
298
299
300
301
302
303
304
305
    }
    return implode(' ', $classes);
  }

  /**
   * Replace a value with tokens from the last field.
   *
   * This function actually figures out which field was last and uses its
   * tokens so they will all be available.
   */
306
  public function tokenizeValue($value, $row_index = NULL) {
merlinofchaos's avatar
merlinofchaos committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) {
      $fake_item = array(
        'alter_text' => TRUE,
        'text' => $value,
      );

      // Use isset() because empty() will trigger on 0 and 0 is
      // the first row.
      if (isset($row_index) && isset($this->view->style_plugin->render_tokens[$row_index])) {
        $tokens = $this->view->style_plugin->render_tokens[$row_index];
      }
      else {
        // Get tokens from the last field.
        $last_field = end($this->view->field);
        if (isset($last_field->last_tokens)) {
          $tokens = $last_field->last_tokens;
        }
        else {
325
          $tokens = $last_field->getRenderTokens($fake_item);
merlinofchaos's avatar
merlinofchaos committed
326
327
328
        }
      }

329
      $value = strip_tags($this->renderAltered($fake_item, $tokens));
merlinofchaos's avatar
merlinofchaos committed
330
331
332
333
334
335
336
337
338
339
340
      if (!empty($this->options['alter']['trim_whitespace'])) {
        $value = trim($value);
      }
    }

    return $value;
  }

  /**
   * Return the class of the field's label.
   */
341
  public function elementLabelClasses($row_index = NULL) {
merlinofchaos's avatar
merlinofchaos committed
342
343
    $classes = explode(' ', $this->options['element_label_class']);
    foreach ($classes as &$class) {
344
      $class = $this->tokenizeValue($class, $row_index);
345
      $class = drupal_clean_css_identifier($class);
merlinofchaos's avatar
merlinofchaos committed
346
347
348
349
350
351
352
    }
    return implode(' ', $classes);
  }

  /**
   * Return the class of the field's wrapper.
   */
353
  public function elementWrapperClasses($row_index = NULL) {
merlinofchaos's avatar
merlinofchaos committed
354
355
    $classes = explode(' ', $this->options['element_wrapper_class']);
    foreach ($classes as &$class) {
356
      $class = $this->tokenizeValue($class, $row_index);
357
      $class = drupal_clean_css_identifier($class);
merlinofchaos's avatar
merlinofchaos committed
358
359
360
361
    }
    return implode(' ', $classes);
  }

362
  /**
363
   * Gets the entity matching the current row and relationship.
364
   *
365
   * @param \Drupal\views\ResultRow $values
366
   *   An object containing all retrieved values.
367
368
369
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   Returns the entity matching the values.
370
   */
371
  public function getEntity(ResultRow $values) {
372
373
374
375
376
377
378
379
380
    $relationship_id = $this->options['relationship'];
    if ($relationship_id == 'none') {
      return $values->_entity;
    }
    else {
      return $values->_relationship_entities[$relationship_id];
    }
  }

merlinofchaos's avatar
merlinofchaos committed
381
382
383
384
385
386
387
388
389
390
391
  /**
   * Get the value that's supposed to be rendered.
   *
   * This api exists so that other modules can easy set the values of the field
   * without having the need to change the render method as well.
   *
   * @param $values
   *   An object containing all retrieved values.
   * @param $field
   *   Optional name of the field where the value is stored.
   */
392
  public function getValue($values, $field = NULL) {
merlinofchaos's avatar
merlinofchaos committed
393
394
395
396
397
398
    $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
    if (isset($values->{$alias})) {
      return $values->{$alias};
    }
  }

399
400
401
402
403
404
405
  /**
   * Determines if this field will be available as an option to group the result
   * by in the style settings.
   *
   * @return bool
   *  TRUE if this field handler is groupable, otherwise FALSE.
   */
406
  public function useStringGroupBy() {
407
408
409
    return TRUE;
  }

410
411
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
412
413
414
415
416
417
418
419

    $options['label'] = array('default' => $this->definition['title'], 'translatable' => TRUE);
    $options['exclude'] = array('default' => FALSE, 'bool' => TRUE);
    $options['alter'] = array(
      'contains' => array(
        'alter_text' => array('default' => FALSE, 'bool' => TRUE),
        'text' => array('default' => '', 'translatable' => TRUE),
        'make_link' => array('default' => FALSE, 'bool' => TRUE),
420
        'path' => array('default' => ''),
merlinofchaos's avatar
merlinofchaos committed
421
422
423
424
425
426
427
428
429
430
        'absolute' => array('default' => FALSE, 'bool' => TRUE),
        'external' => array('default' => FALSE, 'bool' => TRUE),
        'replace_spaces' => array('default' => FALSE, 'bool' => TRUE),
        'path_case' => array('default' => 'none', 'translatable' => FALSE),
        'trim_whitespace' => array('default' => FALSE, 'bool' => TRUE),
        'alt' => array('default' => '', 'translatable' => TRUE),
        'rel' => array('default' => ''),
        'link_class' => array('default' => ''),
        'prefix' => array('default' => '', 'translatable' => TRUE),
        'suffix' => array('default' => '', 'translatable' => TRUE),
431
        'target' => array('default' => ''),
merlinofchaos's avatar
merlinofchaos committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
        'nl2br' => array('default' => FALSE, 'bool' => TRUE),
        'max_length' => array('default' => ''),
        'word_boundary' => array('default' => TRUE, 'bool' => TRUE),
        'ellipsis' => array('default' => TRUE, 'bool' => TRUE),
        'more_link' => array('default' => FALSE, 'bool' => TRUE),
        'more_link_text' => array('default' => '', 'translatable' => TRUE),
        'more_link_path' => array('default' => ''),
        'strip_tags' => array('default' => FALSE, 'bool' => TRUE),
        'trim' => array('default' => FALSE, 'bool' => TRUE),
        'preserve_tags' => array('default' => ''),
        'html' => array('default' => FALSE, 'bool' => TRUE),
      ),
    );
    $options['element_type'] = array('default' => '');
    $options['element_class'] = array('default' => '');

    $options['element_label_type'] = array('default' => '');
    $options['element_label_class'] = array('default' => '');
    $options['element_label_colon'] = array('default' => TRUE, 'bool' => TRUE);

    $options['element_wrapper_type'] = array('default' => '');
    $options['element_wrapper_class'] = array('default' => '');

    $options['element_default_classes'] = array('default' => TRUE, 'bool' => TRUE);

    $options['empty'] = array('default' => '', 'translatable' => TRUE);
    $options['hide_empty'] = array('default' => FALSE, 'bool' => TRUE);
    $options['empty_zero'] = array('default' => FALSE, 'bool' => TRUE);
    $options['hide_alter_empty'] = array('default' => TRUE, 'bool' => TRUE);

    return $options;
  }

  /**
   * Performs some cleanup tasks on the options array before saving it.
   */
468
  public function submitOptionsForm(&$form, &$form_state) {
merlinofchaos's avatar
merlinofchaos committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
    $options = &$form_state['values']['options'];
    $types = array('element_type', 'element_label_type', 'element_wrapper_type');
    $classes = array_combine(array('element_class', 'element_label_class', 'element_wrapper_class'), $types);

    foreach ($types as $type) {
      if (!$options[$type . '_enable']) {
        $options[$type] = '';
      }
    }

    foreach ($classes as $class => $type) {
      if (!$options[$class . '_enable'] || !$options[$type . '_enable']) {
        $options[$class] = '';
      }
    }

    if (empty($options['custom_label'])) {
      $options['label'] = '';
      $options['element_label_colon'] = FALSE;
    }
  }

  /**
   * Default options form that provides the label widget that all fields
   * should have.
   */
495
496
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
497
498
499
500
501
502
503
504
505
506
507
508

    $label = $this->label();
    $form['custom_label'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create a label'),
      '#default_value' => $label !== '',
      '#weight' => -103,
    );
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label'),
      '#default_value' => $label,
509
510
511
512
      '#states' => array(
        'visible' => array(
          ':input[name="options[custom_label]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
513
514
515
516
517
518
519
      ),
      '#weight' => -102,
    );
    $form['element_label_colon'] = array(
      '#type' => 'checkbox',
      '#title' => t('Place a colon after the label'),
      '#default_value' => $this->options['element_label_colon'],
520
521
522
523
      '#states' => array(
        'visible' => array(
          ':input[name="options[custom_label]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
524
525
526
527
528
529
530
531
532
533
534
535
536
      ),
      '#weight' => -101,
    );

    $form['exclude'] = array(
      '#type' => 'checkbox',
      '#title' => t('Exclude from display'),
      '#default_value' => $this->options['exclude'],
      '#description' => t('Enable to load this field as hidden. Often used to group fields, or to use as token in another field.'),
      '#weight' => -100,
    );

    $form['style_settings'] = array(
537
      '#type' => 'details',
merlinofchaos's avatar
merlinofchaos committed
538
      '#title' => t('Style settings'),
539
      '#collapsed' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
540
541
542
543
544
545
546
547
548
549
550
      '#weight' => 99,
    );

    $form['element_type_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Customize field HTML'),
      '#default_value' => !empty($this->options['element_type']) || (string) $this->options['element_type'] == '0' || !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
      '#fieldset' => 'style_settings',
    );
    $form['element_type'] = array(
      '#title' => t('HTML element'),
551
      '#options' => $this->getElements(),
merlinofchaos's avatar
merlinofchaos committed
552
553
554
      '#type' => 'select',
      '#default_value' => $this->options['element_type'],
      '#description' => t('Choose the HTML element to wrap around this field, e.g. H1, H2, etc.'),
555
556
557
558
      '#states' => array(
        'visible' => array(
          ':input[name="options[element_type_enable]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
559
560
561
562
563
564
565
      ),
      '#fieldset' => 'style_settings',
    );

    $form['element_class_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create a CSS class'),
566
567
568
569
      '#states' => array(
        'visible' => array(
          ':input[name="options[element_type_enable]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
570
571
572
573
574
575
576
577
578
      ),
      '#default_value' => !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
      '#fieldset' => 'style_settings',
    );
    $form['element_class'] = array(
      '#title' => t('CSS class'),
      '#description' => t('You may use token substitutions from the rewriting section in this class.'),
      '#type' => 'textfield',
      '#default_value' => $this->options['element_class'],
579
580
581
582
583
      '#states' => array(
        'visible' => array(
          ':input[name="options[element_type_enable]"]' => array('checked' => TRUE),
          ':input[name="options[element_class_enable]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
584
585
586
587
588
589
590
591
592
593
594
595
      ),
      '#fieldset' => 'style_settings',
    );

    $form['element_label_type_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Customize label HTML'),
      '#default_value' => !empty($this->options['element_label_type']) || (string) $this->options['element_label_type'] == '0' || !empty($this->options['element_label_class']) || (string) $this->options['element_label_class'] == '0',
      '#fieldset' => 'style_settings',
    );
    $form['element_label_type'] = array(
      '#title' => t('Label HTML element'),
596
      '#options' => $this->getElements(FALSE),
merlinofchaos's avatar
merlinofchaos committed
597
598
599
      '#type' => 'select',
      '#default_value' => $this->options['element_label_type'],
      '#description' => t('Choose the HTML element to wrap around this label, e.g. H1, H2, etc.'),
600
601
602
603
      '#states' => array(
        'visible' => array(
          ':input[name="options[element_label_type_enable]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
604
605
606
607
608
609
      ),
      '#fieldset' => 'style_settings',
    );
    $form['element_label_class_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create a CSS class'),
610
611
612
613
      '#states' => array(
        'visible' => array(
          ':input[name="options[element_label_type_enable]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
614
615
616
617
618
619
620
621
622
      ),
      '#default_value' => !empty($this->options['element_label_class']) || (string) $this->options['element_label_class'] == '0',
      '#fieldset' => 'style_settings',
    );
    $form['element_label_class'] = array(
      '#title' => t('CSS class'),
      '#description' => t('You may use token substitutions from the rewriting section in this class.'),
      '#type' => 'textfield',
      '#default_value' => $this->options['element_label_class'],
623
624
625
626
627
      '#states' => array(
        'visible' => array(
          ':input[name="options[element_label_type_enable]"]' => array('checked' => TRUE),
          ':input[name="options[element_label_class_enable]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
628
629
630
631
632
633
634
635
636
637
638
639
      ),
      '#fieldset' => 'style_settings',
    );

    $form['element_wrapper_type_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Customize field and label wrapper HTML'),
      '#default_value' => !empty($this->options['element_wrapper_type']) || (string) $this->options['element_wrapper_type'] == '0' || !empty($this->options['element_wrapper_class']) || (string) $this->options['element_wrapper_class'] == '0',
      '#fieldset' => 'style_settings',
    );
    $form['element_wrapper_type'] = array(
      '#title' => t('Wrapper HTML element'),
640
      '#options' => $this->getElements(FALSE),
merlinofchaos's avatar
merlinofchaos committed
641
642
643
      '#type' => 'select',
      '#default_value' => $this->options['element_wrapper_type'],
      '#description' => t('Choose the HTML element to wrap around this field and label, e.g. H1, H2, etc. This may not be used if the field and label are not rendered together, such as with a table.'),
644
645
646
647
      '#states' => array(
        'visible' => array(
          ':input[name="options[element_wrapper_type_enable]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
648
649
650
651
652
653
654
      ),
      '#fieldset' => 'style_settings',
    );

    $form['element_wrapper_class_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create a CSS class'),
655
656
657
658
      '#states' => array(
        'visible' => array(
          ':input[name="options[element_wrapper_type_enable]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
659
660
661
662
663
664
665
666
667
      ),
      '#default_value' => !empty($this->options['element_wrapper_class']) || (string) $this->options['element_wrapper_class'] == '0',
      '#fieldset' => 'style_settings',
    );
    $form['element_wrapper_class'] = array(
      '#title' => t('CSS class'),
      '#description' => t('You may use token substitutions from the rewriting section in this class.'),
      '#type' => 'textfield',
      '#default_value' => $this->options['element_wrapper_class'],
668
669
670
671
672
      '#states' => array(
        'visible' => array(
          ':input[name="options[element_wrapper_class_enable]"]' => array('checked' => TRUE),
          ':input[name="options[element_wrapper_type_enable]"]' => array('checked' => TRUE),
        ),
merlinofchaos's avatar
merlinofchaos committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
      ),
      '#fieldset' => 'style_settings',
    );

    $form['element_default_classes'] = array(
      '#type' => 'checkbox',
      '#title' => t('Add default classes'),
      '#default_value' => $this->options['element_default_classes'],
      '#description' => t('Use default Views classes to identify the field, field label and field content.'),
      '#fieldset' => 'style_settings',
    );

    $form['alter'] = array(
      '#title' => t('Rewrite results'),
687
      '#type' => 'details',
688
      '#collapsed' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
689
690
691
      '#weight' => 100,
    );

692
    if ($this->allowAdvancedRender()) {
merlinofchaos's avatar
merlinofchaos committed
693
694
695
      $form['alter']['#tree'] = TRUE;
      $form['alter']['alter_text'] = array(
        '#type' => 'checkbox',
696
        '#title' => t('Override the output of this field with custom text'),
merlinofchaos's avatar
merlinofchaos committed
697
698
699
700
701
702
703
704
        '#default_value' => $this->options['alter']['alter_text'],
      );

      $form['alter']['text'] = array(
        '#title' => t('Text'),
        '#type' => 'textarea',
        '#default_value' => $this->options['alter']['text'],
        '#description' => t('The text to display for this field. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
705
706
707
708
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][alter_text]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
709
710
711
712
713
        ),
      );

      $form['alter']['make_link'] = array(
        '#type' => 'checkbox',
714
        '#title' => t('Output this field as a custom link'),
merlinofchaos's avatar
merlinofchaos committed
715
716
717
718
719
720
721
        '#default_value' => $this->options['alter']['make_link'],
      );
      $form['alter']['path'] = array(
        '#title' => t('Link path'),
        '#type' => 'textfield',
        '#default_value' => $this->options['alter']['path'],
        '#description' => t('The Drupal path or absolute URL for this link. You may enter data from this view as per the "Replacement patterns" below.'),
722
723
724
725
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
726
727
728
729
730
731
732
        ),
        '#maxlength' => 255,
      );
      $form['alter']['absolute'] = array(
        '#type' => 'checkbox',
        '#title' => t('Use absolute path'),
        '#default_value' => $this->options['alter']['absolute'],
733
734
735
736
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
737
738
739
740
741
742
        ),
      );
      $form['alter']['replace_spaces'] = array(
        '#type' => 'checkbox',
        '#title' => t('Replace spaces with dashes'),
        '#default_value' => $this->options['alter']['replace_spaces'],
743
744
745
746
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
747
748
749
750
751
752
753
        ),
      );
      $form['alter']['external'] = array(
        '#type' => 'checkbox',
        '#title' => t('External server URL'),
        '#default_value' => $this->options['alter']['external'],
        '#description' => t("Links to an external server using a full URL: e.g. 'http://www.example.com' or 'www.example.com'."),
754
755
756
757
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
758
759
760
761
762
763
        ),
      );
      $form['alter']['path_case'] = array(
        '#type' => 'select',
        '#title' => t('Transform the case'),
        '#description' => t('When printing url paths, how to transform the case of the filter value.'),
764
765
766
767
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
        ),
       '#options' => array(
          'none' => t('No transform'),
          'upper' => t('Upper case'),
          'lower' => t('Lower case'),
          'ucfirst' => t('Capitalize first letter'),
          'ucwords' => t('Capitalize each word'),
        ),
        '#default_value' => $this->options['alter']['path_case'],
      );
      $form['alter']['link_class'] = array(
        '#title' => t('Link class'),
        '#type' => 'textfield',
        '#default_value' => $this->options['alter']['link_class'],
        '#description' => t('The CSS class to apply to the link.'),
783
784
785
786
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
787
788
789
790
791
792
793
        ),
      );
      $form['alter']['alt'] = array(
        '#title' => t('Title text'),
        '#type' => 'textfield',
        '#default_value' => $this->options['alter']['alt'],
        '#description' => t('Text to place as "title" text which most browsers display as a tooltip when hovering over the link.'),
794
795
796
797
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
798
799
800
801
802
803
804
        ),
      );
      $form['alter']['rel'] = array(
        '#title' => t('Rel Text'),
        '#type' => 'textfield',
        '#default_value' => $this->options['alter']['rel'],
        '#description' => t('Include Rel attribute for use in lightbox2 or other javascript utility.'),
805
806
807
808
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
809
810
811
812
813
814
815
        ),
      );
      $form['alter']['prefix'] = array(
        '#title' => t('Prefix text'),
        '#type' => 'textfield',
        '#default_value' => $this->options['alter']['prefix'],
        '#description' => t('Any text to display before this link. You may include HTML.'),
816
817
818
819
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
820
821
822
823
824
825
826
        ),
      );
      $form['alter']['suffix'] = array(
        '#title' => t('Suffix text'),
        '#type' => 'textfield',
        '#default_value' => $this->options['alter']['suffix'],
        '#description' => t('Any text to display after this link. You may include HTML.'),
827
828
829
830
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
831
832
833
834
835
836
837
        ),
      );
      $form['alter']['target'] = array(
        '#title' => t('Target'),
        '#type' => 'textfield',
        '#default_value' => $this->options['alter']['target'],
        '#description' => t("Target of the link, such as _blank, _parent or an iframe's name. This field is rarely used."),
838
839
840
841
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
842
843
844
845
846
        ),
      );


      // Get a list of the available fields and arguments for token replacement.
847
848
849
850
851

      // Setup the tokens for fields.
      $previous = $this->getPreviousFieldLabels();
      foreach ($previous as $id => $label) {
        $options[t('Fields')]["[$id]"] = $label;
merlinofchaos's avatar
merlinofchaos committed
852
      }
853

merlinofchaos's avatar
merlinofchaos committed
854
      $count = 0; // This lets us prepare the key as we want it printed.
855
      foreach ($this->view->display_handler->getHandlers('argument') as $arg => $handler) {
856
857
        $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->adminLabel()));
        $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->adminLabel()));
merlinofchaos's avatar
merlinofchaos committed
858
859
      }

860
      $this->documentSelfTokens($options[t('Fields')]);
merlinofchaos's avatar
merlinofchaos committed
861
862

      // Default text.
863
      $output = '<p>' . t('You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.') . '</p>';
merlinofchaos's avatar
merlinofchaos committed
864
865
      // We have some options, so make a list.
      if (!empty($options)) {
866
        $output = '<p>' . t("The following tokens are available for this field. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields. If you would like to have the characters '[' and ']' use the html entity codes '%5B' or '%5D' or they will get replaced with empty space.") . '</p>';
merlinofchaos's avatar
merlinofchaos committed
867
868
869
870
871
872
        foreach (array_keys($options) as $type) {
          if (!empty($options[$type])) {
            $items = array();
            foreach ($options[$type] as $key => $value) {
              $items[] = $key . ' == ' . $value;
            }
873
874
875
            $item_list = array(
              '#theme' => 'item_list',
              '#items' => $items,
876
              '#list_type' => $type,
877
878
            );
            $output .= drupal_render($item_list);
merlinofchaos's avatar
merlinofchaos committed
879
880
881
882
883
884
885
886
          }
        }
      }
      // This construct uses 'hidden' and not markup because process doesn't
      // run. It also has an extra div because the dependency wants to hide
      // the parent in situations like this, so we need a second div to
      // make this work.
      $form['alter']['help'] = array(
887
        '#type' => 'details',
merlinofchaos's avatar
merlinofchaos committed
888
        '#title' => t('Replacement patterns'),
889
        '#collapsed' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
890
        '#value' => $output,
891
892
893
894
895
896
897
898
899
900
901
902
        '#states' => array(
          'visible' => array(
            array(
              ':input[name="options[alter][make_link]"]' => array('checked' => TRUE),
            ),
            array(
              ':input[name="options[alter][alter_text]"]' => array('checked' => TRUE),
            ),
            array(
              ':input[name="options[alter][more_link]"]' => array('checked' => TRUE),
            ),
          ),
merlinofchaos's avatar
merlinofchaos committed
903
904
905
906
907
        ),
      );

      $form['alter']['trim'] = array(
        '#type' => 'checkbox',
908
        '#title' => t('Trim this field to a maximum number of characters'),
merlinofchaos's avatar
merlinofchaos committed
909
910
911
912
        '#default_value' => $this->options['alter']['trim'],
      );

      $form['alter']['max_length'] = array(
913
        '#title' => t('Maximum number of characters'),
merlinofchaos's avatar
merlinofchaos committed
914
915
        '#type' => 'textfield',
        '#default_value' => $this->options['alter']['max_length'],
916
917
918
919
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][trim]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
920
921
922
923
924
925
926
927
        ),
      );

      $form['alter']['word_boundary'] = array(
        '#type' => 'checkbox',
        '#title' => t('Trim only on a word boundary'),
        '#description' => t('If checked, this field be trimmed only on a word boundary. This is guaranteed to be the maximum characters stated or less. If there are no word boundaries this could trim a field to nothing.'),
        '#default_value' => $this->options['alter']['word_boundary'],
928
929
930
931
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][trim]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
932
933
934
935
936
        ),
      );

      $form['alter']['ellipsis'] = array(
        '#type' => 'checkbox',
937
        '#title' => t('Add "..." at the end of trimmed text'),
merlinofchaos's avatar
merlinofchaos committed
938
        '#default_value' => $this->options['alter']['ellipsis'],
939
940
941
942
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][trim]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
943
944
945
946
947
948
949
        ),
      );

      $form['alter']['more_link'] = array(
        '#type' => 'checkbox',
        '#title' => t('Add a read-more link if output is trimmed.'),
        '#default_value' => $this->options['alter']['more_link'],
950
951
952
953
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][trim]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
954
955
956
957
958
        ),
      );

      $form['alter']['more_link_text'] = array(
        '#type' => 'textfield',
959
        '#title' => t('More link label'),
merlinofchaos's avatar
merlinofchaos committed
960
        '#default_value' => $this->options['alter']['more_link_text'],
961
        '#description' => t('You may use the "Replacement patterns" above.'),
962
963
964
965
966
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][trim]"]' => array('checked' => TRUE),
            ':input[name="options[alter][more_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
967
968
969
970
971
972
        ),
      );
      $form['alter']['more_link_path'] = array(
        '#type' => 'textfield',
        '#title' => t('More link path'),
        '#default_value' => $this->options['alter']['more_link_path'],
973
        '#description' => t('This can be an internal Drupal path such as node/add or an external URL such as "http://drupal.org". You may use the "Replacement patterns" above.'),
974
975
976
977
978
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][trim]"]' => array('checked' => TRUE),
            ':input[name="options[alter][more_link]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
979
980
981
982
983
984
        ),
      );

      $form['alter']['html'] = array(
        '#type' => 'checkbox',
        '#title' => t('Field can contain HTML'),
985
        '#description' => t('An HTML corrector will be run to ensure HTML tags are properly closed after trimming.'),
merlinofchaos's avatar
merlinofchaos committed
986
        '#default_value' => $this->options['alter']['html'],
987
988
989
990
        '#states' => array(
          'visible' => array(
            ':input[name="options[alter][trim]"]' => array('checked' => TRUE),
          ),
merlinofchaos's avatar
merlinofchaos committed
991
992
993
994
995
996
997
998
999
1000
        ),
      );

      $form['alter']['strip_tags'] = array(
        '#type' => 'checkbox',
        '#title' => t('Strip HTML tags'),
        '#default_value' => $this->options['alter']['strip_tags'],
      );

      $form['alter']['preserve_tags'] = array(