Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
In a recent project we had the need to be able to respond to response exceptions (404 for instance) to not show a block. We created a custom condition plugin "ExceptionResponse" that evaluates to TRUE or FALSE based on the response. Maybe this is useful for others or could it be an addition to the Context module?
<?php
namespace Drupal\example\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Provides an 'Exception response' condition.
*
* @Condition(
* id = "exception_response",
* label = @Translation("Exception response"),
* )
*/
class ExceptionResponse extends ConditionPluginBase implements ContainerFactoryPluginInterface {
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a ExceptionResponse 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;
}
/**
* {@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 summary() {
if (!empty($this->configuration['negate'])) {
return $this->t('Do not return true on exception responses');
}
return $this->t('Return true on exception responses');
}
/**
* {@inheritdoc}
*/
public function evaluate() {
// Check if the current request results in an exception response.
return ($this->requestStack->getCurrentRequest()->attributes->get('exception')) ? TRUE : FALSE;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
$contexts[] = 'url.path';
return $contexts;
}
}
Comment | File | Size | Author |
---|---|---|---|
#4 | Screen Shot 2019-09-17 at 8.54.26 PM.png | 20.62 KB | tetranz |
#4 | status-code-condition-2989464-4.patch | 4.15 KB | tetranz |
Comments
Comment #2
boshtian CreditAttribution: boshtian at Agiledrop - Your Trusted Drupal Teammates commentedThank you Marty2081 for this. Is it possible you create a patch and upload it to the issue?
Comment #3
Chris Charlton+1 to this set. :)
Comment #4
tetranz CreditAttribution: tetranz at Third and Grove commentedThis seems useful so I took it and made it more generic.
I modeled the logic in the node types condition.
I think 200, 403 and 404 are the only codes that make sense here but easy to add more if there are others.
Comment #5
tetranz CreditAttribution: tetranz at Third and Grove commentedComment #6
Chris CharltonNice changes.
Comment #7
strozx CreditAttribution: strozx at Agiledrop - Your Trusted Drupal Teammates commentedI have tested the code and it works so I'm moving this to Reviewed.
Comment #9
boshtian CreditAttribution: boshtian at Agiledrop - Your Trusted Drupal Teammates commented