Merge FarmAssetAddLogAction and FarmAddLogPrepopulate into FarmActions and AddEntity classes in farm_ui_actions module.
This commit is contained in:
parent
59e25655ae
commit
22ae662113
|
@ -66,6 +66,26 @@ class FarmActions extends DeriverBase {
|
|||
$this->derivatives[$name]['appears_on'][] = 'view.farm_' . $type . '.page_type';
|
||||
$this->derivatives[$name]['bundle_parameter'] = 'arg_0';
|
||||
}
|
||||
|
||||
// Generate links to [entity-type]/add/[bundle]?asset=[id] on asset pages.
|
||||
if ($type == 'log') {
|
||||
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('log');
|
||||
foreach ($bundles as $bundle => $bundle_info) {
|
||||
$name = 'farm.asset.add.' . $type . '.' . $bundle;
|
||||
$this->derivatives[$name] = $base_plugin_definition;
|
||||
$this->derivatives[$name]['route_name'] = 'entity.' . $type . '.add_form';
|
||||
$this->derivatives[$name]['class'] = 'Drupal\farm_ui_action\Plugin\Menu\LocalAction\AddEntity';
|
||||
$this->derivatives[$name]['entity_type'] = $type;
|
||||
$this->derivatives[$name]['bundle'] = $bundle;
|
||||
$this->derivatives[$name]['appears_on'][] = 'entity.asset.canonical';
|
||||
$this->derivatives[$name]['prepopulate'] = [
|
||||
'asset' => [
|
||||
'route_parameter' => 'asset',
|
||||
],
|
||||
];
|
||||
$this->derivatives[$name]['cache_tags'] = ['entity_bundles'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return parent::getDerivativeDefinitions($base_plugin_definition);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Drupal\farm_ui_action\Plugin\Menu\LocalAction;
|
||||
|
||||
use Drupal\asset\Entity\AssetInterface;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\Menu\LocalActionDefault;
|
||||
|
@ -83,6 +84,56 @@ class AddEntity extends LocalActionDefault {
|
|||
return $this->t('Add @bundle @entity_type', ['@bundle' => $bundle_label, '@entity_type' => $entity_type_label]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getOptions(RouteMatchInterface $route_match) {
|
||||
$options = parent::getOptions($route_match);
|
||||
|
||||
// Bail if there are no fields to prepopulate.
|
||||
if (empty($this->pluginDefinition['prepopulate'])) {
|
||||
return $options;
|
||||
}
|
||||
|
||||
// Check if there is an asset field to prepopulate.
|
||||
if (!empty($this->pluginDefinition['prepopulate']['asset'])) {
|
||||
|
||||
$asset_id = NULL;
|
||||
|
||||
// If an asset id is specified, use it.
|
||||
if (!empty($this->pluginDefinition['prepopulate']['asset']['id'])) {
|
||||
$asset_id = $this->pluginDefinition['prepopulate']['asset']['id'];
|
||||
}
|
||||
|
||||
// If a route parameter is specified, use it instead.
|
||||
if (!empty($this->pluginDefinition['prepopulate']['asset']['route_parameter'])) {
|
||||
|
||||
// Get the asset.
|
||||
$asset_param = $this->pluginDefinition['prepopulate']['asset']['route_parameter'];
|
||||
$asset = $route_match->getParameter($asset_param);
|
||||
|
||||
// If the parameter returned an entity, get its ID.
|
||||
if ($asset instanceof AssetInterface) {
|
||||
$asset_id = $asset->id();
|
||||
}
|
||||
// Else, assume the parameter is the asset ID.
|
||||
else {
|
||||
$asset_id = $asset;
|
||||
}
|
||||
}
|
||||
|
||||
// Continue if the asset_id was found.
|
||||
if (!empty($asset_id)) {
|
||||
|
||||
// Build a query param to prepopulate the asset field in the log form.
|
||||
$param = 'asset';
|
||||
$options['query'][$param] = $asset_id;
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
farm.asset.add_log.type:
|
||||
class: Drupal\Core\Menu\LocalActionDefault
|
||||
deriver: Drupal\farm_ui_menu\Plugin\Derivative\FarmAssetAddLogActionLink
|
|
@ -1,43 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_ui_menu\Plugin\Derivative;
|
||||
|
||||
use Drupal\Component\Plugin\Derivative\DeriverBase;
|
||||
|
||||
/**
|
||||
* Provides action links to create logs from the asset view.
|
||||
*/
|
||||
class FarmAssetAddLogActionLink extends DeriverBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDerivativeDefinitions($base_plugin_definition) {
|
||||
$links = [];
|
||||
|
||||
// Add links for each log type.
|
||||
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('log');
|
||||
foreach ($bundles as $type => $info) {
|
||||
$links['farm.asset.add_log.' . $type] = [
|
||||
'title' => 'Add ' . $info['label'],
|
||||
'route_name' => 'entity.log.add_form',
|
||||
'class' => '\Drupal\farm_ui_menu\Plugin\Menu\LocalAction\FarmAddLogPrepopulate',
|
||||
'appears_on' => [
|
||||
'entity.asset.canonical',
|
||||
],
|
||||
'route_parameters' => ['log_type' => $type],
|
||||
'cache_tags' => [
|
||||
'entity_bundles',
|
||||
],
|
||||
'prepopulate' => [
|
||||
'asset' => [
|
||||
'route_parameter' => 'asset',
|
||||
],
|
||||
],
|
||||
] + $base_plugin_definition;
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,128 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_ui_menu\Plugin\Menu\LocalAction;
|
||||
|
||||
use Drupal\asset\Entity\AssetInterface;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\Menu\LocalActionDefault;
|
||||
use Drupal\Core\Routing\RedirectDestinationInterface;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Routing\RouteProviderInterface;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Creates an action link to add logs.
|
||||
*
|
||||
* The 'prepopulate' key from the action link configuration is used to specify
|
||||
* which field and value to prepopulate.
|
||||
*/
|
||||
class FarmAddLogPrepopulate extends LocalActionDefault {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* The redirect destination.
|
||||
*
|
||||
* @var \Drupal\Core\Routing\RedirectDestinationInterface
|
||||
*/
|
||||
private $redirectDestination;
|
||||
|
||||
/**
|
||||
* The entity type manager service.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||
*/
|
||||
private $entityTypeManager;
|
||||
|
||||
/**
|
||||
* Constructs a FarmAddLogPrepopulate object.
|
||||
*
|
||||
* @param array $configuration
|
||||
* A configuration array containing information about the plugin instance.
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the plugin instance.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
|
||||
* The route provider to load routes by name.
|
||||
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
|
||||
* The redirect destination.
|
||||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
||||
* The entity type manager service.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, RedirectDestinationInterface $redirect_destination, EntityTypeManagerInterface $entity_type_manager) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider);
|
||||
|
||||
$this->redirectDestination = $redirect_destination;
|
||||
$this->entityTypeManager = $entity_type_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('router.route_provider'),
|
||||
$container->get('redirect.destination'),
|
||||
$container->get('entity_type.manager'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getOptions(RouteMatchInterface $route_match) {
|
||||
$options = parent::getOptions($route_match);
|
||||
|
||||
// Append the current path as destination to the query string.
|
||||
$options['query']['destination'] = $this->redirectDestination->get();
|
||||
|
||||
// Bail if there are no fields to prepopulate.
|
||||
if (empty($this->pluginDefinition['prepopulate'])) {
|
||||
return $options;
|
||||
}
|
||||
|
||||
// Check if there is an asset field to prepopulate.
|
||||
if (!empty($this->pluginDefinition['prepopulate']['asset'])) {
|
||||
|
||||
$asset_id = NULL;
|
||||
|
||||
// If an asset id is specified, use it.
|
||||
if (!empty($this->pluginDefinition['prepopulate']['asset']['id'])) {
|
||||
$asset_id = $this->pluginDefinition['prepopulate']['asset']['id'];
|
||||
}
|
||||
|
||||
// If a route parameter is specified, use it instead.
|
||||
if (!empty($this->pluginDefinition['prepopulate']['asset']['route_parameter'])) {
|
||||
|
||||
// Get the asset.
|
||||
$asset_param = $this->pluginDefinition['prepopulate']['asset']['route_parameter'];
|
||||
$asset = $route_match->getParameter($asset_param);
|
||||
|
||||
// If the parameter returned an entity, get its ID.
|
||||
if ($asset instanceof AssetInterface) {
|
||||
$asset_id = $asset->id();
|
||||
}
|
||||
// Else, assume the parameter is the asset ID.
|
||||
else {
|
||||
$asset_id = $asset;
|
||||
}
|
||||
}
|
||||
|
||||
// Continue if the asset_id was found.
|
||||
if (!empty($asset_id)) {
|
||||
|
||||
// Build a query param to prepopulate the asset field in the log form.
|
||||
$param = 'asset';
|
||||
$options['query'][$param] = $asset_id;
|
||||
}
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue