You are here

function publication_date_node_presave in Publication Date 8

Implements hook_ENTITY_TYPE_presave().

Parameters

\Drupal\node\NodeInterface $node:

File

./publication_date.module, line 54
Add a field to nodes containing the publication date.

Code

function publication_date_node_presave(\Drupal\node\NodeInterface $node) {

  // If a publication date has already been set then retain it.
  if (!empty($node->published_at->value)) {
    $published_at = $node->published_at->value;
  }
  elseif ($node
    ->isPublished()) {
    $published_at = REQUEST_TIME;
  }
  else {
    $published_at = PUBLICATION_DATE_DEFAULT;
  }

  // Allow other modules to alter the publication date before it is saved.
  $data = [
    'published_at' => $published_at,
    'node' => $node,
    'op' => $node
      ->isNew() ? 'insert' : 'update',
  ];
  \Drupal::moduleHandler()
    ->alter('publication_date', $data);

  // Update the node object.
  $node
    ->set('published_at', $published_at);
}