BundlePluginHandler.php in Entity API 8
File
src/BundlePlugin/BundlePluginHandler.php
View source
<?php
namespace Drupal\entity\BundlePlugin;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BundlePluginHandler implements BundlePluginHandlerInterface {
protected $entityType;
protected $pluginManager;
public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $plugin_manager) {
$this->entityType = $entity_type;
$this->pluginManager = $plugin_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('plugin.manager.' . $entity_type
->get('bundle_plugin_type')));
}
public function getBundleInfo() {
$bundles = [];
foreach ($this->pluginManager
->getDefinitions() as $plugin_id => $definition) {
$bundles[$plugin_id] = [
'label' => $definition['label'],
'description' => isset($definition['description']) ? $definition['description'] : '',
'translatable' => $this->entityType
->isTranslatable(),
'provider' => $definition['provider'],
];
}
return $bundles;
}
public function getFieldStorageDefinitions() {
$definitions = [];
foreach (array_keys($this->pluginManager
->getDefinitions()) as $plugin_id) {
$plugin = $this->pluginManager
->createInstance($plugin_id);
assert($plugin instanceof BundlePluginInterface);
$definitions += $plugin
->buildFieldDefinitions();
}
foreach ($definitions as $field_name => $definition) {
$definition
->setName($field_name);
$definition
->setTargetEntityTypeId($this->entityType
->id());
$definitions[$field_name] = $definition;
}
return $definitions;
}
public function getFieldDefinitions($bundle) {
$plugin = $this->pluginManager
->createInstance($bundle);
assert($plugin instanceof BundlePluginInterface);
$definitions = $plugin
->buildFieldDefinitions();
foreach ($definitions as $field_name => $definition) {
$definition
->setName($field_name);
$definition
->setTargetEntityTypeId($this->entityType
->id());
$definition
->setTargetBundle($bundle);
$definitions[$field_name] = $definition;
}
return $definitions;
}
}