publication_date.module in Publication Date 8
Same filename and directory in other branches
Add a field to nodes containing the publication date.
File
publication_date.moduleView source
<?php
/**
* @file
* Add a field to nodes containing the publication date.
*/
/**
* Define the value stored in the database when a node is unpublished and no
* publication date has been set. We use the largest number that the database
* field can hold so unpublished nodes will appear newer than published nodes
* when sorted by publication date.
*
* @note: This is going to trigger the Year 2038 problem.
*/
define('PUBLICATION_DATE_DEFAULT', 2147483647);
/**
* Implements hook_entity_base_field_info().
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
*
* @return array
*/
function publication_date_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type
->id() == 'node') {
$fields['published_at'] = \Drupal\Core\Field\BaseFieldDefinition::create('published_at')
->setLabel(t('Published on'))
->setDescription(t('Keep the publication timestamp for each node.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
))
->setDisplayOptions('form', array(
'type' => 'datetime_timestamp',
'weight' => 10,
))
->setDisplayConfigurable('form', TRUE);
}
return $fields;
}
/**
* Implements hook_ENTITY_TYPE_presave().
*
* @param \Drupal\node\NodeInterface $node
*/
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);
}
/**
* Implements hook_form_BASE_ID_alter().
*
* Display the publication date on the node edit form.
* @note: This won't work where you have Display Suite/REL enabled.
*/
function publication_date_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$account = \Drupal::currentUser();
$node = $form_state
->getFormObject()
->getEntity();
if (isset($form['published_at'])) {
// Check if the user has permission to edit the publication date.
$form['published_at']['#access'] = $account
->hasPermission('set any published on date') || $account
->hasPermission('set ' . $node->type . ' published on date');
$form['published_at']['#group'] = 'revision_information';
}
}
/**
* Implements hook_clone_node_alter().
*
* Reset the publication date when a node is cloned using the Node Clone module.
*
* @see clone.api.php
*/
function publication_date_clone_node_alter(&$node, $context) {
$node->published_at->value = NULL;
}
/**
* Implements hook_menu().
*/
function publication_date_menu() {
$items['admin/config/content/publication-date'] = array(
'title' => 'Publication date',
'description' => 'Configure publication date settings when using the date popup module.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'publication_date_admin_form',
),
'access arguments' => array(
'administer publication date',
),
'file' => 'includes/publication_date.admin.inc',
);
return $items;
}
Functions
Name | Description |
---|---|
publication_date_clone_node_alter | Implements hook_clone_node_alter(). |
publication_date_entity_base_field_info | Implements hook_entity_base_field_info(). |
publication_date_form_node_form_alter | Implements hook_form_BASE_ID_alter(). |
publication_date_menu | Implements hook_menu(). |
publication_date_node_presave | Implements hook_ENTITY_TYPE_presave(). |
Constants
Name | Description |
---|---|
PUBLICATION_DATE_DEFAULT | Define the value stored in the database when a node is unpublished and no publication date has been set. We use the largest number that the database field can hold so unpublished nodes will appear newer than published nodes when sorted by publication… |