link.module 13.6 KB
Newer Older
Nathan Haug's avatar
Nathan Haug committed
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
<?php
// $Id$

/**
 * @file
 * Defines simple link field types.
 */

/**
 * Implementation of hook_help().
 */
function link_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Defines simple link field types. <em>Note: Requires content.module.</em>');
  }
}

/**
 * Implementation of hook_field_info().
 */
function link_field_info() {
  return array(
    'link' => array('label' => 'Link'),
  );
}

/**
 * Implementation of hook_field_settings().
 */
function link_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
35
36
37
38
39
40
41
42
43
44
45
46
47
48
      
      $options = array (
        'optional' => t('Optional Title'),
        'required' => t('Required Title'),
        'none' => t('No Title'),
      );
      
      $form['title'] = array(
        '#type' => 'radios',
        '#title' => t('Link Title'),
        '#default_value' => isset($field['title']) ? $field['title'] : 'optional',
        '#options' => $options,
      );
      
Nathan Haug's avatar
Nathan Haug committed
49
50
51
52
53
54
55
      $options = array(
        'default' => t('Default (no target attribute)'),
        '_top' => t('Open link in window root'),
        '_blank' => t('Open link in new window'),
        'user' => t('Allow the user to choose'),
      );
      $form['attributes'] = array (
56
        '#tree' => true,
Nathan Haug's avatar
Nathan Haug committed
57
58
59
60
61
62
63
      );
      $form['attributes']['target'] = array(
        '#type' => 'radios',
        '#title' => t('Link Target'),
        '#default_value' => $field['attributes']['target'] ? $field['attributes']['target'] : 'default',
        '#options' => $options,
      );
Nathan Haug's avatar
Nathan Haug committed
64
65
66
67
68
69
70
71
72
      $form['attributes']['rel'] = array(
        '#type' => 'checkbox',
        '#return_value' => 'nofollow',
        '#prefix' => '<div class="form-item"><label>Nofollow Value: </label>',
        '#suffix' => '</div>',
        '#title' => t('Add rel=&quot;nofollow&quot; Attribute'),
        '#description' => t('The <a href="http://en.wikipedia.org/wiki/Nofollow#rel.3Dnofollow">rel=&quot;nofollow&quot; attribute</a> prevents some search engines from spidering entered links.'),
        '#default_value' => isset($field['attributes']['rel']) ? $field['attributes']['rel'] : false,
      );
Nathan Haug's avatar
Nathan Haug committed
73
74
75
      return $form;

    case 'save':
76
      return array('attributes', 'title');
Nathan Haug's avatar
Nathan Haug committed
77
78
79
80
81
82
83
84
85
86
87
88
  }
}

/**
 * Implementation of hook_field().
 */
function link_field($op, &$node, $field, &$node_field, $teaser, $page) {
  switch ($op) {
    case 'load':
      switch($field['type']) {
        case 'link':
          $result = db_query("SELECT field_url, field_title, attributes FROM {node_field_link_data} WHERE vid = %d AND field_name = '%s' ORDER BY delta", $node->vid, $field['field_name']);
Nathan Haug's avatar
Nathan Haug committed
89
90
          while ($value = db_fetch_array($result)) {
            $values[] = array('value' => array('link' => $value['field_url'], 'title' => $value['field_title'], 'attributes' => unserialize($value['attributes'])));
Nathan Haug's avatar
Nathan Haug committed
91
          }
Nathan Haug's avatar
Nathan Haug committed
92
          $additions = array($field['field_name'] => $values);
Nathan Haug's avatar
Nathan Haug committed
93
94
95
96
97
          break;
      }
      return $additions;

    case 'view':
Nathan Haug's avatar
Nathan Haug committed
98
99
      foreach ($node_field as $delta => $item) {
        $node_field[$delta]['view'] = _link_field_view($field, $item['value'], $item, $node);
Nathan Haug's avatar
Nathan Haug committed
100
      }
Nathan Haug's avatar
Nathan Haug committed
101
      return theme('field', $node, $field, $node_field, $teaser, $page);
Nathan Haug's avatar
Nathan Haug committed
102
103
104
    case 'insert':
      switch($field['type']) {
        case 'link':
Nathan Haug's avatar
Nathan Haug committed
105
106
107
          foreach ($node_field as $delta => $item) {
            if ($item['value']['link']) {
              db_query("INSERT INTO {node_field_link_data} (nid, vid, field_name, delta, field_url, field_title, attributes) VALUES (%d, %d, '%s', %d, '%s', '%s', '%s')", $node->nid, $node->vid, $field['field_name'], $delta, $item['value']['link'], $item['value']['title'], serialize($item['value']['attributes']));
Nathan Haug's avatar
Nathan Haug committed
108
109
110
111
112
113
114
115
116
117
            }
          }
          break;
      }
      return;

    case 'update':
      // Delete and insert, rather than update, in case a field was added.
      switch($field['type']) {
        case 'link':
Nathan Haug's avatar
Nathan Haug committed
118
          db_query("DELETE FROM {node_field_link_data} WHERE vid = %d AND field_name = '%s'", $node->vid, $field['field_name']);
Nathan Haug's avatar
Nathan Haug committed
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
          link_field('insert', $node, $field, $node_field, $teaser, $page);
          break;
      }
      return;

    case 'delete':
      // Delete using nid rather than vid to purge all revisions.
      switch($field['type']) {
        case 'link':
          db_query("DELETE FROM {node_field_link_data} WHERE nid = %d AND field_name = '%s'", $node->nid, $field['field_name']);
          break;
      }
      return;
  }
}

/**
 * Implementation of hook_widget_info().
 */
function link_widget_info() {
  return array(
    'link' => array(
      'label' => 'Text Fields for Title and URL',
      'field types' => array('link'),
    ),
  );
}

/**
 * Implementation of hook_widget().
 */
function link_widget($op, &$node, $field, &$node_field) {
  switch ($op) {
    case 'form':
      $form = array();

      $form[$field['field_name']] = array('#tree' => TRUE);

      if ($field['multiple']) {
158
159
160
161
        // Generate more fields if necessary on preview
        if ($_POST['edit'][$field['field_name']]) {
          $node_field = $_POST['edit'][$field['field_name']];
        }
Nathan Haug's avatar
Nathan Haug committed
162
        $delta = 0;
163
        // Render link fields for all the entered values
Nathan Haug's avatar
Nathan Haug committed
164
        foreach ($node_field as $data) {
165
166
        if ($data['value']['link']) {
            _link_widget_form($form[$field['field_name']][$delta], $field, $data, $delta);
Nathan Haug's avatar
Nathan Haug committed
167
168
169
            $delta++;
          }
        }
170
        // Render two additional new link fields
Nathan Haug's avatar
Nathan Haug committed
171
        foreach (range($delta, $delta + 1) as $delta) {
172
          _link_widget_form($form[$field['field_name']][$delta], $field, $node_field, $delta);
Nathan Haug's avatar
Nathan Haug committed
173
174
175
        }
      } // end if multiple
      else {
Nathan Haug's avatar
Nathan Haug committed
176
177
178
179
        // Convert an old 'singe' value field into the new array based format
        if ($node_field['value']) {
          $node_field[0]['value'] = $node_field['value'];
          unset($node_field['value']);
180
        }
Nathan Haug's avatar
Nathan Haug committed
181
        _link_widget_form($form[$field['field_name']][0], $field, $node_field[0]);
Nathan Haug's avatar
Nathan Haug committed
182
183
184
185
      }
      return $form;

    case 'validate':
Nathan Haug's avatar
Nathan Haug committed
186
187
188
189
190
      foreach($node_field as $delta => $value) {
        if ($value['value']['link']) {
          // Validate the link
          if (!link_validate_link($value['value']['link'])) {
            form_set_error($field['field_name'] .']['. $delta. '][value][link', t('Not a valid URL.'));
191
          }
Nathan Haug's avatar
Nathan Haug committed
192
193
194
          // Require a title for the link if necessary
          elseif ($field['title'] == 'required' && strlen(trim($value['value']['title'])) == 0) {
            form_set_error($field['field_name'] .']['. $delta. '][value][title', t('Titles are required for all links.'));
Nathan Haug's avatar
Nathan Haug committed
195
196
          }
        }
Nathan Haug's avatar
Nathan Haug committed
197
198
199
        // Require a link if we have a title
        elseif (strlen($value['value']['title']) > 0) {
          form_set_error($field['field_name'] .']['. $delta. '][value][link', t('You cannot enter a title without a link.'));
200
        }
Nathan Haug's avatar
Nathan Haug committed
201
202
203
204
      }
      return;
      
    case 'process form values':
Nathan Haug's avatar
Nathan Haug committed
205
206
      foreach($node_field as $delta => $value) {
        _link_widget_process($node_field[$delta],$delta);
Nathan Haug's avatar
Nathan Haug committed
207
208
209
210
211
212
213
214
      }
      return;
    
    case 'submit':
      return;
  }
}

215
216
217
218
219
220
221
/**
 * Helper function renders the link widget in both single and multiple value cases.
 */

function _link_widget_form (&$form_item, $field, $node_field, $delta = 0) {
  $form_item['value'] = array(
    '#tree' => true,
Nathan Haug's avatar
Nathan Haug committed
222
    '#weigth' => $field['widget']['weight'],
223
224
225
  );
  $form_item['value']['link'] = array(
    '#type' => 'textfield',
Nathan Haug's avatar
Nathan Haug committed
226
227
    '#maxlength' => '255',
    '#title' => $field['title'] == 'none' ? t($field['widget']['label']) : t($field['widget']['label'])." ".t('URL'),
228
229
    '#default_value' => $node_field['value']['link'],
    '#required' => ($delta == 0) ? $field['required'] : FALSE,
Nathan Haug's avatar
Nathan Haug committed
230
    '#description' => $field['widget']['description'],
231
  );
232
233
234
  if ($field['title'] != 'none') {
    $form_item['value']['title'] = array(
      '#type' => 'textfield',
Nathan Haug's avatar
Nathan Haug committed
235
      '#maxlength' => '255',
236
237
238
239
240
      '#title' => t($field['widget']['label'])." ".t('Title'),
      '#default_value' => $node_field['value']['title'],
      '#required' => ($delta == 0 && $field['title'] == 'required') ? $field['required'] : FALSE,
    );
  }
241
242
243
244
245
246
247
248
249
250
  if ($field['attributes']['target'] == 'user') {
    $form_item['value']['attributes']['target'] = array(
      '#type' => 'checkbox',
      '#title' => t('Open URL in a New Window'),
      '#default_value' => $node_field['value']['attributes']['target'],
      '#return_value' => "_blank",
    );
  }
}

251
252
253
254
function _link_widget_process (&$node_field, $delta = 0) {
  // Remove the target attribute if not selected
  if (!$node_field['value']['attributes']['target'] || $node_field['value']['attributes']['target'] == "default") {
    unset($node_field['value']['attributes']['target']);
Nathan Haug's avatar
Nathan Haug committed
255
  }
256
257
}

Nathan Haug's avatar
Nathan Haug committed
258
259
260
/**
 * Implementation of hook_field_view() which performs any translation necessary.
 */
261
262
function _link_field_view($field, $value, $addlfields = array(), $node = NULL) {
  $attributes = array();
Nathan Haug's avatar
Nathan Haug committed
263
264
265
  // Add attributes defined at the widget level
  if (is_array($value['attributes'])) {
    foreach($value['attributes'] as $attribute => $attbvalue) {
266
267
268
      if (isset($field['attributes'][$attribute]) && $field['attributes'][$attribute] == 'user') {
        $attributes[$attribute] = $attbvalue;
      }
Nathan Haug's avatar
Nathan Haug committed
269
270
271
272
273
274
    }
  }
  // Add attributes defined at the field level
  if (is_array($field['attributes'])) {
    foreach($field['attributes'] as $attribute => $attbvalue) {
      if (!empty($attbvalue) && $attbvalue != 'default' && $attbvalue != 'user') {
275
        $attributes[$attribute] = $attbvalue;
Nathan Haug's avatar
Nathan Haug committed
276
277
278
279
280
      }
    }
  }
  // Build the link with a title
  if (strlen(trim($value['title']))) {
281
    $output = l($value['title'],link_cleanup_url($value['link']),$attributes);
Nathan Haug's avatar
Nathan Haug committed
282
  }
283
  // Build the link with the URL as the title (max 80 characters)
Nathan Haug's avatar
Nathan Haug committed
284
  else {
285
    $output = l(strlen($value['link']) > 80 ? substr($value['link'],0,80)."..." : $value['link'],link_cleanup_url($value['link']),$attributes);
Nathan Haug's avatar
Nathan Haug committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  }
  return $output;
}

/**
 * Implementation of hook_views_tables().
 */
function link_views_tables() {
  $tables = array();

  $fields = content_fields();
  foreach ($fields as $field) {
    if ($field['type'] == 'link') {
      $tables['node_field_link_data_'. $field['field_name']] = array(
        'name' => 'node_field_link_data',
        'join' => array(
          'left' => array(
            'table' => 'node',
            'field' => 'vid',
          ),
          'right' => array(
            'field' => 'vid',
          ),
            'extra' => array('field_name' => $field['field_name']),
        ),
        'fields' => array(
          'field_url' => array(
            'name' => 'Link URL: '. $field['field_name'],
            'sortable' => TRUE,
          ),
          'field_title' => array(
            'name' => 'Link Title: '. $field['field_name'],
            'sortable' => TRUE,
          ),
        ),
        'sorts' => array(
          'field_url' => array('name' => 'Link URL: '. $field['field_name']),
          'field_title' => array('name' => 'Link Title: '. $field['field_name']),
        ),
        'filters' => array(
          'field_url' => array(
            'name' => 'Link URL: '. $field['field_name'],
            'operator' => array(
              '='  => 'is',
              'contains' => 'contains',
              'begins' => 'begins with',
              'ends' => 'ends wth',
              'LIKE'  => 'matches pattern',
            ),
            'operator-handler' => '_text_filter_operator',
          ),
          'field_title' => array(
            'name' => 'Link Title: '. $field['field_name'],
            'operator' => array(
              '='  => 'is',
              'contains' => 'contains',
              'begins' => 'begins with',
              'ends' => 'ends wth',
              'LIKE'  => 'matches pattern',
            ),
            'operator-handler' => '_text_filter_operator',
          ),
        ),
      );
    }
  }

  return $tables;
}

356
357
358
359
/**
 * Forms a valid URL if possible from an entered address.
 * Trims whitespace and automatically adds an http:// to addresses without a protocol specified
 *
360
361
 * @param string $url
 * @param string $protocol The protocol to be prepended to the url if one is not specified
362
 */
363
function link_cleanup_url ($url, $protocol = "http") {
364
365
366
367
368
369
370
371
  $url = trim($url);
  
  // Check if there is no protocol specified
  $protocol_match = preg_match("/^([a-z0-9][a-z0-9\.\-_]*:\/\/)/i",$url);
  if (empty($protocol_match)) {
    // But should it be? Add an automatic http:// if it starts with a domain name
    $domain_match = preg_match('/^(([a-z0-9]([a-z0-9\-_]*\.)+)(aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|[a-z]{2}))/i',$url);
    if (!empty($domain_match)) {
372
      $url = $protocol."://".$url;
373
374
375
    }
  }
  
376
  return check_url($url);
377
378
379
380
381
382
383
384
385
}

/**
 * A lenient verification for URLs. Accepts all URLs following RFC 1738 standard for URL formation.  
 *
 * @param string $text
 * @return mixed Returns boolean FALSE if the URL is not valid. On success, returns an object with
 * the following attributes: protocol, hostname, ip, and port.
 */
Nathan Haug's avatar
Nathan Haug committed
386
function link_validate_link($text) {
387
  if (!preg_match(
388
389
    // protocol
    '/^([a-z0-9][a-z0-9\.\-_]*:\/\/)?'.
390
391
  '('.
    // domains
392
      '(([a-z0-9]([a-z0-9\-_]*\.)+)(aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|mobi|[a-z]{2}))'.
Nathan Haug's avatar
Nathan Haug committed
393
394
      // OR ip addresses
      '|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'.
395
  ')'.
Nathan Haug's avatar
Nathan Haug committed
396
397
    // port number
    '(:([0-9]{1,4}))?'.
Nathan Haug's avatar
Nathan Haug committed
398
399
400
    // the rest of the path
    "(\/[a-z0-9_\-\.~+%=&,$'():;*@]+)*".
    // anchors
Nathan Haug's avatar
Nathan Haug committed
401
    "\/?#?[a-z0-9_\-\.~+%=&,$'():;*@]*".
Nathan Haug's avatar
Nathan Haug committed
402
    // the query string
Nathan Haug's avatar
Nathan Haug committed
403
    "(\/?\?[a-z0-9+_\-\.\/%=&,$'():;*@]*)?".
Nathan Haug's avatar
Nathan Haug committed
404
405
406
407
    // forward slash 0 or 1 times
    '(\/)?'.
    // end of the expression, case insensitive
    '$/i', $text, $m)) {
408
    return false;
Nathan Haug's avatar
Nathan Haug committed
409
410
  }
  else {
411
    $url = new stdClass();
Nathan Haug's avatar
Nathan Haug committed
412
413
414
415
416
417
    $url->protocol = $m[2];
    $url->hostname = strtolower($m[5]).strtolower($m[7]);
    $url->ip = $m[8];
    $url->port = $m[10];
    return $url;
  }
418
}