我定义了两个 profile2 配置文件 - main 和 customer_profile。另外,我有一个名为 Customer 的节点类型。
创建新的客户节点时,我想加载 custom_profile 表单。这个想法是同时创建一个节点和一个配置文件。
我知道这绝对是一个 hook_form_alter 解决方案,但有人可以告诉我如何在创建或编辑客户节点时以编程方式加载配置文件。
我定义了两个 profile2 配置文件 - main 和 customer_profile。另外,我有一个名为 Customer 的节点类型。
创建新的客户节点时,我想加载 custom_profile 表单。这个想法是同时创建一个节点和一个配置文件。
我知道这绝对是一个 hook_form_alter 解决方案,但有人可以告诉我如何在创建或编辑客户节点时以编程方式加载配置文件。
您可以使用这些功能加载配置文件类型和数据
$types = profile2_get_types();
profile2_load_by_user($account, $type_name = NULL)
例如 :
$types = profile2_get_types();
if (!empty($types)) {
foreach ($types as $type) {
$profile = profile2_load_by_user($uid, $type->type);
}
}
即使您能够加载 customer_profile 表单,您也需要单独处理这些值,因为它们是两个不同的节点。
我建议在客户节点表单中捕获这些字段,然后以编程方式从这些值创建一个 customer_profile。
如果您想获取 profile2 表单本身,则可以使用类似
module_load_include('inc', 'profile2_page', 'profile2_page');
$profile2 = profile2_by_uid_load($uid, 'seeker_profile');
$entity_form = entity_ui_get_form('profile2', $profile2, 'edit');
然后将其添加到您要放置的表单中。
您可以使用 profile2_load_by_user() 加载完整的配置文件数据;参数如: -
profile2_load_by_user($account,$type_name)
$account: The user account to load profiles for, or its uid.
$type_name: To load a single profile, pass the type name of the profile to load
所以代码如下
$account->uid = $existingUser->uid;
$type_name = 'user_about';
$profile = profile2_load_by_user($account, $type_name);
//$profile variable have full data of profile fields
//changing data profile2 fields
if(isset($_POST['field_user_first_name'])&& !empty($_POST['field_user_first_name'])){
$profile->field_user_first_name['und'][0]['value'] = $_POST['field_user_first_name'];
}
profile2_save($profile);
那么当创建一个新的 Profile 时, Profile2 字段在手动保存完成之前是不可见的。
要自动创建 profile2 对象,我们使用规则模块
步骤
1)转到 Drupal admin/config/workflow/rules
2)创建新规则
3)给一个名称并在 react/event 中选择“保存新用户帐户后”
4)操作,>> 添加操作 >> 执行自定义 PHP 代码
5)插入 php 代码
$profile = profile_create(array('type' => 'profile2 type machine name', 'uid' => $account->uid));
profile2_save($profile);
6)保存 >> 保存更改。
这将在创建新用户时创建 profile2 字段。
我也需要在用户页面上创建一个自定义选项卡并在其中加载用户 profile2 表单。
这是我如何设法做到这一点的快照代码:
MYMODULE.module https://gist.github.com/4223234
MYMODULE_profile2_MYPROFILE2TYPE.inc https://gist.github.com/4223201
希望能帮助到你。