From a4b06e112610960a8eb0f64e86577ef330a07bc6 Mon Sep 17 00:00:00 2001 From: Matt Korostoff Date: Sat, 30 Aug 2014 15:27:04 -0400 Subject: [PATCH] Issue #1011370 by leonsio: ctools relationship from user --- plugins/relationships/profile2.inc | 59 ++++++++++++++++++++++++++++++++++++ profile2.module | 42 +++++++++++++++++++++++++ 2 files changed, 101 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..467333d 100644 --- a/profile2.module +++ b/profile2.module @@ -1049,3 +1049,45 @@ 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 our own relationship. + * + * @see + * plugins/relationships/profile2.inc + */ +function profile2_form_ctools_context_handler_edit_context_alter(&$form, $form_state) { + unset($form['right']['relationships_table']['buttons']['relationship']['item']['#options']['entity_from_schema:uid-user-profile2']); +} + +/** + * Implements hook_preprocess_ctools_context_item_form + * + * This has the same functional goal as profile2_form_ctools_context_handler_edit_context_alter. + * When the User context is added for the first time, Ctools will update the + * relationship dropdown with ajax. hook_form_alter is not called in this case, + * because ctools calls form_builder() directly. The new dropdown is passed + * through theme_ctools_context_item_form before being passed to ajax_render, so + * that is our best opportunity to alter it. + * + * @see + * ctools_context_ajax_item_add + */ +function profile2_preprocess_ctools_context_item_form(&$vars) { + unset($vars['form']['buttons']['relationship']['item']['#options']['entity_from_schema:uid-user-profile2']); +} -- 1.7.10.2 (Apple Git-33)