Last active
May 25, 2026 01:03
-
-
Save aklump/9a790adc80d06975a85e0dacca4b6165 to your computer and use it in GitHub Desktop.
Provides access to the typed entity repository manager in Drupal custom classes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Drupal\tsb\Traits; | |
| use Drupal\typed_entity\RepositoryManager; | |
| /** | |
| * Provides access to the typed entity repository manager. | |
| * | |
| * Consuming classes may inject the manager by defining a | |
| * `$typedEntityRepositoryManager` property, typically through constructor | |
| * property promotion: | |
| * | |
| * @code | |
| * public function __construct( | |
| * protected RepositoryManager $typedEntityRepositoryManager, | |
| * ) {} | |
| * @endcode | |
| * | |
| * When the property is not set, the manager is retrieved from Drupal's service | |
| * container. This keeps the trait usable in classes where constructor injection | |
| * is awkward or unavailable. | |
| */ | |
| trait HasTypedEntitiesTrait { | |
| public function typedEntityRepositoryManager(): RepositoryManager { | |
| if (isset($this->typedEntityRepositoryManager)) { | |
| return $this->typedEntityRepositoryManager; | |
| } | |
| return \Drupal::service(RepositoryManager::class); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment