contact_field.module 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * A contact field for use with contact.module
  5. *
  6. * - can be used as just a formatter for the user object
  7. */
  8. function contact_field_field_formatter_info() {
  9. $ret = array(
  10. 'user_contact_form_link' => array(
  11. 'label' => t('Contact form'),
  12. 'field types' => array('user_contact'),
  13. ),
  14. 'user_contact_mailto_link' => array(
  15. 'label' => t('mailto: link'),
  16. 'field types' => array('user_contact'),
  17. ),
  18. );
  19. // dsm(get_defined_vars(), __FUNCTION__);
  20. return $ret;
  21. }
  22. function contact_field_field_info() {
  23. $ret = array(
  24. 'user_contact' => array(
  25. 'label' => t('User contact'),
  26. 'description' => t('This field builds a contact link from the entity to which it is attached, as long as that entity has a uid field linking to a user account'),
  27. 'settings' => array(),
  28. 'instance settings' => array(),
  29. 'default_widget' => NULL,
  30. 'default_formatter' => 'user_contact_link',
  31. 'cardinality' => 1,
  32. ),
  33. );
  34. // dsm(get_defined_vars(), __FUNCTION__);
  35. return $ret;
  36. }
  37. /**
  38. * Implements hook_field_is_empty().
  39. */
  40. function contact_field_field_is_empty($item, $field) {
  41. return empty($item);
  42. }
  43. function contact_field_field_schema($field) {
  44. if ($field['type'] != 'user_contact') {
  45. return;
  46. }
  47. $ret = array(
  48. 'columns' => array(),
  49. 'indexes' => array(),
  50. );
  51. // dsm(get_defined_vars(), __FUNCTION__);
  52. return $ret;
  53. }
  54. function contact_field_field_widget_info() {
  55. $ret = array(
  56. 'contact_field_simple' => array(
  57. 'label' => t('No input necessary'),
  58. 'description' => t('Take the information from the underlying account'),
  59. 'field types' => array('user_contact'),
  60. 'behaviors' => array(
  61. 'multiple values' => FIELD_BEHAVIOR_CUSTOM, // No values input anyway
  62. 'default value' => FIELD_BEHAVIOR_DEFAULT,
  63. ),
  64. ),
  65. );
  66. // dsm(get_defined_vars(), __FUNCTION__);
  67. return $ret;
  68. }
  69. function contact_field_field_widget_form(&$form, &$form_state, $field,
  70. $instance, $langcode, $items, $delta, $element) {
  71. // dsm(get_defined_vars(), __FUNCTION__);
  72. $ret = array();
  73. if (isset($form['uid'])) {
  74. // This is not the widget settings form
  75. $uid = $form['uid']['#value'];
  76. $account = user_load($uid);
  77. $ret['mail'] = array(
  78. '#type' => 'value',
  79. '#title' => $instance['label'],
  80. '#disabled' => TRUE,
  81. '#value' => $account->mail,
  82. );
  83. }
  84. return $ret;
  85. }
  86. /**
  87. * Implements hook_field_formatter_view().
  88. */
  89. function contact_field_field_formatter_view($obj_type, $object, $field,
  90. $instance, $langcode, $items, $display) {
  91. $uid = $object->uid;
  92. $name = $object->name;
  93. $ret = array();
  94. switch ($display['type']) {
  95. case 'user_contact_form_link':
  96. foreach (array_keys($items) as $delta) {
  97. // Cannot use theme_username() in D7.7. See http://drupal.org/node/1238094
  98. $ret[$delta] = array('#markup' => l($name, "user/$uid/contact"));
  99. }
  100. break;
  101. case 'user_contact_mailto_link':
  102. $account = user_load($uid);
  103. $mail = $account->mail;
  104. foreach (array_keys($items) as $delta) {
  105. // Cannot use theme_username() in D7.7. See http://drupal.org/node/1238094
  106. $ret[$delta] = array('#markup' => l($name, "mailto:$mail"));
  107. }
  108. break;
  109. default:
  110. return;
  111. }
  112. $ret = array($instance['id'] => $ret);
  113. // dsm(get_defined_vars(), __FUNCTION__);
  114. return $ret;
  115. }
  116. /**
  117. * Force cardinality: there is no point in having multiple instances of this
  118. * field since it is single-valued per entity instance, instead of having
  119. * standalone storage.
  120. */
  121. function contact_field_form_field_ui_field_edit_form_alter(&$form, $form_state, $form_id) {
  122. dsm($form);
  123. if ($form['#field']['type'] == 'user_contact') {
  124. $override = array(
  125. '#access' => FALSE,
  126. '#value' => 1,
  127. );
  128. $form['field']['cardinality'] = $override + $form['field']['cardinality'];
  129. }
  130. }