salesforce-8.x-4.x-dev/modules/salesforce_mapping_ui/src/Plugin/Derivative/SalesforceMappingExtraLinks.php
modules/salesforce_mapping_ui/src/Plugin/Derivative/SalesforceMappingExtraLinks.php
<?php
namespace Drupal\salesforce_mapping_ui\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\system\Entity\Menu;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a default implementation for menu link plugins.
*/
class SalesforceMappingExtraLinks extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* The admin toolbar tools configuration.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, RouteProviderInterface $route_provider, ThemeHandlerInterface $theme_handler, ConfigFactoryInterface $config_factory, AccountInterface $current_user) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->routeProvider = $route_provider;
$this->themeHandler = $theme_handler;
$this->config = $config_factory->get('admin_toolbar_tools.settings');
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('router.route_provider'),
$container->get('theme_handler'),
$container->get('config.factory'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$links = [];
$entity_types = $this->entityTypeManager->getDefinitions();
$content_entities = [];
foreach ($entity_types as $key => $entity_type) {
if ($entity_type->getBundleEntityType() && ($entity_type->get('field_ui_base_route') != '')) {
$content_entities[$key] = [
'content_entity' => $key,
'content_entity_bundle' => $entity_type->getBundleEntityType(),
];
}
}
$links['entity.salesforce_mapping.add_form'] = [
'menu_name' => 'admin',
'route_name' => 'entity.salesforce_mapping.add_form',
'parent' => 'salesforce_mapping.admin',
'title' => 'Add Mapping',
'provider' => 'salesforce_mapping_ui',
];
$mappings = $this->entityTypeManager->getStorage('salesforce_mapping')
->loadMultiple();
foreach ($mappings as $mapping) {
$mapping_root = 'entity.salesforce_mapping.edit_form.' . $mapping->id();
$links[$mapping_root] = [
'menu_name' => 'admin',
'route_name' => 'entity.salesforce_mapping.fields',
'parent' => 'salesforce_mapping.admin',
'route_parameters' => ['salesforce_mapping' => $mapping->id()],
'title' => $mapping->label(),
'provider' => 'salesforce_mapping_ui',
];
$links[$mapping_root . '.fields'] = [
'menu_name' => 'admin',
'route_name' => 'entity.salesforce_mapping.fields',
'parent' => $base_plugin_definition['id'] . ':' . $mapping_root,
'route_parameters' => ['salesforce_mapping' => $mapping->id()],
'title' => 'Fields',
'provider' => 'salesforce_mapping_ui',
];
$links[$mapping_root . '.settings'] = [
'menu_name' => 'admin',
'route_name' => 'entity.salesforce_mapping.edit_form',
'parent' => $base_plugin_definition['id'] . ':' . $mapping_root,
'route_parameters' => ['salesforce_mapping' => $mapping->id()],
'title' => 'Settings',
'provider' => 'salesforce_mapping_ui',
];
}
return $links;
}
/**
* Determine if a route exists by name.
*
* @param string $route_name
* The name of the route to check.
*
* @return bool
* Whether a route with that route name exists.
*/
public function routeExists($route_name) {
return (count($this->routeProvider->getRoutesByNames([$route_name])) === 1);
}
}
