1

I have a custom block that is found by "Place Block" but does not actually get placed. The spinny just spins for second and then nothing happens; the screen just stays at the list of available blocks.

  1. Is there any method within Drupal 8 to see what the error is?
  2. What incompatibilities might be causing this?

Setup Notes

  1. Drupal 8.1
  2. Lots of cache refreshing attempts
  3. Lots of uninstall/reinstall attempts
  4. Same behavior on a fresh D8 install
  5. The module does install and the controller works at its prescribed URL
  6. The base directory of the module is nrs_raceroftheday

This is the content of ROTDBlock.php.

/**
 * @file
 * Contains \Drupal\nrs_raceroftheday\Plugin\Block\ROTDBlock. 
 */

namespace Drupal\nrs_raceroftheday\Plugin\Block;
use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Racer Of the Day' Block
 *
 * @Block(
 *   id = "rotd_block",
 *   admin_label = @Translation("RacerOTD v4 block"),
 *   )
 */
class ROTDBlock extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() 
    {
    $pageContent='asdf';
    return array('#markup' => $this->t($pageContent),  );
  }
}

Here's the routing file nrs_raceroftheday.routing.yml.

nrs_raceroftheday.controller:
  path: '/racer-of-the-day'
  defaults:
    _controller: 'Drupal\nrs_raceroftheday\Controller\NrsRacerOfTheDayController::content'
    _title: 'Racer of the Day'
  requirements:
    _permission: 'access content'
  • 1
    Could be any kind of error. Check the response in the network tab or open the link in a new tab (right click, open in new tab) or check your apache error log. – Berdir May 1 '16 at 16:37
  • Well, it couldn't be ANY kind of error. The most likely that I've seen is a naming mismatch, between file name and class name. That doesn't seem to be the case here. I can't open this in a new tab, it's an modal popup. – Anders8 May 1 '16 at 17:16
  • Yes you can. As I said, just open it in a new tab. Works perfectly fine and you see the error, assuming that display errors is enabled. If it's not, enable it or check the logs. Or check in the network tab, also as I said. – Berdir May 1 '16 at 17:46
  • Well how about that! Result was: The website encountered an unexpected error. Please try again later. RuntimeException: Failed to start the session because headers have already been sent by "/home/nasarall/public_html/d810/modules/nrs_raceroftheday/src/Plugin/Block/ROTDBlock.php" at line 119. Turns out... there was a space after the closing php ?> tag! And that rendered a "header output". Thanks for the clue, this was literally an invisible problem. If you create an answer that is something like "check for trailing white space" or similar I'll accept it. – Anders8 May 1 '16 at 17:51
  • 1
    That is why Drupal PHP files don't close the <?php ?> tag opened at the beginning of the file. – kiamlaluno May 1 '16 at 18:04
2

In general, that can have many possible causes. But it's very likely some sort of syntax or other error that leads to an exception or a fatal error.

Open the Place block link in a new tab to see the response, check the network tab and/or check your apache or drupal logs.

That should give you a hint about the problem. In this specific example, it was a trailing space after the closing PHP tag, as suggested by @kiamlaluno, that's why Drupal coding standard says that they should be left out.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.