How to build a link to a field_ui form? Given an entity type id of a fieldable entity type, a bundle name, and a view mode name, how do I construct a url to the respective field_ui form where this view mode can be edited?

The patterns for the route name seem to be:


entity.entity_view_display.ENTITY_TYPE_ID.view_mode
entity.entity_view_display.ENTITY_TYPE_ID.default


But it seems the parameters have custom names, e.g. /admin/structure/types/manage/{node_type}/display/{view_mode_name}, here the bundle parameter is called node_type.

Is there a generic way to build such urls to field_ui pages for any entity type?
After searching in the core files I found the pattern, see the example below:

use Drupal\Core\Url;
// The entity type can be 'node'.
$entity_type = 'taxonomy_term';

// Find the bundle entity type, e.g. 'node_type' or .
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $etm */
$etm = \Drupal::service('entity_type.manager');
$bundle_entity_type = $etm-&gt;getDefinition($entity_type)-&gt;getBundleEntityType();

if (NULL === $bundle_entity_type) {
  // Not clear what to do in this case.
  return NULL;
}

// The bundle can be 'article'.
$bundle = 'tags';
// The route, see that the $entity_type variable is used.
$route = "entity.entity_view_display.$entity_type.view_mode";
// The view mode can be 'teaser'.
$view_mode = 'full';
$a = Url::fromRoute($route, [$bundle_entity_type =&gt; $bundle, 'view_mode_name' =&gt; $view_mode])-&gt;toString();
// To use dpm() you need the Devel module.
dpm($a);