function Role::preSave

Same name in other branches
  1. 9 core/modules/user/src/Entity/Role.php \Drupal\user\Entity\Role::preSave()
  2. 8.9.x 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 181

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() && $this->hasTrustedData()) {
        // Permissions are always ordered alphabetically to avoid conflicts in the
        // exported configuration. If the save is not trusted then the
        // configuration will be sorted by StorableConfigBase.
        sort($this->permissions);
    }
}

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