Skip to content

Instantly share code, notes, and snippets.

@timneutkens
Last active September 27, 2016 10:05
Show Gist options
  • Save timneutkens/d461cdaa4f57bdd36c78ac35304b793a to your computer and use it in GitHub Desktop.
Save timneutkens/d461cdaa4f57bdd36c78ac35304b793a to your computer and use it in GitHub Desktop.
Load a block based on deployment mode (development, default, production) Magento 2
<?php namespace TimNeutkens\Example\Block;
use Magento\Framework\App\State;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
class ExampleBlock extends Template {
protected $developerMode;
public function __construct(Context $context, array $data = []) {
/** @var State $state */
$state = $context->getAppState();
// Check if current mode is developer mode
$this->developerMode = $state->getMode() === $state::MODE_DEVELOPER;
// Create the template block
parent::__construct($context, $data);
}
public function _toHtml() {
// Don't render block when in developer mode
return !$this->developerMode ? parent::_toHtml() : '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment