Add path assumes /[type]/add/[bundle] which is not always the case

Created on 31 October 2023, about 1 year ago
Updated 16 September 2024, about 2 months ago

Problem/Motivation

Entity Construction Kit β†’ provides entity add paths at /admin/content/[type]/add/[bundle], not at /[type]/add/[bundle], which appears to be what Views Add Button assumes is uniformly the case.

Steps to reproduce

  1. Create an ECK entity type
  2. Create a view listing entities of those type with an add button.

Clicking on the button will result in a 404.

Proposed resolution

Forgive me, but I'm back into Drupal after nearly a decade away, which has demoted me to site builder status until the stars align, a choir of angels sings, and the whole Symfony/PSR-4 thing suddenly makes sense to me.

It seems to me that there should be a service somewhere which when given an entity type and bundle name will hand back a path to the add form, but I haven't a clue where to go looking for such a thing or how to use it when I found it. (In less professional days, back on the island, it would have been a global function.)

A workaround is to create a path alias from /admin/content/[type]/add to /[type]/add. With Sub-pathauto β†’ enabled, that will automatically fix the immediate problem for all bundles of that entity type.

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code: General

Created by

πŸ‡¦πŸ‡ΊAustralia Katy Swain

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @Katy Swain
  • πŸ‡ΊπŸ‡ΈUnited States laboratory.mike

    Hi there Katy, I'm glad to explain both what the overall issue is, how I solved it when setting up the module, and will add some instructions on what to do for the ECK content.

    Problem

    The root issue concerning paths also extends to the names of routes used by certain entity types. For example, the route name for adding a node is "node.add" but for a user its "user.admin_create" . Some entities have bundles, which features into access and the add process, and further, certain entities in contrib modules, like Group, have further considerations beyond that.

    IOW, there isn't a universal way to do entity creation.

    Solution

    To manage this, I set up a plugin system, with the most relevant folder to check out being here:

    views_add_button/src/Plugin/views_add_button

    The ViewsAddButtonDefault.php file contains the default logic you are observing. It tries its best, but as you saw, it can't cover everything.

    Next, I created Node, Taxonomy, and User specific plugins, which handle specifics for each entity type.

    There's also a views_add_button_group contrib module for Groups and Group Content.

    For ECK

    If you're in a position to do so, try creating a custom module, adding an src/Plugin/views_add_button folder, and copying in one of the contrib module's plugins, and modifying it to suit your case. Otherwise, I know ECK's pretty popular and I could add a submodule to support ECK entities. I would just need your help to QA it.

  • Assigned to Terry_Kolodiy
  • Merge request !5add plugin for ECK entities β†’ (Open) created by Terry_Kolodiy
  • Issue was unassigned.
  • Status changed to Needs review 7 months ago
  • πŸ‡ΊπŸ‡¦Ukraine Terry_Kolodiy

    Hi! I've added a Merge Request with the plugin for ECK entities. Could you please check? Thanks

  • πŸ‡ΉπŸ‡­Thailand AlfTheCat

    @Terry_Kolodiy thanks very much for the patch, it works.

  • πŸ‡ΉπŸ‡­Thailand AlfTheCat

    There is one caveat here, when using ECK, there are different types of bundles for each entity type. The current patch is fine in case there are multiple bundles and users should be presented with their choice of bundle to add.

    However, in my case, I need the views add button to point to a specific bundle (there is only one) and also pass on arguments that the EPP β†’ module requires to pre-populate reference fields. Because the views add button present the entity bundle selection dialog, and with only one option in this case, the reference field does not get populated because the query string is lost.

    It would be great if we could set a target bundle in the Views add more button settings.

  • πŸ‡ΉπŸ‡­Thailand AlfTheCat

    I can add some more info to this, the bundle type selection interface pops up when the ECK entity type name is the same as the bundle name.

    So, if you have an entity type called "Task" and a bundle called "Task" (same machine names), the bundle selection UI will show, regardless of which bundle you specify in the Views Add Button settings, or if there is only one bundle available.

Production build 0.71.5 2024