2

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

我在这里做错了什么?

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

1 回答 1

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();
于 2015-12-15T00:30:45.143 回答