You are here

function scheduler_entity_view in Scheduler 2.x

Implements hook_entity_view().

File

./scheduler.module, line 688
Scheduler publishes and unpublishes entities on dates specified by the user.

Code

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',
    ];
  }
}