publication_date.module in Publication Date 8.2
Add a field to nodes containing the publication date.
File
publication_date.module
View source
<?php
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
define('PUBLICATION_DATE_DEFAULT', 2147483647);
function publication_date_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type
->id() == 'node') {
$fields['published_at'] = BaseFieldDefinition::create('published_at')
->setLabel(t('Published on'))
->setDescription(t('Keep the publication timestamp for each node.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'region' => 'hidden',
))
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', array(
'type' => 'publication_date_timestamp',
'weight' => 10,
))
->setDisplayConfigurable('form', TRUE);
}
return $fields;
}
function publication_date_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$account = \Drupal::currentUser();
$node = $form_state
->getFormObject()
->getEntity();
if (isset($form['published_at'])) {
$form['published_at']['#access'] = $account
->hasPermission('set any published on date') || $account
->hasPermission('set ' . $node
->bundle() . ' published on date');
$form['published_at']['#group'] = 'revision_information';
}
}
function publication_date_clone_node_alter(&$node, $context) {
$node->published_at->value = NULL;
}
function publication_date_field_formatter_info_alter(array &$info) {
$info['timestamp']['field_types'][] = 'published_at';
$info['timestamp_ago']['field_types'][] = 'published_at';
}
Functions
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… |