Generalize AddEntity LocalAction bundle parameter logic.

Derivatives can either define a bundle_parameter to look for
(eg: 'arg_0' in the case of Views), OR explicitly define the
bundle.

This will allow FarmAddLogPrepopulate to be merged into the
Menu/LocalAction/AddEntity class, and FarmAssetAddLogAction
to be merged into the Derivative\FarmActions class.
This commit is contained in:
Michael Stenta 2021-09-08 19:34:53 -04:00
parent a4aa8a1859
commit 59e25655ae
2 changed files with 29 additions and 2 deletions

View File

@ -64,6 +64,7 @@ class FarmActions extends DeriverBase {
// Add it to entity bundle Views, if the farm_ui_views module is enabled.
if (\Drupal::moduleHandler()->moduleExists('farm_ui_views')) {
$this->derivatives[$name]['appears_on'][] = 'view.farm_' . $type . '.page_type';
$this->derivatives[$name]['bundle_parameter'] = 'arg_0';
}
}

View File

@ -74,7 +74,7 @@ class AddEntity extends LocalActionDefault {
// Get the bundle machine name.
$route_match = RouteMatch::createFromRequest($request);
$bundle = $route_match->getparameter('arg_0');
$bundle = $this->getBundle($route_match);
// Get the bundle label.
$bundle_label = $this->entityTypeManager->getStorage($entity_type->getBundleEntityType())->load($bundle)->label();
@ -92,10 +92,36 @@ class AddEntity extends LocalActionDefault {
$entity_type = $this->pluginDefinition['entity_type'];
$entity_type_param = $entity_type . '_type';
// Get the bundle machine name.
$bundle = $this->getBundle($route_match);
// Set the entity_type parameter for the entity.type.add_form route.
return [
$entity_type_param => $route_match->getParameter('arg_0'),
$entity_type_param => $bundle,
];
}
/**
* Get the bundle machine name.
*
* This will first look for an explicit bundle set in the plugin definition.
* If that fails, then it will look for a bundle parameter in the route.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* Route match object.
*
* @return string
* Bundle machine name.
*/
protected function getBundle(RouteMatchInterface $route_match) {
$bundle = NULL;
if (!empty($this->pluginDefinition['bundle'])) {
$bundle = $this->pluginDefinition['bundle'];
}
elseif (!empty($this->pluginDefinition['bundle_parameter'])) {
$bundle = $route_match->getParameter($this->pluginDefinition['bundle_parameter']);
}
return $bundle;
}
}