-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_text_formats.module
executable file
·115 lines (99 loc) · 3.39 KB
/
display_text_formats.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* @file
* Module file for display_text_formats. 'administer filters'
*/
/**
* Implements hook_field_formatter_info().
*/
function display_text_formats_field_formatter_info() {
return array(
'text_formats' => array(
'label' => t('Custom Format'),
'field types' => array('text', 'text_long', 'text_with_summary'),
'settings' => array(
'trim_length' => 600,
'intelligent_trim' => 1,
'text_format' => 'plain_text',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function display_text_formats_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] === 'text_formats') {
$element['trim_length'] = array(
'#title' => t('Trim length'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $settings['trim_length'],
'#element_validate' => array('element_validate_integer_positive'),
'#required' => TRUE,
);
// Checkbox whether or not to trim intelligently.
$element['intelligent_trim'] = array(
'#title' => t('Use intelligent trim'),
'#type' => 'checkbox',
'#default_value' => $settings['intelligent_trim'],
);
// Build the options array for text formats.
$formats = filter_formats();
$options = array();
foreach ($formats as $key => $format) {
$options[$key] = $format->name;
}
// Dropdown of text formats.
$element['text_format'] = array(
'#title' => t('Text format'),
'#type' => 'select',
'#empty_option' => t('Select a format'),
'#default_value' => $settings['text_format'],
'#options' => $options,
'#required' => TRUE,
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function display_text_formats_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] === 'text_formats') {
$summary = ($settings['intelligent_trim']) ? 'Intelligent' : 'Dumb';
$summary .= ' ' . t('trim length') . ': ' . $settings['trim_length'] . ', ' .
t('Text format') . ': ' . $settings['text_format'];
}
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
function display_text_formats_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
if ($display['type'] === 'text_formats') {
foreach ($items as $delta => $item) {
// Do what _text_sanitize() does, knowing it has a text format.
$output = check_markup($item['value'], $display['settings']['text_format'], $langcode);
if (($line_break = strpos($item['safe_value'], '<!--break-->')) !== FALSE) {
$output = text_summary($output, $display['settings']['text_format'], $line_break);
}
// Use the text module's trimming function natively.
elseif ($display['settings']['intelligent_trim']) {
$output = text_summary($output, $display['settings']['text_format'], $display['settings']['trim_length']);
}
else {
$output = drupal_substr($output, 0, $display['settings']['trim_length']);
}
$element[$delta] = array('#markup' => $output);
}
}
return $element;
}