/[drupal]/contributions/modules/collapse_text/collapse_text.module
ViewVC logotype

Contents of /contributions/modules/collapse_text/collapse_text.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Mon Dec 15 17:04:49 2008 UTC (11 months, 1 week ago) by pukku
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +47 -12 lines
File MIME type: text/x-php
merging changes from Drupal 6 branch
1 <?php
2
3 // $Id: collapse_text.module,v 1.4.2.3 2008/12/15 15:47:33 pukku Exp $
4
5 /**
6 * @file
7 * collapse_text is an input filter that allows text to be collapsible
8 */
9
10 /**
11 * Implementation of hook_filter_tips().
12 */
13 function collapse_text_filter_tips($delta, $format, $long = false) {
14 if ($long) {
15 return t('Enclose sections of text in [collapse] and [/collapse] to turn them into collapsible sections. If you use [collapse collapsed] and [/collapse], the section will start out collapsed. You may specify a title with [collapse title=some title] (or [collapse collapsed title=some title]). If no title is specified, the title will be taken from the first header (&lt;h1&gt;, &lt;h2&gt;, &lt;h3&gt;, ...) found. In the absence of a header, a default title is used.');
16 }
17 else {
18 return t('Make collapsible text blocks using [collapse] and [/collapse].');
19 }
20 }
21
22 /**
23 * Implementation of hook_filter().
24 */
25 function collapse_text_filter($op, $delta = 0, $format = -1, $text = '') {
26 switch ($op) {
27 case 'list':
28 return array(
29 0 => t('Collapse text')
30 );
31 break;
32
33 case 'description':
34 return t('Make collapsing text sections');
35 break;
36
37 case 'settings':
38 // This filter has no user settings.
39 break;
40
41 case 'no cache':
42 return FALSE;
43 break;
44
45 case 'prepare':
46 return $text;
47 break;
48
49 case 'process':
50 return collapse_text_process($text);
51 break;
52
53 default:
54 return $text;
55 break;
56 }
57 }
58
59 /**
60 * Provides a layer of encapsulation for the regex call.
61 */
62 function collapse_text_process($text) {
63 // Per #259535 and #233877, add ability to specify title
64 // in collapse text. Thanks rivena, Justyn
65 $text = preg_replace_callback('/
66 \[ # look for an opening bracket
67 collapse # followed by the word `collapse`
68 (\ collapsed)? # followed by (optionally) a space and the word `collapsed` (captured)
69 (?:\ title=([^\]]*))? # followed by (optionally) a space and a title, consisting of any
70 # characters except a close bracket (captured)
71 \] # followed by a closing bracket
72 (.+?) # then capture as few characters as possible until
73 \[\/collapse\] # a closing "tag", which is a slash followed by `collapse` in brackets
74 /smx',
75 "_collapse_text_replace_callback", $text);
76 return $text;
77 }
78
79 function _collapse_text_replace_callback($matches) {
80 // 2008-12-15 REMorse (no issue number) added space to make
81 // $collapsed work
82 $collapsed = ($matches[1] == ' collapsed');
83 $title = trim($matches[2]);
84 $interior = $matches[3];
85
86 if (empty($title)) {
87 // If a title is not supplied, look for a header (<h1>, <h2> ...)
88 // and use it as the title.
89 $h_matches = array();
90 preg_match('/<h\d[^>]*>(.+?)<\/h\d>/smi', $interior, $h_matches);
91 $title = strip_tags($h_matches[1]);
92 }
93
94 if (empty($title)) {
95 // If there is still no title, provide some default text.
96 // Added call to t() per #256176 yngens
97 $title = t('Use the arrow to expand or collapse this section');
98 }
99
100 $render_array = array(
101 '#type' => 'fieldset',
102 '#title' => $title,
103 '#collapsible' => true,
104 '#collapsed' => $collapsed,
105 );
106 $render_array['text_contents'] = array(
107 '#type' => 'markup',
108 '#value' => '<div>' . $interior . '</div>',
109 );
110
111 return drupal_render($render_array);
112 }
113
114 /**
115 * Implementation of hook_init().
116 *
117 * Due to caching issues, `collapse.js` is not properly added to
118 * a page when the cached version is used. This adds `collapse.js`
119 * to every page. This is not really the right way to solve the
120 * problem, but I don't know that Drupal provides us with the
121 * ability to attach CSS files to input filter caches.
122 *
123 * Per #256354 and #231529. Thanks rolf, sza, jippie1948, Justyn
124 */
125 function collapse_text_init() {
126 drupal_add_js('misc/collapse.js', 'core');
127 }

  ViewVC Help
Powered by ViewVC 1.1.2