devel_wizard-2.x-dev/templates/derivative/links-task/devel_wizard.entity-overview.class.php.twig
templates/derivative/links-task/devel_wizard.entity-overview.class.php.twig
{%
include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
with {
'namespace': linksTaskBase.classNamespace,
}
%}
{%-
include '@devel_wizard/php/devel_wizard.php.file.use_statements.php.twig'
with {
useStatements: {
'Drupal\\Core\\Entity\\EntityStorageInterface': '',
'Drupal\\Core\\Entity\\EntityTypeManagerInterface': '',
'Drupal\\Core\\Extension\\ModuleHandlerInterface': '',
'Drupal\\Core\\Routing\\RouteProviderInterface': '',
'Drupal\\Core\\State\\StateInterface': '',
'Drupal\\Core\\StringTranslation\\StringTranslationTrait': '',
'Drupal\\views\\Plugin\\Derivative\\ViewsLocalTask': '',
'Symfony\\Component\\DependencyInjection\\ContainerInterface': '',
}
}
%}
abstract class {{ linksTaskBase.class }} extends ViewsLocalTask {
use StringTranslationTrait;
protected EntityTypeManagerInterface $entityTypeManager;
protected ModuleHandlerInterface $moduleHandler;
/**
* @abstract
*/
protected string $configEntityTypeId = '';
/**
* @abstract
*/
protected string $contentEntityTypeId = '';
/**
* @abstract
*/
protected string $primaryTaskId = '';
/**
* @abstract
*/
protected string $primaryTaskRouteName = '';
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
$base_plugin_id,
) {
return new static(
$container->get('router.route_provider'),
$container->get('state'),
$container->get('entity_type.manager')->getStorage('view'),
$container->get('entity_type.manager'),
$container->get('module_handler'),
);
}
public function __construct(
RouteProviderInterface $routeProvider,
StateInterface $state,
EntityStorageInterface $viewStorage,
EntityTypeManagerInterface $entityTypeManager,
ModuleHandlerInterface $moduleHandler,
) {
$this->entityTypeManager = $entityTypeManager;
$this->moduleHandler = $moduleHandler;
parent::__construct(
$routeProvider,
$state,
$viewStorage,
);
}
/**
* {@inheritdoc}
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
$this->derivatives += $this->getBundleAdminViewDerivatives($base_plugin_definition);
return $this->derivatives;
}
/**
* Collect admin views for node types generated by Devel Wizard.
*
* @param array $basePluginDefinition
* The definition array of the base plugin.
*
* @return array
* Returns the devel wizard block content type admin views list.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getBundleAdminViewDerivatives(array $basePluginDefinition): array {
$derivatives = [];
if (!$this->entityTypeManager->hasDefinition($this->contentEntityTypeId)) {
return $derivatives;
}
$existingRoutes = $this->state->get('views.view_route_names');
if (!$existingRoutes) {
return $derivatives;
}
// Add a secondary 'All' local task under the primary tab tab.
// @todo Check that if the view.content.page_1 display is exists.
$derivatives += [
'bundle' => [
'route_name' => $this->primaryTaskRouteName,
'parent_id' => $this->primaryTaskId,
'weight' => -50,
'title' => $this->t('All'),
],
];
$derivatives += $this->getConfigEntityAdminViewDerivatives(
$existingRoutes,
$basePluginDefinition,
);
return $derivatives;
}
/**
* Collects admin view derivatives.
*
* Collects admin view derivatives for a specified config entity created
* by DevelWizard module.
*
* @param array $existingRoutes
* The stored value for view route names.
* @param array $basePluginDefinition
* The definition array of the base plugin.
*
* @return array
* Returns a list of local tasks.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getConfigEntityAdminViewDerivatives(
array $existingRoutes,
array $basePluginDefinition,
): array {
$derivatives = [];
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $configEntityTypeStorage */
$configEntityTypeStorage = $this
->entityTypeManager
->getStorage($this->configEntityTypeId);
$bundles = $configEntityTypeStorage->loadMultiple();
$weight = -1;
foreach ($bundles as $bundle) {
$viewsId = "{{ idPrefix }}_{$this->contentEntityTypeId}_{$bundle->id()}_admin";
/** @var \Drupal\views\Entity\View $view */
$view = $this->viewStorage->load($viewsId);
if (!$view) {
continue;
}
$idSuffix = "bundle_{$bundle->id()}";
$viewExecutable = $view->getExecutable();
$viewRouteName = $existingRoutes[$viewExecutable->storage->id() . '.overview'];
$derivatives[$idSuffix] = [
'route_name' => $viewRouteName,
'parent_id' => $this->primaryTaskId,
'weight' => ++$weight,
'title' => $viewExecutable->getTitle(),
];
$derivatives[$idSuffix] += $basePluginDefinition;
}
return $derivatives;
}
}
