0

I have created a custom block in a custom module. Sometimes the block appears correctly, and sometimes the block doesn't appear. The same happens when I clear the cache.

The build() method returns the following value.

return [
  '#markup' => $html,
  '#cache' => array('max-age' => 0)
];

I also tried with the following code.

return (array(
  '#type' => 'markup',
  '#cache' => array('max-age' => 0),
  '#markup' => $html,
));

These are the other methods of the block.

protected function blockAccess(AccountInterface $account) {
  return AccessResult::allowedIfHasPermission($account, 'access content');
}

public function blockForm($form, FormStateInterface $form_state) {
  $config = $this->getConfiguration();

  return $form;
}

public function blockSubmit($form, FormStateInterface $form_state) {
  $this->configuration['custom_block_settings'] = $form_state->getValue('custom_block_settings');
}
  • From the given code I cannot see anything producing this (I did try to reproduce it myself) What Drupal version do you use? Who is the user? (anonymous, authenticated, a certain role, etc?) What caching modules do you use? Do you use the Masquerade module, etc? – johndevman Mar 5 '18 at 7:08
  • Drupal version: 8.4.4 No cache module is being used rather cache is disabled. User Role: Any – Mohit Wadhwa Mar 5 '18 at 7:11
  • 1
    Are you able to reproduce it on a clean install with only the block? – johndevman Mar 5 '18 at 7:20
  • I think you can try these steps to make it work. drupal.stackexchange.com/questions/85490/… – Alen Simonyan Mar 5 '18 at 7:23
  • 2
    Try splitting the problem up by replacing $html with a constant string. If this always shows up, it's probably how or what you generate for $html. If it still persists with a constant string, it's something with your setup. Maybe an upstream alter on the block or a permission. Also, make sure you don't have any 3rd party caching happening. E.g. CloudFlare, NGinx, Varnish and the like. – CG Monroe Mar 7 '18 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.