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; $bundle = $type; $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().