When having a block that must only be visible on a 200 status code the condition will always prematurely return FALSE since there is no exception.

if (!$this->requestStack->getCurrentRequest()->attributes->has('exception')) {
  return FALSE;
}

I'm wondering if it's safe to assume that the status code is 200 if there's no exception. If yes, this could be handled pretty easily.

$config_response_codes = explode("\n", $response_code_list);
// Trim values.
$config_response_codes = array_map('trim', $config_response_codes);
// Create associative array.
$config_response_codes = array_combine($config_response_codes, $config_response_codes);
if (!$this->requestStack->getCurrentRequest()->attributes->has('exception') && isset($config_response_codes['200'])) {
  return TRUE;
}

But I'm not sure how safe this is to just assume it that way.

Comments

leymannx created an issue. See original summary.

norman.lol’s picture

Issue summary: View changes

Updated code snippet to trim array values. With an associative array you can also completely drop the foreach later and just use the following snippet.

if (isset($config_response_codes[$response_code])) {
  return TRUE;
}
norman.lol’s picture

Title: Block only visible on 200 status code » Allow blocks visibility to be limited to 200 status code

Updated title.

norman.lol’s picture

Title: Allow blocks visibility to be limited to 200 status code » Allow block visibility to be limited to 200 status code

Updated title.

norman.lol’s picture

Issue summary: View changes

Simplified code.

norman.lol’s picture

Issue summary: View changes

Fixed typo.

norman.lol’s picture

Issue summary: View changes

Sorry for the multiple edits. Coding first in the text editor here on drupal.org is no good idea.

norman.lol’s picture