function Role::preSave

Same name and namespace in other branches
  1. 9 core/modules/user/src/Entity/Role.php \Drupal\user\Entity\Role::preSave()
  2. 10 core/modules/user/src/Entity/Role.php \Drupal\user\Entity\Role::preSave()
  3. 11.x core/modules/user/src/Entity/Role.php \Drupal\user\Entity\Role::preSave()

Overrides ConfigEntityBase::preSave

File

core/modules/user/src/Entity/Role.php, line 178

Class

Role
Defines the user role entity class.

Namespace

Drupal\user\Entity

Code

public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    if (!isset($this->weight) && ($roles = $storage->loadMultiple())) {
        // Set a role weight to make this new role last.
        $max = array_reduce($roles, function ($max, $role) {
            return $max > $role->weight ? $max : $role->weight;
        });
        $this->weight = $max + 1;
    }
    if (!$this->isSyncing()) {
        // Permissions are always ordered alphabetically to avoid conflicts in the
        // exported configuration.
        sort($this->permissions);
    }
}

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