add "+Add content" button to a view page
function MY_MODULE_NAME_menu_local_actions_alter(&$local_actions)
{
// Alter local actions if needed
// Check current route
$current_route = \Drupal::routeMatch()->getRouteName();
$viewId = 'contentcontent';
$viewDisplay = 'page_1';
if (str_starts_with($current_route, 'view.contentcontent')) {
// Add a local action button to add new content
$local_actions['node.add_page'] = [
'title' => "add",
'route_name' => 'node.add',
'route_parameters' => [
'node_type' => 'article'
],
'appears_on' => [
'view.'.$viewId.'.'.$viewDisplay,
],
'class' => 'Drupal\Core\Menu\LocalActionDefault',
'weight' => 10,
'options' => [],
];
}
}
留言
發佈留言