You are here

class BundlePluginHandler in Entity API 8

Hierarchy

Expanded class hierarchy of BundlePluginHandler

1 file declares its use of BundlePluginHandler
entity.module in ./entity.module
Provides expanded entity APIs.

File

src/BundlePlugin/BundlePluginHandler.php, line 9

Namespace

Drupal\entity\BundlePlugin
View source
class BundlePluginHandler implements BundlePluginHandlerInterface {

  /**
   * The entity type.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface
   */
  protected $entityType;

  /**
   * The bundle plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $pluginManager;

  /**
   * Constructs a new BundlePluginHandler object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
   *   The bundle plugin manager.
   */
  public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $plugin_manager) {
    $this->entityType = $entity_type;
    $this->pluginManager = $plugin_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('plugin.manager.' . $entity_type
      ->get('bundle_plugin_type')));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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();
    }

    // Ensure the presence of required keys which aren't set by the plugin.
    foreach ($definitions as $field_name => $definition) {
      $definition
        ->setName($field_name);
      $definition
        ->setTargetEntityTypeId($this->entityType
        ->id());
      $definitions[$field_name] = $definition;
    }
    return $definitions;
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldDefinitions($bundle) {
    $plugin = $this->pluginManager
      ->createInstance($bundle);
    assert($plugin instanceof BundlePluginInterface);
    $definitions = $plugin
      ->buildFieldDefinitions();

    // Ensure the presence of required keys which aren't set by the plugin.
    foreach ($definitions as $field_name => $definition) {
      $definition
        ->setName($field_name);
      $definition
        ->setTargetEntityTypeId($this->entityType
        ->id());
      $definition
        ->setTargetBundle($bundle);
      $definitions[$field_name] = $definition;
    }
    return $definitions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BundlePluginHandler::$entityType protected property The entity type.
BundlePluginHandler::$pluginManager protected property The bundle plugin manager.
BundlePluginHandler::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
BundlePluginHandler::getBundleInfo public function Gets the bundle info. Overrides BundlePluginHandlerInterface::getBundleInfo
BundlePluginHandler::getFieldDefinitions public function Gets the field definitions for a specific bundle. Overrides BundlePluginHandlerInterface::getFieldDefinitions
BundlePluginHandler::getFieldStorageDefinitions public function Gets the field storage definitions. Overrides BundlePluginHandlerInterface::getFieldStorageDefinitions
BundlePluginHandler::__construct public function Constructs a new BundlePluginHandler object.