devel_wizard-2.x-dev/templates/derivative/links-action/devel_wizard.entity-add.class.php.twig
templates/derivative/links-action/devel_wizard.entity-add.class.php.twig
{%
include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
with {
'namespace': linksActionBase.classNamespace,
}
%}
{%-
include '@devel_wizard/php/devel_wizard.php.file.use_statements.php.twig'
with {
useStatements: {
'Drupal\\Component\\Plugin\\Derivative\\DeriverBase': '',
'Drupal\\Core\\Entity\\EntityTypeManagerInterface': '',
'Drupal\\Core\\Plugin\\Discovery\\ContainerDeriverInterface': '',
'Drupal\\Core\\Routing\\RouteProviderInterface': '',
'Drupal\\Core\\StringTranslation\\StringTranslationTrait': '',
'Symfony\\Component\\DependencyInjection\\ContainerInterface': '',
}
}
%}
abstract class {{ linksActionBase.class }} extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected RouteProviderInterface $routeProvider;
protected EntityTypeManagerInterface $entityTypeManager;
/**
* @abstract
*/
protected string $configEntityTypeId = '';
/**
* @abstract
*/
protected string $contentEntityTypeId = '';
/**
* @abstract
*/
protected string $actionRouteName = '';
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
$base_plugin_id,
) {
return new static(
$container->get('router.route_provider'),
$container->get('entity_type.manager'),
);
}
public function __construct(
RouteProviderInterface $routeProvider,
EntityTypeManagerInterface $entityTypeManager,
) {
$this->routeProvider = $routeProvider;
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
if (!$this->entityTypeManager->hasDefinition($this->contentEntityTypeId)
|| !$this->entityTypeManager->hasDefinition('view')
) {
return $this->derivatives;
}
/** @noinspection PhpUnhandledExceptionInspection */
$contentEntityType = $this
->entityTypeManager
->getDefinition($this->contentEntityTypeId);
/** @noinspection PhpUnhandledExceptionInspection */
$bundles = $this
->entityTypeManager
->getStorage($this->configEntityTypeId)
->loadMultiple();
/** @noinspection PhpUnhandledExceptionInspection */
$viewStorage = $this->entityTypeManager->getStorage('view');
foreach ($bundles as $bundle) {
// @todo Configurable viewId pattern and displayId.
$viewId = "{{ idPrefix }}_{$this->contentEntityTypeId}_{$bundle->id()}_admin";
$view = $viewStorage->load($viewId);
if (!$view) {
continue;
}
$derivativeId = "bundle_{$bundle->id()}";
$this->derivatives[$derivativeId] = [
'route_name' => $this->actionRouteName,
'title' => $this->t(
'Add @entity_type.label',
[
'@entity_type.label' => $contentEntityType->getLabel(),
]
),
'appears_on' => [
"view.$viewId.overview",
],
];
$this->derivatives[$derivativeId] += $base_plugin_definition;
}
return $this->derivatives;
}
}
