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' => [],
        ];

       
       
    }
}

留言

此網誌的熱門文章

Install Drupal 10 with composer in XAMPP htdocs OR PHP Command

fix drupal 11 install error

[custom block module]Correcting Heredoc Syntax and Attaching JS