function user_user_role_insert

Same name in other branches
  1. 9 core/modules/user/user.module \user_user_role_insert()
  2. 8.9.x core/modules/user/user.module \user_user_role_insert()
  3. 11.x core/modules/user/user.module \user_user_role_insert()

Implements hook_ENTITY_TYPE_insert() for user_role entities.

File

core/modules/user/user.module, line 855

Code

function user_user_role_insert(RoleInterface $role) {
    // Ignore the authenticated and anonymous roles or the role is being synced.
    if (in_array($role->id(), [
        RoleInterface::AUTHENTICATED_ID,
        RoleInterface::ANONYMOUS_ID,
    ]) || $role->isSyncing()) {
        return;
    }
    assert(Inspector::assertStringable($role->label()), 'Role label is expected to be a string.');
    $add_id = 'user_add_role_action.' . $role->id();
    if (!Action::load($add_id)) {
        $action = Action::create([
            'id' => $add_id,
            'type' => 'user',
            'label' => t('Add the @label role to the selected user(s)', [
                '@label' => $role->label(),
            ]),
            'configuration' => [
                'rid' => $role->id(),
            ],
            'plugin' => 'user_add_role_action',
        ]);
        $action->trustData()
            ->save();
    }
    $remove_id = 'user_remove_role_action.' . $role->id();
    if (!Action::load($remove_id)) {
        $action = Action::create([
            'id' => $remove_id,
            'type' => 'user',
            'label' => t('Remove the @label role from the selected user(s)', [
                '@label' => $role->label(),
            ]),
            'configuration' => [
                'rid' => $role->id(),
            ],
            'plugin' => 'user_remove_role_action',
        ]);
        $action->trustData()
            ->save();
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.