Problem/Motivation
The Counter block only initializes several variables inside conditional blocks, but always passes those variables to the render array. If an administrator disables one or more display options, the corresponding variable is never defined before being used.
On sites that convert PHP warnings/notices to logged errors, this can create runtime warnings and noisy logs when the block renders.
Warning: Undefined variable $published_nodes in Drupal\counter\Plugin\Block\CounterBlock->build() (line 183 of /var/www/html/debug/web/modules/contrib/counter/src/Plugin/Block/CounterBlock.php)
Warning: Undefined variable $registered_user in Drupal\counter\Plugin\Block\CounterBlock->build() (line 180 of /var/www/html/debug/web/modules/contrib/counter/src/Plugin/Block/CounterBlock.php)
Warning: Undefined variable $unique_visitor in Drupal\counter\Plugin\Block\CounterBlock->build() (line 179 of /var/www/html/debug/web/modules/contrib/counter/src/Plugin/Block/CounterBlock.php)
Warning: Undefined variable $site_counter in Drupal\counter\Plugin\Block\CounterBlock->build() (line 178 of /var/www/html/debug/web/modules/contrib/counter/src/Plugin/Block/CounterBlock.php)
Steps to reproduce
1. Install Drupal Drupal 11.4.
2. Install and enable Counter 8.x-1.6.
3. Place the "Counter" block in a visible region.
4. Go to the Counter settings form.
5. Disable one or more display options, for example:
- Show Site Counter
- Show Unique Visitors
- Show Registered Users
- Show Published Nodes
6. Save configuration.
7. Rebuild caches.
8. Visit a page where the Counter block is displayed.
9. Check PHP/Drupal logs.
Proposed resolution
Initialize all output variables before conditionals:
$site_counter = NULL;
$unique_visitor = NULL;
$registered_user = NULL;
Or use null coalescing in the render array:
'#site_counter' => $site_counter ?? NULL,
Issue fork counter-3608644
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
ankit_kumarComment #4
ankit_kumarI was able to reproduce this on Drupal 11.4. Disabling any of the counter display options triggers PHP warnings in CounterBlock::build() because the variables are passed to the render array without being defined.
I've created an MR using the null coalescing operator (?? NULL) in the render array, which aligns with how CounterDashboard already handles this.
Tested locally with various options disabled, all options disabled, and all options enabled — the block renders cleanly in each case and no warnings are logged.
Moving this to Needs Review.