How do I use dpm() or kint() to debug a block plugin I created? I created a custom module which implements a block plugin. The code was generated with Drupal Console. When I try to debug the code within the build function, I get a function not found error.

Why doesn't my code print the value of $build?

namespace Drupal\my_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'DefaultBlock' block.
 *
 * @Block(
 *  id = "default_block",
 *  admin_label = @Translation("test block"),
 * )
 */
class DefaultBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];

    $build['default_block']['#markup'] = '1:Implement DefaultBlock.';
    dpm($build);
    //kint($build);
    return $build;
  }

}
dpm() definitely works within a custom block; I just tested. Since it calls a service, you may need to clear your cache before it shows up, for example with the command line with drupal cache:rebuild all or drush cr.

To test if the service is available, you could use this code.

dump( \Drupal::service('devel.dumper')); die();


Another thing to consider is that blocks are cached in Drupal 8 by default.  Make sure your build() is actually executing. You may want to turn off the rendering cache while developing, in settings.php or settings.local.php.

$settings['cache']['bins']['render'] = 'cache.backend.null';