Skip to content

Instantly share code, notes, and snippets.

@aklump
Last active May 25, 2026 01:03
Show Gist options
  • Select an option

  • Save aklump/9a790adc80d06975a85e0dacca4b6165 to your computer and use it in GitHub Desktop.

Select an option

Save aklump/9a790adc80d06975a85e0dacca4b6165 to your computer and use it in GitHub Desktop.
Provides access to the typed entity repository manager in Drupal custom classes.
<?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