3 namespace Drupal\entity\BundlePlugin;
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
9 class BundlePluginHandler implements BundlePluginHandlerInterface {
14 * @var \Drupal\Core\Entity\EntityTypeInterface
16 protected $entityType;
19 * The bundle plugin manager.
21 * @var \Drupal\Component\Plugin\PluginManagerInterface
23 protected $pluginManager;
26 * Constructs a new BundlePluginHandler object.
28 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
30 * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
31 * The bundle plugin manager.
33 public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $plugin_manager) {
34 $this->entityType = $entity_type;
35 $this->pluginManager = $plugin_manager;
41 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
44 $container->get('plugin.manager.' . $entity_type->get('bundle_plugin_type'))
51 public function getBundleInfo() {
53 foreach ($this->pluginManager->getDefinitions() as $plugin_id => $definition) {
54 $bundles[$plugin_id] = [
55 'label' => $definition['label'],
56 'description' => isset($definition['description']) ? $definition['description'] : '',
57 'translatable' => $this->entityType->isTranslatable(),
58 'provider' => $definition['provider'],
67 public function getFieldStorageDefinitions() {
69 foreach (array_keys($this->pluginManager->getDefinitions()) as $plugin_id) {
70 /** @var \Drupal\entity\BundlePlugin\BundlePluginInterface $plugin */
71 $plugin = $this->pluginManager->createInstance($plugin_id);
72 $definitions += $plugin->buildFieldDefinitions();
74 // Ensure the presence of required keys which aren't set by the plugin.
75 foreach ($definitions as $field_name => $definition) {
76 $definition->setName($field_name);
77 $definition->setTargetEntityTypeId($this->entityType->id());
78 $definitions[$field_name] = $definition;
87 public function getFieldDefinitions($bundle) {
88 /** @var \Drupal\entity\BundlePlugin\BundlePluginInterface $plugin */
89 $plugin = $this->pluginManager->createInstance($bundle);
90 $definitions = $plugin->buildFieldDefinitions();
91 // Ensure the presence of required keys which aren't set by the plugin.
92 foreach ($definitions as $field_name => $definition) {
93 $definition->setName($field_name);
94 $definition->setTargetEntityTypeId($this->entityType->id());
95 $definition->setTargetBundle($bundle);
96 $definitions[$field_name] = $definition;