How to change the site logo in a preprocess function? I want to set my site logo programmatically, so that I can change the logo based on URL args.

I am able to set the $variables['logo_img']['#uri'] with the following code in MYTHEME.theme but still the logo displayed does not change. 

I have cleared registry.

I have tried setting my own logo and using default logo settings in theme settings UI:

function THEME_preprocess_page(&amp;$variables) {
  $variables['logo_img']['#uri'] = 'http://newlogo.jpg'; }


I have also tried this:

function THEME_preprocess_page(&amp;$variables) {
  global $base_url, $base_path;
  $variables['logo'] = $base_url . $base_path . drupal_get_path('theme','THEME') . '/images/logos/newlogo.svg';
}


Any help?
How to change the site logo in .theme preprocess function?

The site logo is no longer in the page template. You can change it now in the branding block:

function mytheme_preprocess_block(&amp;$variables) {
  if ($variables['base_plugin_id'] == 'system_branding_block') {
    $variables['site_logo'] = '/newlogo.jpg';
  }
}


Caching

You mentioned you want to change the logo based on URL args. This would be difficult to do this in the system branding block, because the template is not rendering the render array, but transferring only the content of three variables and so any attempt to add cache meta data would be useless.

You have two options. Implement your own site logo block plugin in a module or use this theming only solution:


disable the site logo in the branding block
set a variable with the url for the logo in page preprocess $variables['logo']
add your own markup to the page twig to output the site logo


page.html.twig:

&lt;a href="{{ path('&lt;front&gt;') }}" title="{{ 'Home'|t }}"&gt;
   &lt;img src="{{ logo }}" alt="{{ 'Home'|t }}" /&gt;
&lt;/a&gt;


By doing this in the page template you avoid caching problems because the page is already cached for each url separately.