devel_wizard-2.x-dev/src/Spell/SpellTraitUserRoleManager.php
src/Spell/SpellTraitUserRoleManager.php
<?php
declare(strict_types=1);
namespace Drupal\devel_wizard\Spell;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\user\RoleInterface;
trait SpellTraitUserRoleManager {
protected EntityTypeManagerInterface $entityTypeManager;
abstract protected function messageConfigEntityExists(ConfigEntityInterface $entity): static;
abstract protected function messageConfigEntityCreate(ConfigEntityInterface $entity): static;
/**
* @throws \Drupal\Core\Entity\EntityMalformedException
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function ensureUserRole(array $values): RoleInterface {
/* @noinspection PhpUnhandledExceptionInspection */
$storage = $this->entityTypeManager->getStorage('user_role');
$userRole = $storage->load($values['id']);
if ($userRole) {
/* @noinspection PhpUnhandledExceptionInspection */
$this->messageConfigEntityExists($userRole);
return $userRole;
}
$userRole = $storage->create($values);
$userRole->save();
/* @noinspection PhpUnhandledExceptionInspection */
$this->messageConfigEntityCreate($userRole);
return $userRole;
}
}
