ds-8.x-3.9/src/Plugin/Derivative/BundleField.php
src/Plugin/Derivative/BundleField.php
<?php
namespace Drupal\ds\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a derivative for bundle fields.
*/
class BundleField extends DeriverBase implements ContainerDeriverInterface {
/**
* Stores all entity row plugin information.
*
* @var array
*/
protected $derivatives = [];
/**
* The base plugin ID that the derivative is for.
*
* @var string
*/
protected $basePluginId;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a Bundlefield object.
*
* @param string $base_plugin_id
* The base plugin ID.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager.
*/
public function __construct($base_plugin_id, EntityTypeManagerInterface $entity_type_manager) {
$this->basePluginId = $base_plugin_id;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$base_plugin_id,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
$base_table = $entity_type->getBaseTable();
if ($entity_type->get('field_ui_base_route') && !empty($base_table)) {
$this->derivatives[$entity_type_id] = $base_plugin_definition;
$this->derivatives[$entity_type_id]['entity_type'] = $entity_type_id;
$this->derivatives[$entity_type_id]['title'] = 'Bundle name';
}
}
return $this->derivatives;
}
}
