profile2.inc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for profile2 from user.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t('Profile2 from user'),
  12. 'keyword' => 'profile2',
  13. 'description' => t('Adds a profile2 type from a user context.'),
  14. 'required context' => new ctools_context_required(t('User'), 'entity:user'),
  15. 'context' => 'profile2_from_user_context',
  16. 'edit form' => 'profile2_from_user_settings_form',
  17. 'defaults' => array('type' => 'main'),
  18. );
  19. /**
  20. * Return a new context based on an existing context.
  21. */
  22. function profile2_from_user_context($context, $conf) {
  23. // If unset it wants a generic, unfilled context, which is just NULL.
  24. if (empty($context->data) || !isset($context->data->uid)) {
  25. return ctools_context_create_empty('entity:profile2', NULL);
  26. }
  27. // Load user's profile and return a ctools context from it.
  28. if ($profile = profile2_load_by_user($context->data, $conf['type'])) {
  29. return ctools_context_create('entity:profile2', $profile);
  30. }
  31. // In case when we can't load a profile, just return an empty context.
  32. return ctools_context_create_empty('entity:profile2', NULL);
  33. }
  34. /**
  35. * Settings form for the relationship.
  36. */
  37. function profile2_from_user_settings_form($form, &$form_state) {
  38. $conf = $form_state['conf'];
  39. $options = array();
  40. foreach (profile2_get_types() as $type => $info) {
  41. $options[$type] = $info->label;
  42. }
  43. $form['type'] = array(
  44. '#type' => 'select',
  45. '#title' => t('Select Profile2 Type'),
  46. '#options' => $options,
  47. '#default_value' => $conf['type'],
  48. '#prefix' => '<div class="clearfix">',
  49. '#suffix' => '</div>',
  50. );
  51. return $form;
  52. }