Projet

Général

Profil

Paste
Télécharger (1,84 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds_tamper / plugins / truncate_text.inc @ 7b9e8704

1
<?php
2

    
3
/**
4
 * @file
5
 * Truncate text to a specified length.
6
 */
7

    
8
$plugin = array(
9
  'form' => 'feeds_tamper_truncate_text_form',
10
  'callback' => 'feeds_tamper_truncate_text_callback',
11
  'validate' => 'feeds_tamper_truncate_text_validate',
12
  'name' => 'Truncate',
13
  'multi' => 'loop',
14
  'category' => 'Text',
15
);
16

    
17
function feeds_tamper_truncate_text_form($importer, $element_key, $settings) {
18
  $form = array();
19
  $form['num_char'] = array(
20
    '#type' => 'textfield',
21
    '#title' => t('Number of characters'),
22
    '#default_value' => isset($settings['num_char']) ? $settings['num_char'] : '',
23
    '#description' => t('The number of characters the text will be limited to.'),
24
  );
25
  $form['ellipses'] = array(
26
    '#type' => 'checkbox',
27
    '#title' => t('Ellipses'),
28
    '#default_value' => isset($settings['ellipses']) ? $settings['ellipses'] : FALSE,
29
    '#description' => t('Add ellipses (...) to the end of the truncated text.'),
30
  );
31
  $form['wordsafe'] = array(
32
    '#type' => 'checkbox',
33
    '#title' => t('Truncate on a word boundary'),
34
    '#default_value' => isset($settings['wordsafe']) ? $settings['wordsafe'] : FALSE,
35
    '#description' => t('Attempt to truncate on a word boundary.'),
36
  );
37
  return $form;
38
}
39

    
40
function feeds_tamper_truncate_text_validate($settings) {
41
  $settings['num_char'] = trim($settings['num_char']);
42
  if (!is_int($settings['num_char']) && ($settings['num_char'] !== (string) (int) $settings['num_char'])) {
43
    form_set_error('settings][num_char', t('Needs to be an integer.'));
44
  }
45
}
46

    
47
function feeds_tamper_truncate_text_callback($result, $item_key, $element_key, &$field, $settings, $source) {
48
  if (!empty($settings['wordsafe'])) {
49
    $field = truncate_utf8($field, $settings['num_char'], $settings['wordsafe'], $settings['ellipses']);
50
  }
51
  else {
52
    $field = truncate_utf8($field, $settings['num_char'], FALSE, $settings['ellipses']);
53
  }
54
}