From 997f6b349ee364fd27def6a0906d5de342cd6ae7 Mon Sep 17 00:00:00 2001 From: Matt Korostoff Date: Thu, 28 Aug 2014 22:52:43 -0400 Subject: [PATCH] Issue #1011370 by leonsio: Ctools relationship from user --- plugins/relationships/profile2.inc | 59 ++++++++++++++++++++++++++++++++++++ profile2.module | 27 +++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 plugins/relationships/profile2.inc diff --git a/plugins/relationships/profile2.inc b/plugins/relationships/profile2.inc new file mode 100644 index 0000000..e95d85e --- /dev/null +++ b/plugins/relationships/profile2.inc @@ -0,0 +1,59 @@ + t('Profile2 from user'), + 'keyword' => 'profile2', + 'description' => t('Adds a profile2 type from a user context.'), + 'required context' => new ctools_context_required(t('User'), 'entity:user'), + 'context' => 'profile2_from_user_context', + 'edit form' => 'profile2_from_user_settings_form', + 'defaults' => array('type' => 'main'), +); + +/** + * Return a new context based on an existing context. + */ +function profile2_from_user_context($context, $conf) { + // If unset it wants a generic, unfilled context, which is just NULL. + if (empty($context->data) || !isset($context->data->uid)) { + return ctools_context_create_empty('entity:profile2', NULL); + } + + $account = clone $context->data; + + if ($profile = profile2_load_by_user($account, $conf['type'])) { + return ctools_context_create('entity:profile2', $profile); + } +} + +/** + * Settings form for the relationship. + */ +function profile2_from_user_settings_form($form, &$form_state) { + $conf = $form_state['conf']; + + $options = array(); + foreach (profile2_get_types() as $type => $info) { + $options[$type] = $info->label; + } + + $form['type'] = array( + '#type' => 'select', + '#title' => t('Select Profile2 Type'), + '#options' => $options, + '#default_value' => $conf['type'], + '#prefix' => '
', + '#suffix' => '
', + ); + + return $form; +} diff --git a/profile2.module b/profile2.module index 032f043..496b933 100644 --- a/profile2.module +++ b/profile2.module @@ -1049,3 +1049,30 @@ function profile2_user_get_properties($account, array $options, $name) { $profile = profile2_load_by_user($account, substr($name, 8)); return $profile ? $profile : NULL; } + +/** + * Implements hook_ctools_plugin_directory(). + */ +function profile2_ctools_plugin_directory($owner, $plugin_type) { + if ($owner == 'ctools' && !empty($plugin_type)) { + return 'plugins/' . $plugin_type; + } +} + +/** + * Implements hook_FORM_ID_alter + * + * On the page manager edit form, if you add the user context, ctools automatically + * provides the option to create a relationship to the profile table. This + * relationship is nearly useless, as it can only be used to include data from + * the main profile type (i.e. the bundle is hardcoded). We will remove this + * option, and provide out own relationship. + * + * @see + * plugins/relationships/profile2.inc + */ +function profile2_form_ctools_context_handler_edit_context_alter(&$form) { + if (isset($form['right']['relationships_table']['buttons']['relationship']['item']['#options']['entity_from_schema:uid-user-profile2'])) { + unset($form['right']['relationships_table']['buttons']['relationship']['item']['#options']['entity_from_schema:uid-user-profile2']); + } +} -- 1.7.10.2 (Apple Git-33)