-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_shortcodes.module
117 lines (97 loc) · 3.43 KB
/
custom_shortcodes.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
116
117
<?php
/**
* @file custom_shortcodes.module
*/
/**
* Implements [mymodule]_shortcode_info
*
* Define our shortcodes and their titles, descriptions, and callback functions.
* See the comments below for an explanation of each parameter
*
* @param none
* @return array
*/
function custom_shortcodes_shortcode_info() {
$shortcodes['button'] = array(
'title' => t('Link Button'), // The title of this shortcode, displayed in the Drupal backend for administrators to enable/disable shortcodes for input types
'description' => t('A simple button.'), // Description shown along with the title in the Drupal backend
'process callback' => 'custom_shortcodes_shortcode_button', // Custom function that deals with the variables and html output
'tips callback' => 'custom_shortcodes_shortcode_button_tip' // Custom function that displays some help text to the user
);
// $shortcodes['second_shortcode'] = array();
// $shortcodes['third_shortcode'] = array();
// and so on...
return $shortcodes;
}
/**
* Implements [mymodule]_theme
*
* Define our variables/attributes for each custom shortcode
*
* @param none
* @return array
*/
function custom_shortcodes_theme() {
return array(
'shortcode_button' => array(
'variables' => array('text' => '', 'link' => ''),
),
// 'shortcode_second_shortcode' => array(),
// 'shortcode_third_shortcode' => array(),
// and so on...
);
}
/**
* Implements [mymodule]_shortcode_[shortcodename]
*
* Define our process callback function for our [button] shortcode. This
* takes in our shortcode attributes from the shortcode and if empty, sets
* the property to the default value stated in this function. We then pass
* in our attributes to the theme() function which returns the HTML.
*
* $attrs = shortcode_attrs(array(
* 'attribute' => 'default_value_goes_here'
* ),
*
* @param $attrs array Array of variables/attributes the user can customize
* @return string
*/
function custom_shortcodes_shortcode_button($attrs, $text) {
$attrs = shortcode_attrs(array(
'link' => 'http://mywebsite.com'
), $attrs );
return theme('shortcode_button', array('link' => $attrs['link'], 'text' => $text));
}
/**
* Implements theme_shortcode_[shortcodename]
*
* This function returns the output of the shortcode. The variables/attributes
* are passed in as an array.
*
* @param $vars array
* @return string
*/
function theme_shortcode_button($vars) {
return '<div class="button"><a href="' . $vars['link'] . '">' . $vars['text'] . '</a></div>';
}
/**
* Implements [mymodule]_shortcode_[shortcodename]_tip
*
* This function outputs text below the WYSIWYG editor to tell the user what
* shortcodes are available and what the parameters are for the shortcode.
*
* @param $format stdClass Information about which text format is being used
* @param $long bool Whether this is the 'More Info' page for the format
* @return string The text to be returned
*/
function custom_shortcodes_shortcode_button_tip($format, $long) {
$output = array();
$output[] = '<p><strong>' . t('[button link="http://URLhere.com"]text[/button]') . '</strong> ';
if ($long) {
$output[] = t('Outputs text that is displayed as a button, which links to a specified URL.') . '</p>';
}
else {
$output[] = t('Outputs text that links to a URL.') . '</p>';
}
return implode(' ', $output);
}