How to get the entity ID in a twig template I have a custom entity, with the template my-custom-entity--my-entity.html.twig.
How do I access the ID in the template? I can do this by implementing hook_preprocess_entity, but am wondering if the ID is already available in the template.
You can just add the full object to a twig variable, so
$vars['myentity'] = $my_entity;

and then access with
{{ myentity.id }}

A bit more elegant is to implement hook_theme:
/**
 * Implements hook_theme().
 */
function MODULENAME_theme() {
  return [
    'customentity' =&gt; [
      'render element' =&gt; 'elements',
    ],
  ];
}

and a preprocess function to expose it to twigs customentity and maybe content variables:
function template_preprocess_customentity(&amp;$variables) {
  $variables['customentity'] = $variables['elements']['#node'];

  // Helpful $content variable for templates.
  $variables += ['content' =&gt; []];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}