-
Notifications
You must be signed in to change notification settings - Fork 0
/
readmore.field.inc
152 lines (136 loc) · 4.89 KB
/
readmore.field.inc
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* @file
* Field formatter hooks to implement a readmore.
*/
/**
* Implements hook_field_formatter_info().
*/
function readmore_field_formatter_info() {
return array(
'readmore' => array(
'label' => t('Read more'),
'field types' => array(
'text',
'text_long',
'text_with_summary',
),
'settings' => array(
'readmore_trim_length' => 500,
'readmore_trim_on_break' => TRUE,
'readmore_show_readless' => FALSE,
'readmore_ellipsis' => TRUE,
'readmore_wordsafe' => FALSE,
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function readmore_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'readmore') {
$element['readmore_trim_length'] = array(
'#title' => t('Trim length'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $settings['readmore_trim_length'],
'#element_validate' => array('element_validate_integer_positive'),
'#required' => TRUE,
);
$element['readmore_trim_on_break'] = array(
'#type' => 'checkbox',
'#title' => t('Trim on @break', array('@break' => '<!--break-->')),
'#description' => t('If @break not found in the text then trim length used.', array('@break' => '<!--break-->')),
'#default_value' => $settings['readmore_trim_on_break'],
);
$element['readmore_show_readless'] = array(
'#type' => 'checkbox',
'#title' => t('Show read less'),
'#default_value' => $settings['readmore_show_readless'],
);
$element['readmore_ellipsis'] = array(
'#type' => 'checkbox',
'#title' => t('Add ellipsis'),
'#default_value' => $settings['readmore_ellipsis'],
);
$element['readmore_wordsafe'] = array(
'#type' => 'checkbox',
'#title' => t('Truncate on a word boundary'),
'#default_value' => $settings['readmore_wordsafe'],
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function readmore_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] == 'readmore') {
$summary[] = t('Trim length: @length', array('@length' => $settings['readmore_trim_length']));
$summary[] = $settings['readmore_trim_on_break'] ? t('Trim on @break', array('@break' => '<!--break-->')) : t('Do not trim on @break', array('@break' => '<!--break-->'));
$summary[] = $settings['readmore_show_readless'] ? t('Show read less') : t('Do not show read less');
$summary[] = $settings['readmore_ellipsis'] ? t('Add ellipsis') : t('Do not add ellipsis');
$summary[] = $settings['readmore_wordsafe'] ? t('Safe words') : t('Do not safe words');
}
return implode('<br />', $summary);
}
/**
* Implements hook_field_formatter_view().
*/
function readmore_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'readmore':
drupal_add_js(READMORE_MODULE_PATH . '/js/readmore.js');
foreach ($items as $delta => $item) {
$text = _text_sanitize($instance, $langcode, $item, 'value');
$text_length = drupal_strlen($item['value']);
$trim_length = $display['settings']['readmore_trim_length'];
// Don't do anything if text length less than defined.
if ($text_length > $trim_length) {
// Add Read less if need.
if ($display['settings']['readmore_show_readless']) {
$text .= ' ' . l(t('Read less'), current_path(), array('attributes' => array('class' => array('readless-link'))));
}
// Get trimmed string.
$summary = readmore_truncate_string(
$text,
isset($item['format']) ? $item['format'] : NULL,
$trim_length,
$display['settings']['readmore_wordsafe'],
$display['settings']['readmore_trim_on_break']
);
// Add readmore link.
$summary .= '<span>';
$summary .= $display['settings']['readmore_ellipsis'] ? t('...') : NULL;
$summary .= l(t('read more'), current_path(), array('attributes' => array('class' => array('readmore-link'))));
$summary .= '</span>';
// Close all HTML tags.
$summary = _filter_htmlcorrector($summary);
$element[$delta] = array(
'#markup' => theme(
'readmore',
array(
'text' => $text,
'summary' => $summary,
)
),
);
}
else {
$element[$delta] = array(
'#markup' => $text,
);
}
}
break;
}
return $element;
}