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;
  }

}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Marty2081 created an issue. See original summary.

boshtian’s picture

Thank you Marty2081 for this. Is it possible you create a patch and upload it to the issue?

Chris Charlton’s picture

+1 to this set. :)

tetranz’s picture

This seems useful so I took it and made it more generic.

Screenshot

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.

tetranz’s picture

Title: Condition plugin to respond to response exceptions (eg 403/404) » Condition plugin to respond to OK (200) and exception responses (403/404)
Chris Charlton’s picture

Nice changes.

strozx’s picture

Status: Needs review » Reviewed & tested by the community

I have tested the code and it works so I'm moving this to Reviewed.

  • boshtian committed c9d1245 on 8.x-4.x authored by tetranz
    Issue #2989464 by tetranz, Marty2081, strozx: Condition plugin to...
boshtian’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.