Updated: Comment #0

Problem/Motivation

Most config entities follow a common pattern.

  • A list page.
  • An edit page and tab.
  • A delete page and form.
  • An add page.
  • An optional enable/disable page/callback (when there is a status entity key defined).
  • Most of these point to entity forms and list controllers anyway ie there are no true controllers, and the enable/disable can be generalized for most use cases.

As a proof of concept, this patch demonstrates its functionality by removing contact.routing.yml which (at this stage) only points to the routes above.

Note that if you want to change the path (ie keep routing separate from paths) you can still hook_entity_info_alter(), change the 'admin_path' and it will keep working. Note this patch generates route ids of the form {module}.{entity_type}_list|add|edit|delete etc.

Proposed resolution

Dynamically generate the routes for a better DX.

Remaining tasks

Review

User interface changes

None

API changes

A new 'admin_path' and 'admin_permission' annotation entry added for config entities.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

larowlan’s picture

+++ b/core/modules/config/config.module
@@ -5,6 +5,8 @@
+use Drupal\Core\Config\Entity\ConfigEntityInterface;

unintended, will remove in next patch

andypost’s picture

+1 here, awesome idea!

larowlan’s picture

Status: Active » Needs review
larowlan’s picture

larowlan’s picture

Issue summary: View changes

Updated issue summary.

larowlan’s picture

Green! flabbergasted

Gábor Hojtsy’s picture

IMHO it would be vital that we rely on the same path/route definition mechanism that entities use already. See #2083615: Use links annotation for config entity URI like for content entities where I propose to reuse the same links{} structure that is widespread for content entities. That lets us remove the url() method implementations for most entities. I see this just proposes we establish a common URL structure for list/add/edit/delete for all config entities. I think that is in itself a good goal, but it would be good to reconcile that with how content entities do this, so we can share implementations / know-how and not a custom implementation for config entities only. My approach for the instance URLs at least solves that for the instances. It does not provide a way to autogenerate the listing but at least from the instance level, it would equally let you autogenerate. Maybe we should just use the list url link type in the links array?

Gábor Hojtsy’s picture

Status: Needs review » Needs work
larowlan’s picture

Status: Needs work » Postponed

Agree completely, lets postpone this on #2083615: Use links annotation for config entity URI like for content entities and come back to supplement the 'edit-link' added there with additional 'add-link', 'list-link' and 'delete-link' entries.
Will try and review that one later today.

larowlan’s picture

Issue tags: +Prague DX smackdown
Berdir’s picture

Being able to specify a basic permission (possibly even separate ones per operation?) is something I always wanted to do, maybe we can do that in a separate issue, I think it's quite unrelated to the routing stuff?

And +1 on using the links stuff for this.

Gábor Hojtsy’s picture

Status: Postponed » Needs work
Gábor Hojtsy’s picture

Issue summary: View changes

Updated issue summary.

tim.plunkett’s picture

I think #1783964: Allow entity types to provide menu items would be suitably covered by this, should we mark that as a duplicate?

klonos’s picture

catch’s picture

Priority: Normal » Major
Issue tags: +beta target
larowlan’s picture

bump (I keep losing this)

dawehner’s picture

Assigned: Unassigned » dawehner

I will try to work on it.

Berdir’s picture

This is fun as long as you want it exactly standardized. Looking at core, many entity types have some special cases and handling.

The part that I'm worried about is when it's magically generated, is that it's going to be a) harder to understand and b) harder to extend/alter when you don't want it to work exactly like the generated standard.

I'm wondering if we should pursue code scaffolding approaches instead, that generate a routing file (for example) for you, that you can then easily change and customize as you need to.

Xano’s picture

At the very least this needs work, because the routes use permissions directly rather than entity access, which is a bug and a possible security problem.

I also agree with what @Berdir said in #17: this works fine for a few entities in core, but not for all of them, let alone the ones in contrib. It also only takes some operations into account. What about #2201137: Introduce a "duplicate" entity operation, for instance?

andypost’s picture

Suppose this suitable for the most of config entities, they just need "administer" permission, but fields and instances...

Gábor Hojtsy’s picture

This looks like a very similar problem to #2150747: Switch back to URI templates in entity annotations where generating those routes is also of key discussion now.

dawehner’s picture

From my perspective the motivation is not based upon any kind of core usecase or other issue. The goal for this issue, as far as I understand the initial post is not some kind of unification, some kind of having to write less. The goal is to make the out of box DX better.

You just write your basic $entity.php file and you get some kind of admin UI for free. Yes, in the real world most entity types might need some kind of customization but you get a lot of nicer experience on the way to your final code. For example provide an add/edit/delete form out of the box would really help you to write the entity type. Additional some kind of listing (implementation differs with your personal taste) would also help you on the way.

Maybe both goals (unification/autogeneration and the better DX during development) lead to similar/the same solutions, but I just wanted to write down the thoughts.

catch’s picture

This blocks two critical issues atm, so bumping to critical.

tim.plunkett’s picture

Priority: Major » Critical

Whoops, guess this missed the actual critical part.

dawehner’s picture

Assigned: dawehner » Unassigned

There is simply no reason EVER to use the assigned flag. You simply cannot trust people to work on something.

Regarding the actual issue. My primary usecase was developer experience during developing code, so you have at least something which can be shown really fast.
I didn't expected to actually use automatic route generation for production code, which seems to be the usecase of this issue.

dawehner’s picture

Status: Needs work » Needs review
FileSize
6.58 KB

Here is a basic start.

Status: Needs review » Needs work

The last submitted patch, 25: entity_route_building-2089757-25.patch, failed testing.

tstoeckler’s picture

Yay, for working on this!

@dawehner++++++

  1. +++ b/core/lib/Drupal/Core/Entity/EntityRouteBuilder.php
    @@ -0,0 +1,58 @@
    +    if ($entity_type->getAdminPermission() === TRUE) {
    +      $requirements = array('_access' => 'TRUE');
    +    }
    

    I don't think we should support this, as it's really a test-only use-case. Couldn't we rather have an EntityTestAccessController, that always returns TRUE?

  2. +++ b/core/lib/Drupal/Core/Entity/EntityRouteBuilderSubscriber.php
    @@ -0,0 +1,49 @@
    +      foreach ($entity_type->getLinkTemplates() as $name => $link_template) {
    ...
    +            $route_builder->addRoutes($name, $link_template, $entity_type, $event->getRouteCollection());
    

    Awesome! This will allow us to expand this to more templates later on.

martin107’s picture

Status: Needs work » Needs review
FileSize
6.54 KB

reroll of #25.

1 conflict resolved in the area of of RouteBulder
locally RouteBuilderTest is green.

Status: Needs review » Needs work

The last submitted patch, 28: entity_route_building-2089757-28.patch, failed testing.

tim.plunkett’s picture

+++ b/core/lib/Drupal/Core/Entity/EntityRouteBuilder.php
@@ -0,0 +1,58 @@
+ * Contains \Drupal\Core\Entity\Controller\EntityRouteBuildController.
...
+class EntityRouteBuilder extends ControllerBase {

Why would this be a Controller?

martin107’s picture

1) #31 I agree it makes no sense ControllerBase.. removed

2) Trivial update of Contains comment to reflect the correct class/filename.

Just pulling information together from test logs... while I mull things over.

The following tests fail with one of two common failure routes.

Tests
TermFieldTest, CustomBlockCacheTest, TermCacheTagsTest, DataTimeFieldTest

Routes
/entiy_test/{entity_test}
OR
/entity_test/delete/entity_test/{entity_test}

martin107’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 31: entity_route_building-2089757-31.patch, failed testing.

dawehner’s picture

Status: Needs work » Postponed
jhodgdon’s picture

dawehner’s picture

Title: Auto generate routing entries for config entities based on an 'admin_path' and 'admin_permission' annotation » Auto generate routing entries for entities based on an 'admin_path' and 'admin_permission' annotation

Let's be a little bit more honest.

tim.plunkett’s picture

Component: configuration entity system » entity system

:)

xjm’s picture

Issue tags: -Configuration system
xjm’s picture

Berdir’s picture

I'm not quite sure why/how this is blocking criticals? Is it not the other way round?

catch’s picture

Priority: Critical » Major

This is postponed on #2281645: Make entity annotations use link templates instead of route names which is major, so yes I don't think it's blocking any critical issues, and nor is it critical. If we add a bc layer where routes don't get auto-generated there's no API change.

dawehner’s picture

Status: Postponed » Closed (duplicate)