Replace placeholder in custom block with preprocess hook This is the first time I'm using the preprocees hook, apologies in advance.
I added a placeholder, {{user-uid}}, in a custom block body field, &lt;a href=&quot;advisor/{{user-uid}}&quot;&gt;Advisor Page&lt;/a&gt;&lt;/h5&gt;.
My code is very simple, I have this hook theme.
    function boostrap_kampuster_preprocess_block(&amp;$variables) {
        if (isset($variables['content']['#block_content'])) {
            $content = $variables['content']['#block_content'];
            
            /** custom block ID is 4 **/
            if( method_exists($content,'id') &amp;&amp; $content-&gt;id() == '4') {
    
                $body = $variables['content']['body'][0]['#text'];
                
                $vars['user'] = \Drupal\user\Entity\User::load(\Drupal::currentUser()-&gt;id());
                $body=str_replace('{{user-uid}}',$vars['user'],$body);
            }
        }
    }

I am not getting any errors but the function is not doing what it is supposed to do and replace {{user-uid}} with logged in user's ID either.
FYI, I cleared caches several times.
In addition to @sonfd answer you also need to consider caching. The easiest way would be to add a cache context:
$variables['#cache']['contexts'][] = 'user';

But then the database needs to store a cached version for each user.
Better for performance would be to postpone replacing the placeholder by attaching a lazy builder to the render array:
function mytheme_preprocess_block(&amp;$variables) {
  $variables['#attached']['placeholders']['{{user-uid}}'] = [
    '#lazy_builder' =&gt; ['_mytheme_replace_uid', []],
  ];
}

which then returns the markup of the user UID:
function _mytheme_replace_uid() {
  return [
    '#markup' =&gt; \Drupal::currentUser()-&gt;id(),
    '#cache' =&gt; ['contexts' =&gt; ['user']],
  ];
}

Drupal 9
Drupal 9 requires using only trusted callbacks. So define a class implementing TrustedCallbackInterface or RenderCallbackInterface:
mytheme/src/MythemeLazyBuilders.php:
&lt;?php

namespace Drupal\mytheme;

use Drupal\Core\Render\Element\RenderCallbackInterface;

class MythemeLazyBuilders implements RenderCallbackInterface {

  public static function replaceUid() {
    return [
      '#markup' =&gt; \Drupal::currentUser()-&gt;id(),
      '#cache' =&gt; ['contexts' =&gt; ['user']],
    ];
  }

}

and add it to the render array:
function mytheme_preprocess_block(&amp;$variables) {
  $variables['#attached']['placeholders']['{{user-uid}}'] = [
    '#lazy_builder' =&gt; ['\Drupal\mytheme\MythemeLazyBuilders::replaceUid', []],
  ];
}