-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagged_text.module
235 lines (210 loc) · 7.77 KB
/
tagged_text.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* @file
* Tagged Text Field Module
*/
/**
* Implements hook_field_info().
*
* This field provides a select list for choosing a folder path to be saved as field value.
*/
function tagged_text_field_info() {
$fields = array();
$fields['tagged_text'] = array(
'label' => t('Tagged textfield'),
'description' => t('Creates a taxonomy tagged textfield for use in entities'),
//'settings' => array(),
'instance_settings' => array(
'tag_vocabulary' => '',
'title_taxonomy' => '',
'title_textfield' => '',
),
'default_widget' => 'tagged_text_select_text',
'default_formatter' => 'tagged_text_tag_text',
);
return $fields;
}
/**
* Implements hook_field_widget_info().
*/
function tagged_text_field_widget_info() {
$widgets = array();
$widgets['tagged_text_select_text'] = array(
'label' => t('Tagged textfield'),
'field types' => array('tagged_text'),
'settings' => array(
'rows' => 3,
),
);
return $widgets;
}
/**
* Implements hook_field_formatter_info().
*
* https://api.drupal.org/api/drupal/modules!field!field.api.php/function/hook_field_widget_info/7
*/
function tagged_text_field_formatter_info() {
return array(
'tagged_text_tag_only' => array(
'label' => t('Tag only'),
'description' => t('Displays only the renderd taxonomy'),
//'settings' => array('taxonomy_mode' => 'text'),
'field types' => array('tagged_text',),
),
'tagged_text_tag_text' => array(
'label' => t('Tag & text'),
'description' => t('Displays both the rendered taxonomy and the textfield'),
'field types' => array('tagged_text',),
),
'tagged_text_text_only' => array(
'label' => t('Text only'),
'description' => t('Displays only the renderd textfield'),
'field types' => array('tagged_text',),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function tagged_text_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
global $base_path, $base_url;
$settings = $display['settings'];
$element = array();
foreach ($items as $delta => $item) {
$tag = '';
$text = '';
if (in_array($display['type'], array('tagged_text_tag_only', 'tagged_text_tag_text'))) {
$term = taxonomy_term_load($item['tid']);
$tag = $term->name;
$tag = '<div class="tagged-text-term">'.check_plain($tag).'</div>';
}
if (in_array($display['type'], array('tagged_text_tag_text', 'tagged_text_text_only'))) {
$text = check_markup($item['value'], $item['format'], $langcode);
$text = '<div class="tagged-text-text">'.$text.'</div>';
}
$element[$delta] = array(
'#markup' => '<div class="'.drupal_html_class($display['type']).'">'.$tag.$text.'</div>',
);
}
return $element;
}
/**
* Implements hook_field_is_empty().
*/
function tagged_text_field_is_empty($item, $field) {
if (empty($item['value']) && (string) $item['value'] !== '0') {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_field_settings_form().
*/
function tagged_text_field_instance_settings_form($field, $instance) {
switch ($field['type']) {
case 'tagged_text':
$vocabularies = taxonomy_get_vocabularies();
$vocab_array = array();
foreach ($vocabularies as &$item) {
$vocab_array[$item->vid] = $item->name;
}
$form['tag_vocabulary'] = array(
'#type' => 'select',
'#title' => t('Tag Vocabulary'),
'#options' => $vocab_array,
'#default_value' => $instance['settings']['tag_vocabulary'],
'#description' => t("This taxonomy will be used to tag the textfield entries."),
);
$form['title_taxonomy'] = array(
'#type' => 'textfield',
'#title' => t('Taxonomy Title'),
'#default_value' => empty($instance['settings']['title_taxonomy']) ? t('Tag') : $instance['settings']['title_taxonomy'],
'#description' => t("This text will be the header for the Taxonomy. You may leave this blank."),
);
$form['title_textfield'] = array(
'#type' => 'textfield',
'#title' => t('Textfield Title'),
'#default_value' => empty($instance['settings']['title_textfield']) ? t('Details') : $instance['settings']['title_textfield'],
'#description' => t("This text will be the header of the textfield. You may leave this blank."),
);
break;
}
return $form;
}
/**
* Implements hook_field_settings_form().
*/
function tagged_text_field_settings_form($field, $instance, $has_data) {
//$settings = $field['settings'];
}
/**
* Implements hook_field_widget_form().
*/
function tagged_text_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
//global $base_path;
// Setting switch into parameters that wll work widget types that are in module.
switch ($instance['widget']['type']) {
// If the selected widget is 'folder_list'.
case 'tagged_text_select_text':
// Build the Taxonomy select box
//kpr($instance);
$terms = taxonomy_get_tree($instance['settings']['tag_vocabulary']);
$terms_select = array();
foreach ($terms as $term) {
$terms_select[$term->tid] = $term->name;
}
//kpr($terms);
//kpr($items);
$element['tid'] = array(
'#type' => 'select',
// Heading given in element settings.
//'#title' => $element['#title'],
// Widget description is given in element settings.
//'#description' => $element['#description'],
'#default_value' => (isset($items[$delta]['tid']) ? $items[$delta]['tid'] : NULL),
// If element is required it will be indicated in settings of the very element.
'#required' => $element['#required'],
// Element location –
// If it's indicated, value should be used, if not, value is 0.
'#weight' => -1,
// Line that we'll need for the output.
//'#delta' => $delta,
// We assign the options here
'#options' => $terms_select,
'#prefix' => '<div class="tagged-text-admin">', //style="float: left; margin-right: 10px;"
'#suffix' => '</div>',
);
$element += array(
'#type' => 'text_format', //'text_format' or 'textarea' or 'textfield'
//'#title' => t('Value'),
'#description' => $element['#description'],
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
'#rows' => $instance['widget']['settings']['rows'],
'#format' => isset($items[$delta]['format']) ? $items[$delta]['format'] : NULL,
'#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
'#delta' => $delta,
);
//kpr($instance);
if (!empty($instance['settings']['title_taxonomy'])) {
$element['tid']['#title'] = $instance['settings']['title_taxonomy'];
}
if (!empty($instance['settings']['title_textfield'])) {
$element['#title'] = $instance['settings']['title_textfield'];
}
$element['#prefix'] = "<div>"; //<div style=\"font-weight: bold;\" class=\"field-title\">{$instance['settings']['field_title']}</div>";
$element['#suffix'] = "</div>";
break;
}
return $element;
}
/**
* Implements hook_field_presave().
*/
function tagged_text_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
if ($field['type'] == 'tagged_text') {
//dsm($entity); // node object being saved
//dsm($field); // field config values, includes columns, module and type
//dsm($instance); // field instance config (for this node type), bundle and default value
//dsm($items); // $delta keyed list of entries for this save
}
}