Problem/Motivation

We encountered an edge case which might be worth mitigating:

Customer website has banners as node content, which can be shown site-wide or on specific pages. Since we're showing the full banner, the display mode used was full. The banners can also be scheduled. When customer created a site-wide banner with scheduled unpublishing, all site content had the header unavailable_after

Steps to reproduce

  1. Have an entity of schedulable type (e.g. node, media, taxonomy) with scheduled unpublishing
  2. Embed or otherwise show that entity in another context with display mode named full
  3. That context (e.g. another node, view page) will have the header unavailable_after

Proposed resolution

From scheduler.module:

/**
 * Implements hook_entity_view().
 */
function scheduler_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, string $view_mode) {
  // If the entity is going to be unpublished, then add this information to the
  // http header for search engines. Only do this when the current page is the
  // full-page view of the entity.
  // @see https://googleblog.blogspot.be/2007/07/robots-exclusion-protocol-now-with-even.html
  if ($view_mode == 'full' && isset($entity->unpublish_on->value)) {
    $unavailable_after = date(DATE_RFC850, $entity->unpublish_on->value);
    $build['#attached']['http_header'][] = ['X-Robots-Tag', 'unavailable_after: ' . $unavailable_after];

    // Also add the information as a meta tag in the html head section.
    $unavailable_meta_tag = [
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'robots',
        'content' => 'unavailable_after: ' . $unavailable_after,
      ],
    ];
    // Any value seems to be OK for the second item, but it must not be omitted.
    $build['#attached']['html_head'][] = [$unavailable_meta_tag, 'robots_unavailable_date'];
  }
}

This hook could check that we are in the canonical URL of the entity with scheduled unpublishing, and only then alter the HTTP and HTML headers?

Comments

veikkoa created an issue.