/[drupal]/contributions/modules/linkimagefield/linkimagefield_widget.inc
ViewVC logotype

Contents of /contributions/modules/linkimagefield/linkimagefield_widget.inc

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


Revision 1.5 - (show annotations) (download) (as text)
Fri Sep 4 18:32:34 2009 UTC (2 months, 3 weeks ago) by johnfyoung
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-BETA2, HEAD
Changes since 1.4: +3 -3 lines
File MIME type: text/x-php
#561922 by johnfyoung: Fixed call with reference
1 <?php
2 // $Id: linkimagefield_widget.inc,v 1.4 2009/08/27 19:50:30 johnfyoung Exp $
3 /**
4 * @file
5 *
6 * linkimagefield widget code
7 * @author John Young <john@codeandcreative.com>
8 */
9
10 /**
11 * Implementation of CCK's hook_widget_settings($op = 'form')
12 *
13 * @param array $widget
14 * @return array
15 */
16 function linkimagefield_widget_settings_form($widget) {
17 $form = module_invoke('imagefield', 'widget_settings', 'form', $widget);
18
19 $form['url_settings'] = array(
20 '#type' => 'fieldset',
21 '#title' => t('URL Link settings'),
22 '#collapsible' => TRUE,
23 '#collapsed' => FALSE,
24 '#weight' => 0,
25 );
26
27 $form['url_settings']['url'] = array(
28 '#type' => 'textfield',
29 '#maxlength' => '255',
30 '#title' => t('Default URL'),
31 '#description' => t('Provide a well-formed URL. This will be the default url linked to by provided images.'),
32 '#default_value' => !empty($widget['url']) ? $widget['url'] : '',
33 );
34
35 $target_options = _linkimagefield_widget_url_target_options();
36
37 $form['url_settings']['custom_target'] = array(
38 '#type' => 'checkbox',
39 '#title' => t('Enable custom target'),
40 '#default_value' => !empty($widget['custom_target']) ? $widget['custom_target'] : 0,
41 '#description' => t('Enable user to provide alternate target frame for link.'),
42 );
43
44 $form['url_settings']['target'] = array(
45 '#type' => 'select',
46 '#maxlength' => '255',
47 '#title' => t('Default Target'),
48 '#description' => t('Select a default target type.'),
49 '#default_value' => !empty($widget['target']) ? $widget['target'] : '_self',
50 '#options' => $target_options,
51 );
52
53 $form['url_settings']['nofollow'] = array(
54 '#type' => 'checkbox',
55 '#title' => t('Enable nofollow'),
56 '#default_value' => !empty($widget['nofollow']) ? $widget['nofollow'] : 0,
57 '#description' => t('Allow links to have "nofollow" rel tags'),
58 );
59
60 return $form;
61 }
62
63 /**
64 * Implementation of CCK's hook_widget_settings($op = 'save').
65 *
66 * @param array $widget
67 * @return array
68 */
69 function linkimagefield_widget_settings_save($widget) {
70 $imagefield_settings = module_invoke('imagefield', 'widget_settings', 'save', $widget);
71 return array_merge($imagefield_settings, array('url', 'custom_target', 'target', 'nofollow'));
72 }
73
74 /**
75 * Implementation of CCK's hook_widget_settings($op = 'validate').
76 *
77 * @param array $widget
78 */
79 function linkimagefield_widget_settings_validate($widget) {
80 module_invoke('imagefield', 'widget_settings', 'validate', $widget);
81
82 // Check the URL
83 if (!empty($widget['url'])) {
84 if (!valid_url($widget['url'])) {
85 form_set_error('url', t('Please provide a valid url.'));
86 } else {
87 $widget['url'] = check_url($widget['url']);
88 }
89 }
90
91 // Check the target
92 if (!empty($widget['target'])) {
93 $options = array('_self', '_blank', '_parent', '_top');
94
95 if(!in_array($widget['target'], $options)) {
96 $widget['target'] = '_self';
97 }
98 }
99 }
100
101 /**
102 * Element #value_callback function.
103 *
104 * @param array $element
105 * @param mixed $edit
106 * @return array
107 */
108 function linkimagefield_widget_value($element, $edit = FALSE) {
109 $item = imagefield_widget_value($element, $edit);
110 if ($edit) {
111 $item['url'] = isset($edit['data']['url']) ? $edit['data']['url'] : '';
112 $item['target'] = isset($edit['data']['target']) ? $edit['data']['target'] : '_self';
113 $item['nofollow'] = isset($edit['data']['nofollow']) ? $edit['data']['nofollow'] : FALSE;
114 }
115 else {
116 $item['url'] = '';
117 $item['target'] = '_self';
118 $item['nofollow'] = TRUE;
119 }
120 return $item;
121 }
122
123 /**
124 * Element #process callback function.
125 *
126 * @param array $element
127 * @param array $edit
128 * @param arrayreference $form_state
129 * @param array $form
130 * @return array
131 */
132 function linkimagefield_widget_process($element, $edit, &$form_state, $form) {
133 $file = $element['#value'];
134 $element = imagefield_widget_process($element, $edit, $form_state, $form);
135
136 // add some help text, telling user the URL field will be viewable after the file is supplied
137 $element['upload']['#description'] = t('Once an Image is uploaded, you will ba able to supply a URL for the link.') . '<br />' . $element['upload']['#description'];
138
139 $field = content_fields($element['#field_name'], $element['#type_name']);
140 $element['#theme'] = 'linkimagefield_widget_item';
141
142 $element['data']['url'] = array(
143 '#title' => t('URL'),
144 '#type' => 'textfield',
145 '#default_value' => $file['data']['url'] ? $file['data']['url'] : $field['widget']['url'],
146 '#description' => t('This URL will be loaded when the image is clicked'),
147 '#maxlength' => 255,
148 );
149
150 $default_target = !$field['widget']['custom_target'];
151
152 $element['data']['target'] = array(
153 '#title' => t('Target'),
154 '#type' => $field['widget']['custom_target'] ? 'select' : 'value',
155 '#default_value' => $default_target ? $field['widget']['target'] : $file['data']['target'],
156 '#description' => t('Window/Frame Target for the URL'),
157 );
158
159 if($field['widget']['custom_target']) {
160 $element['data']['target']['#options'] = array_merge(array('default' => t('Use Default').' ('.$field['widget']['target'].')'),_linkimagefield_widget_url_target_options());
161 }
162
163 if($field['widget']['nofollow']) {
164 $element['data']['nofollow'] = array(
165 '#title' => t('Mark as "nofollow"'),
166 '#type' => 'checkbox',
167 '#default_value' => $file['data']['nofollow'],
168 '#description' => t('Add a relation tag telling bots not to follow this link'),
169 );
170 }
171 return $element;
172 }
173
174 /**
175 * FormAPI theme function. Theme the output of an image field.
176 *
177 * @param arrayreference $element
178 * @return string
179 */
180 function theme_linkimagefield_widget(&$element) {
181 drupal_add_css(drupal_get_path('module', 'imagefield') .'/imagefield.css');
182 return theme('form_element', $element, $element['#children']);
183 }
184
185 /**
186 * the list of URL frame targets
187 *
188 * @return array
189 */
190 function _linkimagefield_widget_url_target_options(){
191 return array(
192 '_self' => t('Same Window (_self)'),
193 '_blank' => t('New Window (_blank)'),
194 '_parent' => t('Parent Frame (_parent)'),
195 '_top' => t('Top Frame (_top)'),
196 );
197 }

  ViewVC Help
Powered by ViewVC 1.1.2