Skip to content

Commit

Permalink
Merge pull request #754 from UN-OCHA/RW-920
Browse files Browse the repository at this point in the history
[RW-920] 410 for expired jobs/training and remove title on 404
  • Loading branch information
orakili authored Mar 20, 2024
2 parents 38d5790 + 7b21ed0 commit fbfdcff
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 19 deletions.
33 changes: 18 additions & 15 deletions html/modules/custom/reliefweb_entities/reliefweb_entities.module
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ use Drupal\reliefweb_entities\Entity\Report;
use Drupal\reliefweb_entities\EntityFormAlterServiceBase;
use Drupal\reliefweb_utility\Helpers\ClassHelper;
use Drupal\reliefweb_utility\Helpers\DateHelper;
use Drupal\reliefweb_utility\Helpers\EntityHelper;
use Drupal\reliefweb_utility\Helpers\MailHelper;
use Drupal\reliefweb_utility\Helpers\MediaHelper;
use Drupal\reliefweb_utility\Helpers\ReliefWebStateHelper;
use Drupal\reliefweb_utility\Helpers\TaxonomyHelper;
use Drupal\reliefweb_utility\Helpers\TextHelper;
use Drupal\taxonomy\TermInterface;
use Symfony\Component\HttpKernel\Exception\GoneHttpException;

/**
* Implements hook_entity_base_field_info().
Expand Down Expand Up @@ -755,27 +755,19 @@ function reliefweb_entities_menu_local_tasks_alter(&$data, $route_name, Refinabl
}

/**
* Implements hook_preprocess_page__404().
* Implements hook_preprocess_page__410().
*
* Add a user friendly message on inaccessible entity pages to indicate it's
* no longer available (we assume the URL was previously accessible, which is
* probably true in most cases).
*/
function reliefweb_entities_preprocess_page__404(array &$variables) {
// Get the entity from the original request.
$entity = EntityHelper::getEntityFromRequest(\Drupal::requestStack()->getMainRequest());
if (isset($entity) && $entity instanceof BundleEntityInterface) {
$type = mb_strtolower(EntityHelper::getBundleLabelFromEntity($entity));
$title = $entity->label();

$message = t('The @type %title is no longer available.', [
'@type' => $type,
'%title' => $title,
]);

function reliefweb_entities_preprocess_page__410(array &$variables) {
// @see \Drupal\reliefweb_entities\EventSubscriber\AccessDeniedToNotFound::on403().
$exception = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');
if (isset($exception) && $exception instanceof GoneHttpException) {
$variables['page']['content']['message'] = [
'#type' => 'markup',
'#markup' => $message,
'#markup' => $exception->getMessage(),
];
}

Expand All @@ -786,6 +778,17 @@ function reliefweb_entities_preprocess_page__404(array &$variables) {
$variables['#cache']['contexts'][] = 'url.path';
}

/**
* Implements hook_preprocess_page_title().
*/
function reliefweb_entities_4xx_title() {
$exception = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');
if (isset($exception) && $exception instanceof GoneHttpException) {
return t('Gone');
}
return 'Client error';
}

/**
* Implements hook_ENTITY_TYPE_delete() for 'media'.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

namespace Drupal\reliefweb_entities\EventSubscriber;

use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
use Drupal\reliefweb_entities\BundleEntityInterface;
use Drupal\Core\Http\Exception\CacheableGoneHttpException;
use Drupal\Core\Http\Exception\CacheableNotFoundHttpException;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\reliefweb_moderation\EntityModeratedInterface;
use Drupal\reliefweb_utility\Helpers\EntityHelper;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Redirects 403 page error responses to 404 page for bundle entities.
*/
class AccessDeniedToNotFound extends HttpExceptionSubscriberBase {

use StringTranslationTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -35,8 +41,31 @@ protected function getHandledFormats() {
*/
public function on403(ExceptionEvent $event) {
$entity = EntityHelper::getEntityFromRequest($event->getRequest());
if (isset($entity) && $entity instanceof BundleEntityInterface) {
$event->setThrowable(new NotFoundHttpException());
if (isset($entity) && $entity instanceof EntityModeratedInterface) {
$cacheable_metadata = new CacheableMetadata();

if ($entity instanceof CacheableDependencyInterface) {
$cacheable_metadata->addCacheableDependency($entity);
}

$throwable = $event->getThrowable();
if (isset($throwable) && $throwable instanceof CacheableDependencyInterface) {
$cacheable_metadata->addCacheableDependency($throwable);
}

if ($entity->getModerationStatus() === 'expired') {
$message = $this->t('The @bundle %title is no longer available.', [
'@bundle' => $entity->bundle(),
'%title' => $entity->label(),
]);

$exception = new CacheableGoneHttpException($cacheable_metadata, $message);
}
else {
$exception = new CacheableNotFoundHttpException($cacheable_metadata);
}

$event->setThrowable($exception);
}
}

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ protected function alterRoutes(RouteCollection $collection) {
$route->setOption('_admin_route', TRUE);
}
}

// Add a title callback for the 4xx errors so it's not always Client error.
if ($route = $collection->get('system.4xx')) {
$route->setDefault('_title_callback', 'reliefweb_entities_4xx_title');
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{% embed '@common_design/layout/page.html.twig' %}

{% block header %}
{% include '@common_design_subtheme/cd/cd-header/cd-header.html.twig' %}
{% endblock %}

{% block main %}

{{ attach_library('common_design_subtheme/rw-useful-links') }}

{# Link to skip to the main content is in html.html.twig #}
<main role="main" id="main-content" class="cd-container">

<div class="cd-layout">

<div class="cd-layout__content">

<section class="rw-410">
<header>
<h1 class="rw-page-title">{{ '410 - Gone'|t }}</h1>
</header>
{% if page.content.message is not empty %}
<p><strong>{{ page.content.message }}</strong></p>
{% endif %}
<p>{{ 'Sorry for any inconvenience.'|t }}</p>
<p>{{ 'Here are some useful pages to help you get back on track:'|t }}</p>
<div class="rw-entity-useful-links">
<ul>
<li>
<a href="/">{{ 'Homepage'|t }}</a>
<p>{{ 'View ongoing crises and latest headlines.'|t }}</p>
</li>
<li>
<a href="/updates">{{ 'Updates'|t }}</a>
<p>{{ 'Access the latest humanitarian reports, maps and infographics and an archive of more than 20 years of humanitarian information.'|t }}</p>
</li>
<li>
<a href="/jobs">{{ 'Jobs'|t }}</a>
<p>{{ 'Browse open job opportunities in the humanitarian field.'|t }}</p>
</li>
<li>
<a href="/training">{{ 'Training'|t }}</a>
<p>{{ 'Discover training opportunities in the humanitarian field.'|t }}</p>
</li>
<li>
<a href="/help">{{ 'Help Section'|t }}</a>
<p>{{ 'Find help on how to use the site, read terms and conditions, view the FAQs and API documentation.'|t }}</p>
</li>
</ul>
</div>
</section>

</div>{# /.cd-layout__content #}

</div>

</main>
{% endblock %}

{% block footer_soft %}
{% include '@common_design_subtheme/cd/cd-footer/cd-soft-footer.html.twig' %}
{% endblock %}

{% block footer %}
{% include '@common_design_subtheme/cd/cd-footer/cd-footer.html.twig' %}
{% endblock %}

{% endembed %}



0 comments on commit fbfdcff

Please sign in to comment.