You are here

public function OgRole::save in Organic groups 8

Saves an entity permanently.

When saving existing entities, the entity is assumed to be complete, partial updates of entities are not supported.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Throws

\Drupal\Core\Entity\EntityStorageException In case of failures an exception is thrown.

Overrides ConfigEntityBase::save

File

src/Entity/OgRole.php, line 186

Class

OgRole
Defines the OG user role entity class.

Namespace

Drupal\og\Entity

Code

public function save() {

  // The ID of a new OgRole has to consist of the entity type ID, bundle ID
  // and role name, separated by dashes.
  if ($this
    ->isNew() && $this
    ->id()) {
    $pattern = preg_quote("{$this->getGroupType()}-{$this->getGroupBundle()}-{$this->getName()}");
    if (!preg_match("/{$pattern}/", $this
      ->id())) {
      throw new ConfigValueException('The ID should consist of the group entity type ID, group bundle ID and role name, separated by dashes.');
    }
  }

  // If a new OgRole is saved and the ID is not set, construct the ID from
  // the entity type ID, bundle ID and role name.
  if ($this
    ->isNew() && !$this
    ->id()) {
    if (!$this
      ->getGroupType()) {
      throw new ConfigValueException('The group type can not be empty.');
    }
    if (!$this
      ->getGroupBundle()) {
      throw new ConfigValueException('The group bundle can not be empty.');
    }
    if (!$this
      ->getName()) {
      throw new ConfigValueException('The role name can not be empty.');
    }

    // When assigning a role to group we need to add a prefix to the ID in
    // order to prevent duplicate IDs.
    $prefix = $this
      ->getGroupType() . '-' . $this
      ->getGroupBundle() . '-';
    $this
      ->setId($prefix . $this
      ->getName());
  }
  parent::save();
}