Projet

Général

Profil

Paste
Télécharger (2,87 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / modules / feeds_test_field / feeds_test_field.module @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides a field with special validation.
6
 */
7

    
8
/**
9
 * Implements hook_permission().
10
 */
11
function feeds_test_field_permission() {
12
  return array(
13
    'feeds_test_field.edit' => array(
14
      'title' => 'Edit Feeds test fields',
15
    ),
16
  );
17
}
18

    
19
/**
20
 * Implements hook_field_info().
21
 */
22
function feeds_test_field_field_info() {
23
  return array(
24
    'feeds_test_field' => array(
25
      // Since this module is only used in tests, label and description
26
      // don't have to be translatable.
27
      'label' => 'Feeds test field',
28
      'description' => 'This field stores text and requires special validation.',
29
      'default_widget' => 'feeds_test_field_textfield',
30
      'default_formatter' => 'feeds_test_field_default',
31
      'property_type' => 'text',
32
    ),
33
  );
34
}
35

    
36
/**
37
 * Implements hook_field_validate().
38
 */
39
function feeds_test_field_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {
40
  foreach ($items as $delta => $item) {
41
    // Verify that the current user has access to this field.
42
    if (!user_access('feeds_test_field.edit') && !user_access('administer feeds')) {
43
      $errors[$field['field_name']][$langcode][$delta][] = array(
44
        'error' => 'access_denied',
45
        // Since this module is only used in tests, the message doesn't have to
46
        // be translatable.
47
        'message' => 'You are not authorized to edit this field.',
48
      );
49
    }
50
  }
51
}
52

    
53
/**
54
 * Implements hook_field_is_empty().
55
 */
56
function feeds_test_field_field_is_empty($item, $field) {
57
  if (empty($item['value'])) {
58
    return TRUE;
59
  }
60
  return FALSE;
61
}
62

    
63
/**
64
 * Implements hook_field_formatter_info().
65
 */
66
function feeds_test_field_field_formatter_info() {
67
  return array(
68
    'feeds_test_field_default' => array(
69
      'label' => t('Default'),
70
      'description' => t('Display the text.'),
71
      'field types' => array('feeds_test_field'),
72
    ),
73
  );
74
}
75

    
76
/**
77
 * Implements hook_field_formatter_view().
78
 */
79
function feeds_test_field_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
80
  $element = array();
81
  foreach ($items as $delta => $item) {
82
    $element[$delta] = array('#markup' => check_plain($item['value']));
83
  }
84
  return $element;
85
}
86

    
87
/**
88
 * Implements hook_field_widget_info().
89
 */
90
function feeds_test_field_field_widget_info() {
91
  return array(
92
    'feeds_test_field_textfield' => array(
93
      'label' => t('Text field'),
94
      'field types' => array('feeds_test_field'),
95
    ),
96
  );
97
}
98

    
99
/**
100
 * Implements hook_field_widget_form().
101
 */
102
function feeds_test_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
103
  $element = $base;
104
  $element['value'] = $base + array(
105
    '#type' => 'textfield',
106
    '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
107
    '#prefix' => '<div class="text-full-wrapper">',
108
    '#suffix' => '</div>',
109
  );
110
  return $element;
111
}