I am the maintainer for Views Add Button: https://www.drupal.org/project/views_add_button , which creates a button (link with classes) in a Views header or footer, that allows the user to add content of a particular type. Link building and access checking are meant to be handled automatically by the module, so that the user does not have to write a custom plugin or put a "view in a view" to handle access control.

While working on a Drupal 8 port I'm running into some trouble getting the correct route for entities. At this time, the options form for the plugin will pass an entity type and bundle, but given that there are many entity types, some with bundles and some without, I am not sure how to calculate the route name for a given entity-bundle combination.

So far, I've written this to manually create the URL, based on some assumptions. I left the options building out as it is not relevant to the topic:

// Get the create route
$type = explode('+', $this->options['type'], 2);
$entity_type = $type[0];
$bundle = $type[1];
$u = $entity_type === $bundle ? '/' . $entity_type . '/add': '/' .$entity_type . '/add/' . $bundle;
// Create the link
$url = Url::fromUserInput($u, $opts);

The goal is to use Url::fromRoute() instead of Url::fromUserInput, using the entity type and bundle. For example, 'node' and 'article' should return 'node.add' , which would then be given to Url::fromRoute().

  • 1
    An entity type doesn't necessarily have to have an add form, so I guess it's not something you can determine via the API. Take node v. user for example: nodes have a create link listed in their annotation, whereas users don't, they have a register form (not link) declared for that instead. Then have a look at the differences in the way the routes are created for each - nothing common there at all. Unless there's a higher level API somewhere that I'm not aware of, I think you're out of luck
    – Clive
    Commented Sep 7, 2018 at 8:41

1 Answer 1


Given Clive's comment, and some further thought about it, one thing I could do is create an annotation, such that custom entity developers can declare their route. Further, I can add annotations to the module that cover all core cases (node/user/taxonomy), such that the module works with core out of the box.

From there, I could add conditional logic that looks for a given entity's annotation, and if it doesn't find it, it would then make the same assumption (/{entity_type}/add/{bundle}) as above. I suppose from there either developers can declare an annotation in their own module, or if a particular custom entity is popular and keeps hitting the issue queue, I could add that as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.