How can I programmatically change the view mode?
You can select VIew Mode as:

function YOUR_MODULE_entity_view_mode_alter(&amp;$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
  if ($entity-&gt;getEntityTypeId() == 'taxonomy_term' &amp;&amp; $entity-&gt;bundle() == 'tag' &amp;&amp; $view_mode == 'full') {
    $view_mode = 'CUSTOM VIEW MODE';
  }
}