Created
August 12, 2022 04:29
-
-
Save gheydon/d18bceefd182f1136daef0788116d3a1 to your computer and use it in GitHub Desktop.
Condition to allow showing only on a 403/404 page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\example\Plugin\Condition; | |
use Drupal\Core\Condition\ConditionPluginBase; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Http\RequestStack; | |
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Condition to check for 403/403 page. | |
* | |
* @Condition( | |
* id = "error_pages", | |
* label = @Translation("Error pages") | |
* ) | |
*/ | |
class ErrorPages extends ConditionPluginBase implements ContainerFactoryPluginInterface { | |
/** | |
* Request Stack service. | |
* | |
* @var \Drupal\Core\Http\RequestStack | |
*/ | |
protected RequestStack $requestStack; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request_stack) { | |
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( | |
$configuration, | |
$plugin_id, | |
$plugin_definition, | |
$container->get('request_stack') | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
$form['error_pages'] = [ | |
'#type' => 'checkboxes', | |
'#title' => $this->t('Error pages'), | |
'#options' => [ | |
'403' => $this->t('403 (access denied) page'), | |
'404' => $this->t('404 (not found) page'), | |
], | |
'#default_value' => $this->configuration['error_pages'], | |
]; | |
return parent::buildConfigurationForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
$this->configuration['error_pages'] = array_filter($form_state->getValue('error_pages')); | |
parent::submitConfigurationForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function evaluate() { | |
// Returns true if no bundles are selected and negate option is disabled. | |
if (empty($this->configuration['error_pages']) && !$this->isNegated()) { | |
return TRUE; | |
} | |
$request = $this->requestStack->getCurrentRequest(); | |
$parameters = $request->isMethod('GET') ? $request->query : $request->request; | |
if ($parameters->has('_exception_statuscode') && in_array($parameters->get('_exception_statuscode'), $this->configuration['error_pages'])) { | |
return TRUE; | |
} | |
return FALSE; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function summary() { | |
if (count($this->configuration['error_pages']) > 1) { | |
$error_pages = $this->configuration['error_pages']; | |
$last = array_pop($error_pages); | |
$error_pages = implode(', ', $error_pages); | |
return $this->t('The error page is @error_pages or @last', ['@error_pages' => $error_pages, '@last' => $last]); | |
} | |
$bundle = reset($this->configuration['bundles']); | |
return $this->t('The error page is @error_pages', ['@error_pages' => $bundle]); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function defaultConfiguration() { | |
return ['error_pages' => []] + parent::defaultConfiguration(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment