| 9 |
* Provide some verifications on the phone numbers |
* Provide some verifications on the phone numbers |
| 10 |
*/ |
*/ |
| 11 |
|
|
| 12 |
|
|
| 13 |
|
/** |
| 14 |
|
* @addtogroup install |
| 15 |
|
* @{ |
| 16 |
|
*/ |
| 17 |
|
/** |
| 18 |
|
* Implementation of hook_content_notify(). |
| 19 |
|
* |
| 20 |
|
* This hook should be implemented inside hook_install(), hook_uninstall(), |
| 21 |
|
* hook_enable() and hook_disable(), and is used to notify the content |
| 22 |
|
* module when a field module is added or removed so it can respond |
| 23 |
|
* appropriately. One use of this hook is to allow the content module |
| 24 |
|
* to remove fields and field data created by this module when the |
| 25 |
|
* module is uninstalled. |
| 26 |
|
* |
| 27 |
|
* The recommended location for these hooks is in the module's .install file. |
| 28 |
|
*/ |
| 29 |
|
/** |
| 30 |
|
* Implementation of hook_install(). |
| 31 |
|
*/ |
| 32 |
|
function phone_install() { |
| 33 |
|
content_notify('install', 'phone'); |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
/** |
| 37 |
|
* Implementation of hook_uninstall(). |
| 38 |
|
*/ |
| 39 |
|
function phone_uninstall() { |
| 40 |
|
content_notify('uninstall', 'phone'); |
| 41 |
|
} |
| 42 |
|
|
| 43 |
|
/** |
| 44 |
|
* Implementation of hook_enable(). |
| 45 |
|
* |
| 46 |
|
* Notify content module when this module is enabled. |
| 47 |
|
*/ |
| 48 |
|
function phone_enable() { |
| 49 |
|
content_notify('enable', 'phone'); |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
/** |
| 53 |
|
* Implementation of hook_disable(). |
| 54 |
|
* |
| 55 |
|
* Notify content module when this module is disabled. |
| 56 |
|
*/ |
| 57 |
|
function phone_disable() { |
| 58 |
|
content_notify('disable', 'phone'); |
| 59 |
|
} |
| 60 |
|
/** |
| 61 |
|
* @} End of "addtogroup install". |
| 62 |
|
*/ |
| 63 |
|
|
| 64 |
|
|
| 65 |
/** |
/** |
| 66 |
* Implementation of hook_field_info(). |
* Implementation of hook_field_info(). |
| 67 |
|
* |
| 68 |
|
* Here we indicate that the content module will use its default |
| 69 |
|
* handling for the view of this field. |
| 70 |
|
* |
| 71 |
|
* Callbacks can be omitted if default handing is used. |
| 72 |
|
* They're included here just so this module can be used |
| 73 |
|
* as an example for custom modules that might do things |
| 74 |
|
* differently. |
| 75 |
|
* |
| 76 |
|
* If your module will provide its own Views tables or arguments, |
| 77 |
|
* change CONTENT_CALLBACK_DEFAULT to CONTENT_CALLBACK_CUSTOM. |
| 78 |
|
* |
| 79 |
|
* IMPORTANT! - field and widget names will be truncated to 32 characters in |
| 80 |
|
* the database and in internal arrays, like content_fields(). |
| 81 |
*/ |
*/ |
| 82 |
function phone_field_info() { |
function phone_field_info() { |
| 83 |
return array( |
return array( |
| 95 |
} |
} |
| 96 |
|
|
| 97 |
/** |
/** |
| 98 |
|
* Implementation of hook_theme(). |
| 99 |
|
*/ |
| 100 |
|
function phone_theme() { |
| 101 |
|
return array( |
| 102 |
|
'phone_textfield' => array( |
| 103 |
|
'arguments' => array('element' => NULL), |
| 104 |
|
), |
| 105 |
|
'phone_formatter_default' => array( |
| 106 |
|
'arguments' => array('element' => NULL), |
| 107 |
|
), |
| 108 |
|
); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
/** |
| 112 |
* Implementation of hook_field_settings(). |
* Implementation of hook_field_settings(). |
| 113 |
|
* |
| 114 |
|
* Handle the settings for a field. |
| 115 |
|
* |
| 116 |
|
* @param $op |
| 117 |
|
* The operation to be performed. Possible values: |
| 118 |
|
* - "form": Display the field settings form. |
| 119 |
|
* - "validate": Check the field settings form for errors. |
| 120 |
|
* - "save": Declare which fields to save back to the database. |
| 121 |
|
* - "database columns": Declare the columns that content.module should create |
| 122 |
|
* and manage on behalf of the field. If the field module wishes to handle |
| 123 |
|
* its own database storage, this should be omitted. |
| 124 |
|
* - "filters": Declare the Views filters available for the field. |
| 125 |
|
* (this is used in CCK's default Views tables definition) |
| 126 |
|
* They always apply to the first column listed in the "database columns" |
| 127 |
|
* array. |
| 128 |
|
* @param $field |
| 129 |
|
* The field on which the operation is to be performed. |
| 130 |
|
* @return |
| 131 |
|
* This varies depending on the operation. |
| 132 |
|
* - "form": an array of form elements to add to |
| 133 |
|
* the settings page. |
| 134 |
|
* - "validate": no return value. Use form_set_error(). |
| 135 |
|
* - "save": an array of names of form elements to |
| 136 |
|
* be saved in the database. |
| 137 |
|
* - "database columns": an array keyed by column name, with arrays of column |
| 138 |
|
* information as values. This column information must include "type", the |
| 139 |
|
* MySQL data type of the column, and may also include a "sortable" parameter |
| 140 |
|
* to indicate to views.module that the column contains ordered information. |
| 141 |
|
* TODO: Details of other information that can be passed to the database layer can |
| 142 |
|
* be found in the API for the Schema API. |
| 143 |
|
* - "filters": an array of 'filters' definitions as expected by views.module |
| 144 |
|
* (see Views Documentation). |
| 145 |
|
* When providing several filters, it is recommended to use the 'name' |
| 146 |
|
* attribute in order to let the user distinguish between them. If no 'name' |
| 147 |
|
* is specified for a filter, the key of the filter will be used instead. |
| 148 |
*/ |
*/ |
| 149 |
function phone_field_settings($op, $field) { |
function phone_field_settings($op, $field) { |
| 150 |
switch ($op) { |
switch ($op) { |
| 198 |
|
|
| 199 |
/** |
/** |
| 200 |
* Implementation of hook_field(). |
* Implementation of hook_field(). |
| 201 |
|
* |
| 202 |
|
* Define the behavior of a field type. |
| 203 |
|
* |
| 204 |
|
* @param $op |
| 205 |
|
* What kind of action is being performed. Possible values: |
| 206 |
|
* - "load": The node is about to be loaded from the database. This hook |
| 207 |
|
* should be used to load the field. |
| 208 |
|
* - "validate": The user has just finished editing the node and is |
| 209 |
|
* trying to preview or submit it. This hook can be used to check or |
| 210 |
|
* even modify the node. Errors should be set with form_set_error(). |
| 211 |
|
* - "presave": The user has just finished editing the node and the node has |
| 212 |
|
* passed validation. This hook can be used to modify the node. |
| 213 |
|
* - "insert": The node is being created (inserted in the database). |
| 214 |
|
* - "update": The node is being updated. |
| 215 |
|
* - "delete": The node is being deleted. |
| 216 |
|
* @param &$node |
| 217 |
|
* The node the action is being performed on. This argument is passed by |
| 218 |
|
* reference for performance only; do not modify it. |
| 219 |
|
* @param $field |
| 220 |
|
* The field the action is being performed on. |
| 221 |
|
* @param &$node_field |
| 222 |
|
* The contents of the field in this node. Changes to this variable will |
| 223 |
|
* be saved back to the node object. |
| 224 |
|
* @return |
| 225 |
|
* This varies depending on the operation. |
| 226 |
|
* - The "load" operation should return an object containing extra values |
| 227 |
|
* to be merged into the node object. |
| 228 |
|
* - The "insert", "update", "delete", "validate", and "presave" operations |
| 229 |
|
* have no return value. |
| 230 |
|
* |
| 231 |
|
* In most cases, only "validate" operations is relevant ; the rest |
| 232 |
|
* have default implementations in content_field() that usually suffice. |
| 233 |
*/ |
*/ |
| 234 |
function phone_field($op, &$node, $field, &$node_field, $teaser, $page) { |
function phone_field($op, &$node, $field, &$node_field, $teaser, $page) { |
| 235 |
switch ($op) { |
switch ($op) { |
| 236 |
case 'view': |
case 'validate': // corresponds to hook phone_widget validate in phone-5.x |
| 237 |
|
foreach ($node_field as $delta => $item) { |
| 238 |
|
if ($item['value'] != '') { |
| 239 |
|
if ($field['type'] == 'fr_phone' && !valid_phone_number('fr', $item['value'])) { |
| 240 |
|
form_set_error($field['field_name'],t('"%value" is not a valid French phone number<br>French phone numbers should only contain numbers and spaces and be like 99 99 99 99 99', array('%value' => $item['value']))); |
| 241 |
|
} |
| 242 |
|
if ($field['type'] == 'it_phone' && !valid_phone_number('it', $item['value'])) { |
| 243 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Italian phone number<br>Italian phone numbers should only ...', array('%value' => $item['value']))); |
| 244 |
|
} |
| 245 |
|
if ($field['type'] == 'ca_phone' && !valid_phone_number('ca', $item['value'])) { |
| 246 |
|
form_set_error($field['field_name'],t('"%value" is not a valid North American phone number<br>North American Phone numbers should only contain numbers and + and - and ( and ) and spaces and be like 999-999-9999. Please enter a valid ten-digit phone number with optional extension.', array('%value' => $item['value']))); |
| 247 |
|
} |
| 248 |
|
if ($field['type'] == 'cr_phone' && !valid_phone_number('cr', $item['value'])) { |
| 249 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Costa Rican phone number!<br>Costa Rican phone numbers should contain only numbers and spaces be like 99 99 99 99 with an optional prefix of "+506" or "00506".', array('%value' => $item['value']))); |
| 250 |
|
} |
| 251 |
|
if ($field['type'] == 'uk_phone' && !valid_phone_number('uk', $item['value'])) { |
| 252 |
|
form_set_error($field['field_name'],t('"%value" is not a valid British phone number<br>British Phone numbers should .... ', array('%value' => $item['value']))); |
| 253 |
|
} |
| 254 |
|
if ($field['type'] == 'ru_phone' && !valid_phone_number('ru', $item['value'])) { |
| 255 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Russian phone number<br>Russian Phone numbers should .... ', array('%value' => $item['value']))); |
| 256 |
|
} |
| 257 |
|
if ($field['type'] == 'es_phone' && !valid_phone_number('es', $item['value'])) { |
| 258 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Spanish phone number<br>Spanish phone numbers should only contains numbers and spaces and be like 999 999 999', array('%value' => $item['value']))); |
| 259 |
|
} |
| 260 |
|
if ($field['type'] == 'au_phone' && !valid_phone_number('au', $item['value'])) { |
| 261 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Australian phone number<br>Australian phone numbers should contain only numbers with an optional prefix of "+61"', array('%value' => $item['value']))); |
| 262 |
|
} |
| 263 |
|
if ($field['type'] == 'cs_phone' && !valid_phone_number('cs', $item['value'])) { |
| 264 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Czech phone number!<br>Czech phone numbers should contain only numbers and spaces be like 999 999 999 with an optional prefix of "+420" or "00420".', array('%value' => $item['value']))); |
| 265 |
|
} |
| 266 |
|
if ($field['type'] == 'hu_phone' && !valid_phone_number('hu', $item['value'])) { |
| 267 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Hungarian phone number!<br>Hungarian phone numbers should contain only numbers and spaces be like 70 999 9999 with an optional prefix of "+36" or "06".', array('%value' => $item['value']))); |
| 268 |
|
} |
| 269 |
|
} |
| 270 |
|
} |
| 271 |
|
break; |
| 272 |
|
|
| 273 |
|
case 'presave': // corresponds to hook phone_widget 'process form values' in phone-5.x |
| 274 |
foreach ($node_field as $delta => $item) { |
foreach ($node_field as $delta => $item) { |
| 275 |
$node_field[$delta]['view'] = content_format($field, $item, 'default', $node); |
//format the phone number |
| 276 |
|
if ($item['value'] != '') { |
| 277 |
|
if ($field['type'] == 'fr_phone') { |
| 278 |
|
$node_field[$delta]['value'] = format_phone_number('fr', $node_field[$delta]['value'], $field); |
| 279 |
|
} |
| 280 |
|
if ($field['type'] == 'it_phone') { |
| 281 |
|
$node_field[$delta]['value'] = format_phone_number('it', $node_field[$delta]['value'], $field); |
| 282 |
|
} |
| 283 |
|
if ($field['type'] == 'ca_phone') { |
| 284 |
|
$node_field[$delta]['value'] = format_phone_number('ca', $node_field[$delta]['value'], $field); |
| 285 |
|
} |
| 286 |
|
if ($field['type'] == 'cr_phone') { |
| 287 |
|
$node_field[$delta]['value'] = format_phone_number('cr', $node_field[$delta]['value'], $field); |
| 288 |
|
} |
| 289 |
|
if ($field['type'] == 'uk_phone') { |
| 290 |
|
$node_field[$delta]['value'] = format_phone_number('uk', $node_field[$delta]['value'], $field); |
| 291 |
|
} |
| 292 |
|
if ($field['type'] == 'ru_phone') { |
| 293 |
|
$node_field[$delta]['value'] = format_phone_number('ru', $node_field[$delta]['value'], $field); |
| 294 |
|
} |
| 295 |
|
if ($field['type'] == 'es_phone') { |
| 296 |
|
$node_field[$delta]['value'] = format_phone_number('es', $node_field[$delta]['value'], $field); |
| 297 |
|
} |
| 298 |
|
if ($field['type'] == 'au_phone') { |
| 299 |
|
$node_field[$delta]['value'] = format_phone_number('au', $node_field[$delta]['value'], $field); |
| 300 |
|
} |
| 301 |
|
if ($field['type'] == 'cs_phone') { |
| 302 |
|
$node_field[$delta]['value'] = format_phone_number('cs', $node_field[$delta]['value'], $field); |
| 303 |
|
} |
| 304 |
|
if ($field['type'] == 'hu_phone') { |
| 305 |
|
$node_field[$delta]['value'] = format_phone_number('hu', $node_field[$delta]['value'], $field); |
| 306 |
|
} |
| 307 |
|
} |
| 308 |
} |
} |
| 309 |
return theme('field', $node, $field, $node_field, $teaser, $page); |
break; |
| 310 |
} |
} |
| 311 |
} |
} |
| 312 |
|
|
| 322 |
*/ |
*/ |
| 323 |
|
|
| 324 |
/** |
/** |
| 325 |
*Implementation of hook_field_formatter_info |
*Implementation of hook_field_formatter_info(). |
| 326 |
|
* |
| 327 |
|
* The default behavior of formatters is that they will create |
| 328 |
|
* a theme for a single field value. |
| 329 |
|
* |
| 330 |
|
* Setting 'multiple values' to CONTENT_HANDLE_FIELD will create |
| 331 |
|
* a formatter that will receive all the values of a field so you |
| 332 |
|
* can, for instance, plot all the values on a map or in a graph. |
| 333 |
|
* |
| 334 |
|
* The 'view' operation (handled by the Content module) constructs the |
| 335 |
|
* $node in a way that you can use drupal_render() to display the |
| 336 |
|
* formatted output for an individual field. |
| 337 |
|
* |
| 338 |
|
* i.e. print drupal_render($node->field_foo); |
| 339 |
|
* |
| 340 |
|
* The code now supports both single value formatters, which theme an |
| 341 |
|
* individual item value as has been done in previous version of CCK, |
| 342 |
|
* and multiple value formatters, which theme all values for the field |
| 343 |
|
* in a single theme. The multiple value formatters could be used, for |
| 344 |
|
* instance, to plot field values on a single map or display them |
| 345 |
|
* in a graph. Single value formatters are the default, multiple value |
| 346 |
|
* formatters can be designated as such in formatter_info(). |
| 347 |
|
* |
| 348 |
|
* The node array will look like: |
| 349 |
|
* |
| 350 |
|
* 'Single value' formatter: |
| 351 |
|
* $node->content['field_foo'] = array( |
| 352 |
|
* '#type' => 'content_field', |
| 353 |
|
* '#title' => 'label' |
| 354 |
|
* '#field_name' => 'field_name', |
| 355 |
|
* '#node' => $node, |
| 356 |
|
* 'items' => |
| 357 |
|
* 0 => array( |
| 358 |
|
* '#theme' => $theme, |
| 359 |
|
* '#field_name' => 'field_name', |
| 360 |
|
* '#type_name' => $node->type, |
| 361 |
|
* '#formatter' => $formatter_name, |
| 362 |
|
* '#item' => $items[0], |
| 363 |
|
* ), |
| 364 |
|
* 1 => array( |
| 365 |
|
* '#theme' => $theme, |
| 366 |
|
* '#field_name' => 'field_name', |
| 367 |
|
* '#type_name' => $node->type, |
| 368 |
|
* '#formatter' => $formatter_name, |
| 369 |
|
* '#item' => $items[1], |
| 370 |
|
* ), |
| 371 |
|
* ), |
| 372 |
|
* ); |
| 373 |
|
* 'Multiple value' formatter: |
| 374 |
|
* $node->content['field_foo'] = array( |
| 375 |
|
* '#type' => 'content_field', |
| 376 |
|
* '#title' => 'label' |
| 377 |
|
* '#field_name' => 'field_name', |
| 378 |
|
* '#node' => $node, |
| 379 |
|
* 'items' => array( |
| 380 |
|
* '#theme' => $theme, |
| 381 |
|
* '#field_name' => 'field_name', |
| 382 |
|
* '#type_name' => $node->type, |
| 383 |
|
* '#formatter' => $formatter_name, |
| 384 |
|
* 0 => array( |
| 385 |
|
* '#item' => $items[0], |
| 386 |
|
* ), |
| 387 |
|
* 1 => array( |
| 388 |
|
* '#item' => $items[1], |
| 389 |
|
* ), |
| 390 |
|
* ), |
| 391 |
|
* ); |
| 392 |
*/ |
*/ |
| 393 |
function phone_field_formatter_info() { |
function phone_field_formatter_info() { |
| 394 |
return array( |
return array( |
| 405 |
'cs_phone', |
'cs_phone', |
| 406 |
'hu_phone' |
'hu_phone' |
| 407 |
), |
), |
| 408 |
|
'multiple values' => CONTENT_HANDLE_CORE, |
| 409 |
), |
), |
| 410 |
); |
); |
| 411 |
} |
} |
| 412 |
|
|
| 413 |
/** |
/** |
| 414 |
* Implementation of hook_field_formatter(). |
* Implementation of hook_field_formatter(). |
| 415 |
**/ |
* |
| 416 |
|
* Prepare an individual item for viewing in a browser. |
| 417 |
|
* |
| 418 |
|
* @param $field |
| 419 |
|
* The field the action is being performed on. |
| 420 |
|
* @param $item |
| 421 |
|
* An array, keyed by column, of the data stored for this item in this field. |
| 422 |
|
* @param $formatter |
| 423 |
|
* The name of the formatter being used to display the field. |
| 424 |
|
* @param $node |
| 425 |
|
* The node object, for context. Will be NULL in some cases. |
| 426 |
|
* Warning: when displaying field retrieved by Views, $node will not |
| 427 |
|
* be a "full-fledged" node object, but an object containg the data returned |
| 428 |
|
* by the Views query (at least nid, vid, changed) |
| 429 |
|
* @return |
| 430 |
|
* An HTML string containing the formatted item. |
| 431 |
|
* |
| 432 |
|
* In a multiple-value field scenario, this function will be called once per |
| 433 |
|
* value currently stored in the field. This function is also used as the handler |
| 434 |
|
* for viewing a field in a views.module tabular listing. |
| 435 |
|
* |
| 436 |
|
* It is important that this function at the minimum perform security |
| 437 |
|
* transformations such as running check_plain() or check_markup(). |
| 438 |
|
*/ |
| 439 |
function phone_field_formatter($field, $item, $formatter, $node) { |
function phone_field_formatter($field, $item, $formatter, $node) { |
| 440 |
if (!isset($item['value'])) { |
if (!isset($item['value'])) { |
| 441 |
return ''; |
return ''; |
| 455 |
|
|
| 456 |
/** |
/** |
| 457 |
* Implementation of hook_widget_info(). |
* Implementation of hook_widget_info(). |
| 458 |
|
* |
| 459 |
|
* Here we indicate that the content module will handle |
| 460 |
|
* the default value and multiple values for these widgets. |
| 461 |
|
* |
| 462 |
|
* Callbacks can be omitted if default handing is used. |
| 463 |
|
* They're included here just so this module can be used |
| 464 |
|
* as an example for custom modules that might do things |
| 465 |
|
* differently. |
| 466 |
|
* |
| 467 |
|
* IMPORTANT! - field and widget names will be truncated to 32 characters in |
| 468 |
|
* the database and in internal arrays, like content_fields(). |
| 469 |
*/ |
*/ |
| 470 |
function phone_widget_info() { |
function phone_widget_info() { |
| 471 |
return array( |
return array( |
| 472 |
'phone' => array( |
'phone_textfield' => array( |
| 473 |
'label' => t('Textfield'), |
'label' => t('Textfield'), |
| 474 |
'field types' => array('fr_phone', |
'field types' => array('fr_phone', |
| 475 |
'it_phone', |
'it_phone', |
| 482 |
'cs_phone', |
'cs_phone', |
| 483 |
'hu_phone' |
'hu_phone' |
| 484 |
), |
), |
| 485 |
|
'multiple values' => CONTENT_HANDLE_CORE, |
| 486 |
|
'callbacks' => array( |
| 487 |
|
'default value' => CONTENT_CALLBACK_DEFAULT, |
| 488 |
|
), |
| 489 |
), |
), |
| 490 |
); |
); |
| 491 |
} |
} |
| 492 |
|
|
| 493 |
/** |
/** |
| 494 |
* Implementation of hook_widget_settings(). |
* Implementation of hook_widget_settings(). |
| 495 |
|
* |
| 496 |
|
* Handle the parameters for a widget. |
| 497 |
|
* |
| 498 |
|
* @param $op |
| 499 |
|
* The operation to be performed. Possible values: |
| 500 |
|
* - "form": Display the widget settings form. |
| 501 |
|
* - "validate": Check the widget settings form for errors. |
| 502 |
|
* - "save": Declare which pieces of information to save back to the database. |
| 503 |
|
* @param $widget |
| 504 |
|
* The widget on which the operation is to be performed. |
| 505 |
|
* @return |
| 506 |
|
* This varies depending on the operation. |
| 507 |
|
* - "form": an array of form elements to add to the settings page. |
| 508 |
|
* - "validate": no return value. Use form_set_error(). |
| 509 |
|
* - "save": an array of names of form elements to be saved in the database. |
| 510 |
*/ |
*/ |
| 511 |
function phone_widget_settings($op, $widget) { |
function phone_widget_settings($op, $widget) { |
| 512 |
switch ($op) { |
switch ($op) { |
| 522 |
|
|
| 523 |
/** |
/** |
| 524 |
* Implementation of hook_widget(). |
* Implementation of hook_widget(). |
| 525 |
|
* |
| 526 |
|
* Attach a single form element to the form. It will be built out and |
| 527 |
|
* validated in the callback(s) listed in hook_elements. We build it |
| 528 |
|
* out in the callbacks rather than here in hook_widget so it can be |
| 529 |
|
* plugged into any module that can provide it with valid |
| 530 |
|
* $field information. |
| 531 |
|
* |
| 532 |
|
* Content module will set the weight, field name and delta values |
| 533 |
|
* for each form element. This is a change from earlier CCK versions |
| 534 |
|
* where the widget managed its own multiple values. |
| 535 |
|
* |
| 536 |
|
* If there are multiple values for this field, the content module will |
| 537 |
|
* call this function as many times as needed. |
| 538 |
|
* |
| 539 |
|
* @param $form |
| 540 |
|
* the entire form array, $form['#node'] holds node information |
| 541 |
|
* @param $form_state |
| 542 |
|
* the form_state, $form_state['values'][$field['field_name']] |
| 543 |
|
* holds the field's form values. |
| 544 |
|
* @param $field |
| 545 |
|
* the field array |
| 546 |
|
* @param $items |
| 547 |
|
* array of default values for this field |
| 548 |
|
* @param $delta |
| 549 |
|
* the order of this item in the array of subelements (0, 1, 2, etc) |
| 550 |
|
* |
| 551 |
|
* @return |
| 552 |
|
* the form item for a single element for this field |
| 553 |
*/ |
*/ |
| 554 |
function phone_widget($op, &$node, $field, &$node_field) { |
function phone_widget(&$form, &$form_state, $field, $items, $delta = 0) { |
| 555 |
switch ($op) { |
$element = array( |
| 556 |
case 'form': |
'#type' => $field['widget']['type'], |
| 557 |
$form = array(); |
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL, |
| 558 |
|
); |
| 559 |
$form[$field['field_name']] = array('#tree' => TRUE); |
return $element; |
| 560 |
|
} |
|
if ($field['multiple']) { |
|
|
$form[$field['field_name']]['#type'] = 'fieldset'; |
|
|
$form[$field['field_name']]['#title'] = t($field['widget']['label']); |
|
|
foreach (range(0,2) as $delta) { |
|
|
$form[$field['field_name']][$delta]['value'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => '', |
|
|
'#default_value' => isset($node_field[$delta]['value']) ? $node_field[$delta]['value'] : '', |
|
|
'#required' => $field['required'] ? $field['required'] : FALSE, |
|
|
'#maxlength' => 255, |
|
|
'#weight' => $field['widget']['weight'], |
|
|
'#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 20, |
|
|
'#description' => $field['widget']['description'], |
|
|
); |
|
|
} |
|
|
} |
|
|
else { |
|
|
$form[$field['field_name']][0]['value'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => $field['widget']['label'], |
|
|
'#default_value' => isset($node_field[0]['value']) ? $node_field[0]['value'] : '', |
|
|
'#required' => $field['required'] ? $field['required'] : FALSE, |
|
|
'#maxlength' => 255, |
|
|
'#weight' => $field['widget']['weight'], |
|
|
'#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 20, |
|
|
'#description' => $field['widget']['description'], |
|
|
); |
|
|
} |
|
|
|
|
|
return $form; |
|
|
|
|
|
case 'process form values': |
|
|
if (is_array($node_field)) { |
|
|
foreach ($node_field as $delta => $item) { |
|
|
//format the phone number |
|
|
if ($item['value'] != '') |
|
|
{ |
|
|
if ($field['type'] == 'fr_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('fr', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'it_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('it', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'ca_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('ca', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'cr_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('cr', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'uk_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('uk', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'ru_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('ru', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'es_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('es', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'au_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('au', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'cs_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('cs', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'hu_phone') { |
|
|
$node_field[0]['value'] = format_phone_number('hu', $node_field[0]['value'], $field); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
break; |
|
|
|
|
|
case 'validate': |
|
|
if (is_array($node_field)) { |
|
|
foreach ($node_field as $delta => $item) { |
|
| 561 |
|
|
| 562 |
if ($item['value'] != '') |
|
| 563 |
{ |
|
| 564 |
if ($field['type'] == 'fr_phone' && !valid_phone_number('fr', $item['value'])) { |
|
| 565 |
form_set_error($field['field_name'],t('"%value" is not a valid French phone number<br>French phone numbers should only contain numbers and spaces and be like 99 99 99 99 99', array('%value' => $item['value']))); |
/** |
| 566 |
} |
* Implementation of hook_content_is_empty(). |
| 567 |
if ($field['type'] == 'it_phone' && !valid_phone_number('it', $item['value'])) { |
* |
| 568 |
form_set_error($field['field_name'],t('"%value" is not a valid Italian phone number<br>Italian phone numbers should only ...', array('%value' => $item['value']))); |
* NEW REQUIRED HOOK! |
| 569 |
} |
* |
| 570 |
if ($field['type'] == 'ca_phone' && !valid_phone_number('ca', $item['value'])) { |
* This function tells the content module whether or not to consider |
| 571 |
form_set_error($field['field_name'],t('"%value" is not a valid North American phone number<br>North American Phone numbers should only contain numbers and + and - and ( and ) and spaces and be like 999-999-9999. Please enter a valid ten-digit phone number with optional extension.', array('%value' => $item['value']))); |
* the $item to be empty. This is used by the content module |
| 572 |
} |
* to remove empty, non-required values before saving them. |
| 573 |
if ($field['type'] == 'cr_phone' && !valid_phone_number('cr', $item['value'])) { |
*/ |
| 574 |
form_set_error($field['field_name'],t('"%value" is not a valid Costa Rican phone number!<br>Costa Rican phone numbers should contain only numbers and spaces be like 99 99 99 99 with an optional prefix of "+506" or "00506".', array('%value' => $item['value']))); |
function phone_content_is_empty($item, $field) { |
| 575 |
} |
return FALSE; |
| 576 |
if ($field['type'] == 'uk_phone' && !valid_phone_number('uk', $item['value'])) { |
} |
| 577 |
form_set_error($field['field_name'],t('"%value" is not a valid British phone number<br>British Phone numbers should .... ', array('%value' => $item['value']))); |
|
| 578 |
} |
/** |
| 579 |
if ($field['type'] == 'ru_phone' && !valid_phone_number('ru', $item['value'])) { |
* Implementation of FAPI hook_elements(). |
| 580 |
form_set_error($field['field_name'],t('"%value" is not a valid Russian phone number<br>Russian Phone numbers should .... ', array('%value' => $item['value']))); |
* |
| 581 |
} |
* Any FAPI callbacks needed for individual widgets can be declared here, |
| 582 |
if ($field['type'] == 'es_phone' && !valid_phone_number('es', $item['value'])) { |
* and the element will be passed to those callbacks for processing. |
| 583 |
form_set_error($field['field_name'],t('"%value" is not a valid Spanish phone number<br>Spanish phone numbers should only contains numbers and spaces and be like 999 999 999', array('%value' => $item['value']))); |
* |
| 584 |
} |
* Drupal will automatically theme the element using a theme with |
| 585 |
if ($field['type'] == 'au_phone' && !valid_phone_number('au', $item['value'])) { |
* the same name as the hook_elements key. |
| 586 |
form_set_error($field['field_name'],t('"%value" is not a valid Australian phone number<br>Australian phone numbers should contain only numbers with an optional prefix of "+61"', array('%value' => $item['value']))); |
* |
| 587 |
} |
* Autocomplete_path is not used by text_widget but other widgets can use it |
| 588 |
if ($field['type'] == 'cs_phone' && !valid_phone_number('cs', $item['value'])) { |
* (see nodereference and userreference). |
| 589 |
form_set_error($field['field_name'],t('"%value" is not a valid Czech phone number!<br>Czech phone numbers should contain only numbers and spaces be like 999 999 999 with an optional prefix of "+420" or "00420".', array('%value' => $item['value']))); |
*/ |
| 590 |
} |
function phone_elements() { |
| 591 |
if ($field['type'] == 'hu_phone' && !valid_phone_number('hu', $item['value'])) { |
return array( |
| 592 |
form_set_error($field['field_name'],t('"%value" is not a valid Hungarian phone number!<br>Hungarian phone numbers should contain only numbers and spaces be like 70 999 9999 with an optional prefix of "+36" or "06".', array('%value' => $item['value']))); |
'phone_textfield' => array( |
| 593 |
} |
'#input' => TRUE, |
| 594 |
} |
'#columns' => array('value'), '#delta' => 0, |
| 595 |
} |
'#process' => array('phone_textfield_process'), |
| 596 |
} |
'#autocomplete_path' => FALSE, |
| 597 |
break; |
), |
| 598 |
} |
); |
| 599 |
|
} |
| 600 |
|
|
| 601 |
|
/** |
| 602 |
|
* FAPI theme for an individual text elements. |
| 603 |
|
* |
| 604 |
|
* The textfield or textarea is already rendered by the |
| 605 |
|
* textfield or textarea themes and the html output |
| 606 |
|
* lives in $element['#children']. Override this theme to |
| 607 |
|
* make custom changes to the output. |
| 608 |
|
* |
| 609 |
|
* $element['#field_name'] contains the field name |
| 610 |
|
* $element['#delta] is the position of this element in the group |
| 611 |
|
*/ |
| 612 |
|
function theme_phone_textfield($element) { |
| 613 |
|
return $element['#children']; |
| 614 |
|
} |
| 615 |
|
|
| 616 |
|
/** |
| 617 |
|
* Process an individual element. |
| 618 |
|
* |
| 619 |
|
* Build the form element. When creating a form using FAPI #process, |
| 620 |
|
* note that $element['#value'] is already set. |
| 621 |
|
* |
| 622 |
|
* The $fields array is in $form['#field_info'][$element['#field_name']]. |
| 623 |
|
*/ |
| 624 |
|
function phone_textfield_process($element, $edit, $form_state, $form) { |
| 625 |
|
$field = $form['#field_info'][$element['#field_name']]; |
| 626 |
|
$field_key = $element['#columns'][0]; |
| 627 |
|
$delta = $element['#delta']; |
| 628 |
|
$element[$field_key] = array( |
| 629 |
|
'#type' => 'textfield', |
| 630 |
|
'#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL, |
| 631 |
|
'#autocomplete_path' => FALSE, |
| 632 |
|
// The following values were set by the content module and need |
| 633 |
|
// to be passed down to the nested element. |
| 634 |
|
'#title' => $element['#title'], |
| 635 |
|
'#description' => $element['#description'], |
| 636 |
|
'#required' => $element['#required'], |
| 637 |
|
'#field_name' => $element['#field_name'], |
| 638 |
|
'#type_name' => $element['#type_name'], |
| 639 |
|
'#delta' => $element['#delta'], |
| 640 |
|
'#columns' => $element['#columns'], |
| 641 |
|
); |
| 642 |
|
|
| 643 |
|
if (!empty($field['max_length'])) { |
| 644 |
|
$element[$field_key]['#maxlength'] = $field['max_length']; |
| 645 |
|
} |
| 646 |
|
if (!empty($field['text_processing'])) { |
| 647 |
|
$filter_key = $element['#columns'][1]; |
| 648 |
|
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT; |
| 649 |
|
$parents = array_merge($element['#parents'] , array($filter_key)); |
| 650 |
|
$element[$filter_key] = filter_form($format, 1, $parents); |
| 651 |
|
} |
| 652 |
|
|
| 653 |
|
// Used so that hook_field('validate') knows where to flag an error. |
| 654 |
|
$element['_error_element'] = array( |
| 655 |
|
'#type' => 'value', |
| 656 |
|
'#value' => implode('][', array_merge($element['#parents'], array($field_key))), |
| 657 |
|
); |
| 658 |
|
|
| 659 |
|
return $element; |
| 660 |
|
} |
| 661 |
|
|
| 662 |
|
/** |
| 663 |
|
* Theme function for 'default' text field formatter. |
| 664 |
|
*/ |
| 665 |
|
function theme_phone_formatter_default($element) { |
| 666 |
|
return $element['#item']['value']; |
| 667 |
} |
} |
| 668 |
|
|
| 669 |
/** |
/** |
| 693 |
//drupal_set_message('langue = ' . $countrycode, 'error'); |
//drupal_set_message('langue = ' . $countrycode, 'error'); |
| 694 |
|
|
| 695 |
$valid_phone_function = 'valid_'. $countrycode . '_phone_number'; |
$valid_phone_function = 'valid_'. $countrycode . '_phone_number'; |
| 696 |
include_once('./'. drupal_get_path('module', 'phone') . '/phone.'. $countrycode . '.inc'); |
module_load_include('inc', 'phone', './'. drupal_get_path('module', 'phone') . '/phone.'. $countrycode); |
| 697 |
|
|
| 698 |
if (function_exists($valid_phone_function)) { |
if (function_exists($valid_phone_function)) { |
| 699 |
return $valid_phone_function($phonenumber); |
return $valid_phone_function($phonenumber); |
| 735 |
//drupal_set_message('langue = ' . $countrycode, 'error'); |
//drupal_set_message('langue = ' . $countrycode, 'error'); |
| 736 |
|
|
| 737 |
$format_phone_function = 'format_'. $countrycode . '_phone_number'; |
$format_phone_function = 'format_'. $countrycode . '_phone_number'; |
| 738 |
include_once('./'. drupal_get_path('module', 'phone') . '/phone.'. $countrycode . '.inc'); |
module_load_include('inc', 'phone', './'. drupal_get_path('module', 'phone') . '/phone.'. $countrycode); |
| 739 |
|
|
| 740 |
if (function_exists($format_phone_function)) { |
if (function_exists($format_phone_function)) { |
| 741 |
return $format_phone_function($phonenumber, $field); |
return $format_phone_function($phonenumber, $field); |
| 750 |
} |
} |
| 751 |
} |
} |
| 752 |
|
|
| 753 |
|
/** |
| 754 |
|
* Implementation of hook token_list |
| 755 |
|
*/ |
| 756 |
|
function phone_token_list($type = 'all') { |
| 757 |
|
if ($type == 'field' || $type == 'all') { |
| 758 |
|
$tokens['phone']['raw'] = t('Raw phone numbers'); |
| 759 |
|
$tokens['phone']['formatted'] = t('Formatted phone numbers'); |
| 760 |
|
return $tokens; |
| 761 |
|
} |
| 762 |
|
} |
| 763 |
|
|
| 764 |
|
/** |
| 765 |
|
* Implementation of hook token_values |
| 766 |
|
*/ |
| 767 |
|
function phone_token_values($type, $object = NULL, $options = array()) { |
| 768 |
|
if ($type == 'field') { |
| 769 |
|
$item = $object[0]; |
| 770 |
|
$tokens['raw'] = $item['phone']; |
| 771 |
|
$tokens['formatted'] = $item['view']; |
| 772 |
|
return $tokens; |
| 773 |
|
} |
| 774 |
|
} |
| 775 |
|
|