You are here

PageNotFoundRequest.php in Block In Page Not Found 8.3

Same filename and directory in other branches
  1. 8.4 src/Plugin/Condition/PageNotFoundRequest.php

File

src/Plugin/Condition/PageNotFoundRequest.php
View source
<?php

namespace Drupal\block_in_page_not_found\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 a 'Page Not Found' condition.
 *
 * @Condition(
 *   id = "page_not_found_request",
 *   label = @Translation("Page not found"),
 * )
 */

/**
 * Class PageNotFoundRequest.
 *
 * @package Drupal\block_in_page_not_found\Plugin\Condition
 */
class PageNotFoundRequest 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 [
      'page_not_found' => '',
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['prefix'] = [
      '#markup' => '<h5>Page Not Found</h5>',
    ];
    $form['page_not_found'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show in page not found'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['page_not_found'] = $form_state
      ->getValue('page_not_found');
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * Constructs a Page not found 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() {

    // Get the configuration of the page not found.
    $page_not_found_checked = $this->configuration['page_not_found'];

    // If show "in page not found" is not checked
    // or "show in page not found" was passed from checked to unchecked.
    if (!$page_not_found_checked || $page_not_found_checked == 0) {
      return FALSE;
    }

    // If show "in page not found" is not checked.
    if ($page_not_found_checked == 1) {
      $status = $this->requestStack
        ->getCurrentRequest()->attributes
        ->get('exception');
      if ($status && $status
        ->getStatusCode() == 404) {
        return TRUE;
      }
      else {
        return FALSE;
      }
    }
  }

  /**
   * 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 the following page not found.');
    }
    return $this
      ->t('Return true on the following page not found.');
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    $contexts = parent::getCacheContexts();
    $contexts[] = 'url.path';
    return $contexts;
  }

}

Classes

Namesort descending Description
PageNotFoundRequest Class PageNotFoundRequest.