如何在Drupal 8中以编程方式创建角色?

时间:2015-12-13 12:49:27

标签: php drupal roles drupal-8 security-roles

如何在Drupal 8中以编程方式创建角色?

我在这里做错了什么?

$role = \Drupal\user\Entity\Role::create(['id' => 'client', 'name' => 'Client']);
$role->save(); 

1 个答案:

答案 0 :(得分:3)

问题在于标签的数据阵列更改名称

$role = \Drupal\user\Entity\Role::create(array('id' => 'client', 'label' => 'Client'));
$role->save(); 

或者您可以使用:

//your data array
$data = array('id' => 'client', 'label' => 'Client');
//creating your role
$role = \Drupal\user\Entity\Role::create($data);
//saving your role
$role->save();