Variable passed into a preprocess function has incorrect data or or an unexpected format I wanted to change some of the forum’s topic's preprocessing. So I copied template_preprocess_forums() into a custom module (myModule), and renamed the function myModule_preprocess_forums().

I get an error that a method cannot be used on a string, $topic. I inspected. When the code is looping through the array $variables['topics'], it expects each array item to be an object.  However, in $variables['topics'] there are items that are strings and arrays and no topic objects. 

To debug, I inspected $variables['topics'] in the Forums module, but all the  items are objects, as expected. 

Why would the values passed to myModule_preprocess_forums() differ from what template_preprocess_forums() implemented by the Forums module gets?
It happens because template_preprocess_forums(), which is executed before any hook_preprocess_forums() implementation, overrides $variables['topics'] with the render array for its table. 

It would have been better to put the render array in a different array value. As you notice from the function code, starting from Drupal 8.6.x, the original value of $variables['topics'] is available in $variables['topics_original'].

  $variables['topics_original'] = $variables['topics'];
  $variables['topics'] = $table;
  $variables['topics_pager'] = [
    '#type' =&gt; 'pager',
  ];