User inventory views with account tabs and uid redirect

Users can add bikes, parts, and tools to their inventory.

Provide listing pages to view their inventory.

Prevent non admins from viewing user inventories.

Link to user inventory from account menu.

Bikes, parts, tools secondary tabs.

Views Tabs

Views lets you set up local tasks, referred to as "menu tabs".

But it can't do secondary tasks yet.

But it can do the first part, add an Inventory tab to the user account.

  1. Create a view listing parts, tools, and bikes.
  2. Give it the url /user/%user/inventory.
  3. Set up a menu tab with Inventory as the title.
    1. (The selected menu/link does not seem to matter).
  4. Click on the My account link, and you should see an Inventory tab.

Views Local Tasks Module

This module lets you customize views page tabs further and allows secondary tabs.

  1. Configure the Inventory view display menu tab settings.
    (This seems to be necessary when using the module)
    1. Set parent to custom with entity.user.canonical.
  2. Add a view display with the url /user/%user/inventory/parts.
  3. Select menu tab.
  4. Check the box for local task only.
  5. Select the Inventory display as the parent.
  6. Add another view display with /user/%user/inventory/tools.
  7. Repeat the menu tab settings.
  8. You should now have an Inventory tab on the user display page, with Parts and Tools secondary tabs.

Inventory Menu Link

Drupal provides a "My account" in the user account menu, which routes to /user.

The response is provided by UserController::userPage(), which redirects to entity.user.canonical, with the current user's ID as the parameter.

This redirects to /user/1, which is handled by the user entity view.

We'll copy this method to provide an inventory link in the user account menu that redirects to /user/1/inventory.

  1. Use drush gen to create a controller and route.
  2. Set the route url to /user/inventory.
    1. #retrograde_custom.routing.yml
      
      retrograde_custom.inventory:
        path: '/user/inventory'
        defaults:
          _title: 'Inventory'
          _controller: '\Drupal\retrograde_custom\Controller\RetrogradeCustomController::userInventory'
        requirements:
          _permission: 'access own inventory'
  3. Add a userInventory function to the controller.
    1. #\Drupal\retrograde_custom\Controller\RetrogradeCustomController
      
      public function userInventory() {
        return $this->redirect('view.user_inventory.parts', ['user' => $this->currentUser()->id()]);
      }
  4. Clear the cache and visit /user/inventory. It should redirect to user/1/inventory.

You could add a menu link in the account menu for /user/inventory.

But this can also be provided in MODULE.links.menu.yml.

  • retrograde_custom.inventory:
      title: Inventory
      description: My inventory
      menu_name: account
      route_name: retrograde_custom.inventory
      weight: 1

Results

Project