【问题标题】:Edit a specific User attribute in a page in Concrete5在 Concrete5 的页面中编辑特定的用户属性
【发布时间】:2015-04-28 11:21:26
【问题描述】:
我的具体 5.6.3.1 网站已公开注册。我有一个名为“我是大使”的属性,这是一个显示在注册和您的个人资料中的复选框。
我的问题是我需要显示这个特定属性,并且可以在特定页面内进行编辑。
例如:我有一个名为 Sing up as Ambassador 的页面,当转到此页面时,我需要显示这个特定的复选框,用户可以启用/禁用它并保存它。(与配置文件编辑相同)。有人帮我吗?
【问题讨论】:
标签:
attributes
custom-attributes
concrete5
【解决方案1】:
注意:此答案适用于旧的具体 5 版本 6,只有部分适用于版本 7
所以对于这个问题,有几个部分我们需要复习。
-
如何判断当前登录用户?
$user = new User();
$user_info = UserInfo::getByID($user->getUserID());
-
如何获取属性值?
$user_info->getAttribute('my_attribute_handle');
-
如何设置属性值?
$user_info->setAttribute('my_attribute_handle', $value);
那么在我的单页视图方法中,我会有类似的东西:
public function view()
{
$user = new User();
$some_attribute = false;
if ($user->isLoggedIn()) {
$user_info = UserInfo::getByID($user->getUserID());
$some_attribute = $user_info->getAttribute('some_attribute');
}
$this->set('some_attribute', $some_attribute);
}
然后在我的单页查看文件中,我有:
<input type='checkbox' value='1' name='some_attribute' <?= $some_attribute ? 'checked' : '' ?> />
无论我的表单最终提交到哪里,我都会这样做:
$user = new User();
if ($user->isLoggedIn()) {
$user_info = UserInfo::getByID($user->getUserID());
$user_info->setAttribute('some_attribute', $_REQUEST['some_attribute'] == 1);
}