forked from ueberbit/publication_date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublication_date.module
executable file
·135 lines (122 loc) · 4.73 KB
/
publication_date.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* @file
* Add a field to nodes containing the publication date.
*/
use Drupal\Core\Field\BaseFieldDefinition;
/**
* 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'] = 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,
))
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', array(
'type' => 'publication_date_timestamp',
'weight' => 10,
))
->setDisplayConfigurable('form', TRUE);
}
return $fields;
}
/**
* 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->bundle() . ' published on date');
$form['published_at']['#group'] = 'revision_information';
}
/*
// Use the popup date picker provided by the Date module, if it is enabled and
// the user has access to edit the publication date.
if ($pubdate_access && \Drupal::moduleHandler()->moduleExists('date_popup') && \Drupal::config('publication_date')->get('popup_enable')) {
// The date popup field requires a date format without the timezone.
$date_format = 'Y-m-d H:i:s';
$form['options']['pubdate'] = array(
'#type' => 'date_popup',
'#title' => t('Published on'),
'#description' => t('Leave blank to use the time of form submission.'),
'#date_type' => DATE_UNIX,
'#date_timezone' => date_default_timezone(),
'#date_format' => $date_format,
'#date_increment' => 1,
'#date_year_range' => '-' . \Drupal::config('publication_date')->get('year_start') . ':+' . \Drupal::config('publication_date')->get('date_popup_year_end'),
'#weight' => -1,
'#access' => TRUE,
);
}
// Falback to a standard text field.
else {
$form['options']['pubdate'] = array(
'#type' => 'textfield',
'#title' => t('Published on'),
'#maxlength' => 25,
'#description' => t('Format: %time. Leave blank to use the time of form submission.', array('%time' => format_date(REQUEST_TIME, 'custom', $date_format))),
'#weight' => -1,
'#access' => $pubdate_access,
);
}
// If there is an existing publication date, set it as the default value.
$form['options']['pubdate']['#default_value'] = empty($published_at) ? '' : format_date($published_at, 'custom', $date_format);
// If the user can access pubdate, we need to make sure they also have access
// to the options group.
if ($pubdate_access && $form['options']['#access'] == FALSE) {
$form['options']['#access'] = TRUE;
// Check all fields in the options group and, if access has not been set,
// set it to FALSE. We don't want to grant access to any extra fields.
$children = \Drupal\Core\Render\Element::children($form['options']);
foreach ($children as $key => $value) {
if (!isset($form['options'][$value]['#access'])){
$form['options'][$value]['#access'] = FALSE;
}
}
}
*/
}
/**
* 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_field_formatter_info_alter().
*/
function publication_date_field_formatter_info_alter(array &$info) {
$info['timestamp']['field_types'][] = 'published_at';
$info['timestamp_ago']['field_types'][] = 'published_at';
}