2015-12-13 54 views
2

如何在Drupal 8中以編程方式創建角色?如何在Drupal 8中以編程方式創建角色?

我在這裏做錯了什麼?

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

更新我的答案;-) –

+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(); 
相關問題