Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vishalbandre/d9b4d6aa3c165ae90cb2de486297bbe4 to your computer and use it in GitHub Desktop.
Save vishalbandre/d9b4d6aa3c165ae90cb2de486297bbe4 to your computer and use it in GitHub Desktop.
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
Inside your class where you want to inject the Drupal 9 services:
/**
* Get access to entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Get access to Bundle Info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Class constructor.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager,
ConfigFactoryInterface $config_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static(
// Load the service required to construct this class.
$container->get('entity_type.manager'),
$container->get('config.factory'),
$container->get('entity_type.bundle.info')
);
}
<hr />
Brain storming:
// dump($this->entityTypeManager->getStorage($entity_type)->loadMultiple());
// return bundles from entityTypeManager
// $bundle_info = $this->entityTypeManager->getStorage('field_config')->load('node.' . $entity_type . '.body')->getBundleInfo();
// $bundles = $this->entityTypeManager->getStorage('entity_type')->load($entity_type)->getBundleEntityType();
$bundles_data = $this->entityTypeManager->getStorage($entity_type)->loadMultiple();
$bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type);
// kint($bnd);
// kint($this->entityTypeManager->getDefinition($entity_type)->getBundleEntityType());
// kint($this->entityTypeManager->getStorage('entity_type'));
// $this->entityTypeManager
// $bnd = $this->entityTypeManager->get('entity_type.bundle.info')->getBundleInfo($entity_type);
// $bnd = array_keys($bnd);
// foreach($bundles_data as $b => $bf) {
// $bundles[] = $bf->bundle();
// }
// echo '<hr><hr>';
// // kint($bundles);
// foreach ($bundles as $bundle) {
// // kint($bundle);
// echo '..................';
// }
// echo '<hr><hr>';
// echo '<hr />';
// $t = $this->entityTypeManager->getStorage('taxonomy_term')->load(2);
// echo $t->bundle().'<br /><br /><br /><br /><br /><br />';
// $bundles = [];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment