Issue #3187558 by paul121, m.stenta: Create action links to add logs from the asset view pages
This commit is contained in:
commit
dcd01c169f
|
@ -61,9 +61,34 @@ class FarmActions extends DeriverBase {
|
|||
$this->derivatives[$name]['class'] = 'Drupal\farm_ui_action\Plugin\Menu\LocalAction\AddEntity';
|
||||
$this->derivatives[$name]['entity_type'] = $type;
|
||||
|
||||
// Add the entity_bundles cache tag so action links are recreated after
|
||||
// new bundles are installed.
|
||||
$this->derivatives[$name]['cache_tags'] = ['entity_bundles'];
|
||||
|
||||
// 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';
|
||||
}
|
||||
|
||||
// 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'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
@ -74,7 +75,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();
|
||||
|
@ -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}
|
||||
*/
|
||||
|
@ -92,10 +143,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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -60,4 +60,24 @@ function farm_ui_theme_install() {
|
|||
],
|
||||
];
|
||||
Block::create($values)->save();
|
||||
|
||||
// Modify the block.block.gin_local_actions config to use our class.
|
||||
$local_actions_block = \Drupal::configFactory()->getEditable('block.block.gin_local_actions');
|
||||
$local_actions_block->set('plugin', 'farm_local_actions_block');
|
||||
$local_actions_block->set('settings.id', 'farm_local_actions_block');
|
||||
$local_actions_block->set('settings.provider', 'farm_ui_theme');
|
||||
$local_actions_block->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_uninstall().
|
||||
*/
|
||||
function farm_ui_theme_uninstall() {
|
||||
|
||||
// Revert changes to block.block.gin_local_actions config.
|
||||
$local_actions_block = \Drupal::configFactory()->getEditable('block.block.gin_local_actions');
|
||||
$local_actions_block->set('plugin', 'local_actions_block');
|
||||
$local_actions_block->set('settings.id', 'local_actions_block');
|
||||
$local_actions_block->set('settings.provider', 'core');
|
||||
$local_actions_block->save();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_ui_theme\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Menu\Plugin\Block\LocalActionsBlock;
|
||||
|
||||
/**
|
||||
* Provides a block to display the local actions.
|
||||
*
|
||||
* @Block(
|
||||
* id = "farm_local_actions_block",
|
||||
* admin_label = @Translation("Primary farm admin actions")
|
||||
* )
|
||||
*/
|
||||
class FarmLocalActionsBlock extends LocalActionsBlock {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
|
||||
// Render local actions as a dropbutton.
|
||||
$local_actions = parent::build();
|
||||
$links = [];
|
||||
foreach ($local_actions as $local_action) {
|
||||
if (!empty($local_action['#link']) && $local_action['#access']->isAllowed()) {
|
||||
|
||||
// Copy localized_options into the URL options.
|
||||
// This is necessary to maintain any query parameters that were added
|
||||
// to the action links.
|
||||
$local_action['#link']['url']->setOptions(array_merge($local_action['#link']['url']->getOptions(), $local_action['#link']['localized_options']));
|
||||
|
||||
// Add the link.
|
||||
$links[] = $local_action['#link'];
|
||||
}
|
||||
}
|
||||
return [
|
||||
'#type' => 'dropbutton',
|
||||
'#dropbutton_type' => 'standard',
|
||||
'#links' => $links,
|
||||
'#cache' => $local_actions['#cache'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue