Browse Source

Initial commit.

Frederic G. MARAND 12 years ago
commit
5038b79963
2 changed files with 158 additions and 0 deletions
  1. 7 0
      contact_field.info
  2. 151 0
      contact_field.module

+ 7 - 0
contact_field.info

@@ -0,0 +1,7 @@
+name = "Contact Field"
+dependencies[] = "field"
+description = "Provides a direct link to a user contact form."
+package = "Fields"
+core = "7.x"
+
+dependencies[] = contact

+ 151 - 0
contact_field.module

@@ -0,0 +1,151 @@
+<?php
+/**
+ * @file
+ * A contact field for use with contact.module
+ *
+ * - can be used as just a formatter for the user object
+ */
+
+function contact_field_field_formatter_info() {
+  $ret = array(
+    'user_contact_form_link' => array(
+      'label' => t('Contact form'),
+      'field types' => array('user_contact'),
+    ),
+    'user_contact_mailto_link' => array(
+      'label' => t('mailto: link'),
+      'field types' => array('user_contact'),
+    ),
+  );
+
+  // dsm(get_defined_vars(), __FUNCTION__);
+  return $ret;
+}
+
+function contact_field_field_info() {
+  $ret = array(
+    'user_contact' => array(
+      'label' => t('User contact'),
+      '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'),
+      'settings' => array(),
+      'instance settings' => array(),
+      'default_widget' => NULL,
+      'default_formatter' => 'user_contact_link',
+      'cardinality' => 1,
+    ),
+  );
+
+  // dsm(get_defined_vars(), __FUNCTION__);
+  return $ret;
+}
+
+/**
+ * Implements hook_field_is_empty().
+ */
+function contact_field_field_is_empty($item, $field) {
+  return empty($item);
+}
+
+function contact_field_field_schema($field) {
+  if ($field['type'] != 'user_contact') {
+    return;
+  }
+
+  $ret = array(
+    'columns' => array(),
+    'indexes' => array(),
+
+  );
+
+  // dsm(get_defined_vars(), __FUNCTION__);
+  return $ret;
+}
+
+function contact_field_field_widget_info() {
+  $ret = array(
+    'contact_field_simple' => array(
+      'label' => t('No input necessary'),
+      'description' => t('Take the information from the underlying account'),
+      'field types' => array('user_contact'),
+      'behaviors' => array(
+        'multiple values' => FIELD_BEHAVIOR_CUSTOM, // No values input anyway
+        'default value' => FIELD_BEHAVIOR_DEFAULT,
+      ),
+    ),
+  );
+
+  // dsm(get_defined_vars(), __FUNCTION__);
+  return $ret;
+}
+
+function contact_field_field_widget_form(&$form, &$form_state, $field,
+  $instance, $langcode, $items, $delta, $element) {
+  // dsm(get_defined_vars(), __FUNCTION__);
+
+  $ret = array();
+  if (isset($form['uid'])) {
+    // This is not the widget settings form
+    $uid = $form['uid']['#value'];
+    $account = user_load($uid);
+    $ret['mail'] = array(
+      '#type' => 'value',
+      '#title' => $instance['label'],
+      '#disabled' => TRUE,
+      '#value' => $account->mail,
+    );
+  }
+  return $ret;
+
+}
+
+/**
+ * Implements hook_field_formatter_view().
+ */
+function contact_field_field_formatter_view($obj_type, $object, $field,
+  $instance, $langcode, $items, $display) {
+
+  $uid = $object->uid;
+  $name = $object->name;
+
+  $ret = array();
+  switch ($display['type']) {
+    case 'user_contact_form_link':
+      foreach (array_keys($items) as $delta) {
+        // Cannot use theme_username() in D7.7. See http://drupal.org/node/1238094
+        $ret[$delta] = array('#markup' => l($name, "user/$uid/contact"));
+      }
+      break;
+
+    case 'user_contact_mailto_link':
+      $account = user_load($uid);
+      $mail = $account->mail;
+      foreach (array_keys($items) as $delta) {
+        // Cannot use theme_username() in D7.7. See http://drupal.org/node/1238094
+        $ret[$delta] = array('#markup' => l($name, "mailto:$mail"));
+      }
+      break;
+
+    default:
+      return;
+  }
+
+  $ret = array($instance['id'] => $ret);
+  // dsm(get_defined_vars(), __FUNCTION__);
+  return $ret;
+}
+
+/**
+ * Force cardinality: there is no point in having multiple instances of this
+ * field since it is single-valued per entity instance, instead of having
+ * standalone storage.
+ */
+function contact_field_form_field_ui_field_edit_form_alter(&$form, $form_state, $form_id) {
+  dsm($form);
+  if ($form['#field']['type'] == 'user_contact') {
+    $override = array(
+      '#access' => FALSE,
+      '#value' => 1,
+    );
+    $form['field']['cardinality'] = $override + $form['field']['cardinality'];
+  }
+}