summaryrefslogtreecommitdiffstats
path: root/simple_field.api.php
blob: b27a221e521df5f6aaf63e372161eb8f58e9b3b3 (plain)
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
<?php
/**
 * @file
 * Hooks provided by the Simple Field module.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Allow modules to specify custom types of simple fields.
 *
 * @return
 *   An array whose keys are simple field type names and whose values are arrays
 *   containing the keys. All optional except 'field'.
 *   - field: The Field module field type machine name.
 *   - label: The short label for this simple field type or the
 *     machine name if not specified.
 *   - help: The long description of the behavior of the simple
 *     field type or blank if not specified.
 *   - widget: The Field module widget type. Will use field default
 *     if not specified.
 *   - formatter: The Field module formatter type. Will use the field
 *     default formatter if not specified.
 *   - cardinality: The cardinality of the field. Unlimited if blank.
 *   - defaults: An array of settings to override the default Field
 *     module settings. Keyed by the type of setting.
 *     - field_settings: Settings to override values returned from calling
 *       field_info_field_settings($field_type);
 *     - instance_settings: Settings to override values returned from calling
 *       field_info_instance_settings($field_type);
 *     - widget_settings: Settings to override values returned from calling
 *       field_info_widget_settings($widget_type);
 *     - formatter_settings: Settings to override values returned from calling
 *       field_info_formatter_settings($formatter_type);
 */
function hook_simple_field_type_info() {
  return array(
    'simple_field_yesno' => array(
      // There are a ton more options for this, but they are set to defaults.
      // @see simple_field_type_info()
      'label' => t('Yes/No'),
      'help' => t('Display a set of Yes and No radio buttons to allow the user a boolean choice.'),
      'field' => 'list_boolean',
      'cardinality' => 1,
      'defaults' => array(
        'field_settings' => array(
          'allowed_values' => array(
            1 => 'Yes',
            0 => 'No',
          ),
        ),
      ),
    ),
    'simple_field_multichoice_single' => array(
      'label' => t('Multiple Choice (Choose 1 Options)'),
      'help' => t('Enter a list of options and allow the user to select a single options.'),
      'field' => 'list_text',
      'widget' => 'options_buttons',
      'cardinality' => 1,
    ),
    'simple_field_multichoice_multi' => array(
      'label' => t('Multiple Choice (Choose many Options)'),
      'help' => t('Enter a list of options and allow the user to select as many as they would like.'),
      'field' => 'list_text',
      'widget' => 'options_buttons',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    ),
    'simple_field_short_answer' => array(
      'label' => t('Short Answer'),
      'help' => t('Allow the user to enter a single line of text.'),
      'cardinality' => 1,
      'field' => 'text',
    ),
    'simple_field_long_answer' => array(
      'label' => t('Long Answer'),
      'help' => t('Allow the user to enter a large block of text.'),
      'cardinality' => 1,
      'field' => 'text_long',
      'widget' => 'text_textarea',
    ),
    'simple_field_single_date' => array(
      'label' => t('Date'),
      'help' => t('Allow the user to enter a single Date value.'),
      'field' => 'datestamp',
      'widget' => 'date_text',
      'cardinality' => 1,
      'defaults' => array(
        'field_settings' => array(
          'granularity' => array(
            'year' => 'year',
            'month' => 'month',
            'day' => 'day',
          ),
          'tz_handling' => 'none',
        ),
        'instance_settings' => array(
          'default_value' => 'blank',
        ),
        'widget_settings' => array(
          'input_format_custom' => 'Y-m-d',
        ),
      ),
    ),
    'simple_field_date_range' => array(
      'label' => t('Date Range'),
      'help' => t('Allow the user to enter a Date range with a beginning and ending date.'),
      'field' => 'datestamp',
      'widget' => 'date_text',
      'cardinality' => 1,
      'defaults' => array(
        'field_settings' => array(
          'todate' => 'required',
          'granularity' => array(
            'year' => 'year',
            'month' => 'month',
            'day' => 'day',
          ),
          'tz_handling' => 'none',
        ),
        'instance_settings' => array(
          'default_value' => 'blank',
        ),
        'widget_settings' => array(
          'input_format_custom' => 'Y-m-d',
        ),
      ),
    ),
    'simple_field_integer' => array(
      'label' => t('Integer'),
      'help' => t('Allow the user to enter a single single integer value.'),
      'cardinality' => 1,
      'field' => 'number_integer',
    ),
  );
}

/**
 * Allow modules to add elements to the simple field form.
 *
 * This hook allows modules to provide form elements in order to allow users to
 * change field settings. The hook is only called on the module that provided
 * the simple field type, so any module looking to alter these settings should
 * use hook_form_alter.
 *
 * @param SimpleField $simplefield
 *   The simple_field entity that is being added/edited by this form.
 * @param bool $has_data
 *   Boolean indicating whether this field has data associated with it. This is
 *   used to disable modification of certain settings which cannot be changes
 *   once the field schema becomes unchangeable.
 *
 * @return
 *   A renderable form array which will be appended to the add/edit form. The
 *   form elements must be set up so that the settings can be properly split up
 *   when saved. However you want to structure the form, in the end
 *   $form_state['values'] must contain 'field_settings', 'instance_settings',
 *   'widget_settings' and/or 'formatter_settings' keys, and the values must be
 *   in the right place so they can be properly merged back into their
 *   respecting settings arrays.
 */
function hook_simple_field_type_form($simplefield, $has_data) {
  $form = array();
  $type_info = $simplefield->getTypeInfo();
  $field_settings = $simplefield->getSettings('field');

  switch ($type_info['type']) {
    case 'simple_field_yesno':
      break;
    case 'simple_field_multichoice_single':
    case 'simple_field_multichoice_multi':
      $form['field_settings']['somefieldsetting'] = array(
        '#type' => 'textfield',
        '#title' => t('Field specific setting'),
        '#default_value' => $field_setting['somefieldsetting'],
      );
      break;
    case 'simple_field_short_answer':
    case 'simple_field_long_answer':
    case 'simple_field_single_date':
    case 'simple_field_date_range':
  }

  return $form;
}

/**
 * @} End of "addtogroup hooks".
 */