【问题标题】:How to create a role programmatically in Drupal 8?如何在 Drupal 8 中以编程方式创建角色?
【发布时间】:2016-03-19 00:02:48
【问题描述】:
如何在 Drupal 8 中以编程方式创建角色?
我在这里做错了什么?
$role = \Drupal\user\Entity\Role::create(['id' => 'client', 'name' => 'Client']);
$role->save();
【问题讨论】:
标签:
php
drupal
roles
drupal-8
security-roles
【解决方案1】:
问题在于数据数组中的name被label改变了:
$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();