We can develop a new visibility condition for it, It will add a new visibility condition to the block configuration form with our custom configuration. Later we can use those configuration values inside our "evaluate" function to evaluate the condition.
Develop a condition plugin by using
@Condition
annotation. Full plugin code is added below.namespace Drupal\drupaljournal_base\Plugin\Condition; use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RequestStack; /** * Provides 'HTTP 4XX Requests' condition. * * @Condition( * id = "http_4xx_request", * label = @Translation("HTTP 4XX Requests"), * ) */ class Http4xxRequestCondition extends ConditionPluginBase implements ContainerFactoryPluginInterface { /** * The request stack. * * @var \Symfony\Component\HttpFoundation\RequestStack */ protected $requestStack; /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $container->get('request_stack'), $configuration, $plugin_id, $plugin_definition); } /** * {@inheritdoc} */ public function defaultConfiguration() { return [ 'http_401' => 0, 'http_403' => 0, 'http_404' => 0, ] + parent::defaultConfiguration(); } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['prefix'] = ['#markup' => '<h5>HTTP 4XX Requests</h5>']; $form['http_401'] = [ '#type' => 'checkbox', '#title' => $this->t('Show on 401 pages'), '#default_value' => $this->configuration['http_401'], ]; $form['http_403'] = [ '#type' => 'checkbox', '#title' => $this->t('Show on 403 pages'), '#default_value' => $this->configuration['http_403'], ]; $form['http_404'] = [ '#type' => 'checkbox', '#title' => $this->t('Show on 404 pages'), '#default_value' => $this->configuration['http_404'], ]; return $form + parent::buildConfigurationForm($form, $form_state); } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { $this->configuration['http_401'] = $form_state->getValue('http_401'); $this->configuration['http_403'] = $form_state->getValue('http_403'); $this->configuration['http_404'] = $form_state->getValue('http_404'); parent::submitConfigurationForm($form, $form_state); } /** * Constructs "HTTP 4XX Requests" condition plugin. * * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. * @param array $plugin_definition * The plugin implementation definition. */ public function __construct(RequestStack $request_stack, array $configuration, $plugin_id, array $plugin_definition) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->requestStack = $request_stack; } /** * Evaluates the condition and returns TRUE or FALSE accordingly. * * @return bool * TRUE if the condition has been met, FALSE otherwise. */ public function evaluate() { $exception = $this->requestStack->getCurrentRequest()->attributes->get('exception'); if (!is_null($exception)) { $code = $exception->getStatusCode(); $value = $this->configuration["http_{$code}"]; if ($value == 1) { return TRUE; } else { return FALSE; } } else { $this->configuration["negate"] = FALSE; return TRUE; } } /** * Provides a human readable summary of the condition's configuration. */ public function summary() { if (!empty($this->configuration['negate'])) { return $this->t('Do not return true on HTTP 4XX Requests.'); } return $this->t('Return true on HTTP 4XX Requests.'); } /** * {@inheritdoc} */ public function getCacheContexts() { $contexts = parent::getCacheContexts(); $contexts[] = 'url.path'; return $contexts; } }
- Above plugin will add a new condition to the block configuration form. See added screenshot.
- Select the error page for which you want to hide the block. check negate checkbox and save the block configuration.