diff --git a/publication_date.module b/publication_date.module index 7546a24..9404702 100755 --- a/publication_date.module +++ b/publication_date.module @@ -5,6 +5,8 @@ * 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 @@ -26,7 +28,7 @@ function publication_date_entity_base_field_info(\Drupal\Core\Entity\EntityTypeI $fields = []; if ($entity_type->id() == 'node') { - $fields['published_at'] = \Drupal\Core\Field\BaseFieldDefinition::create('published_at') + $fields['published_at'] = BaseFieldDefinition::create('published_at') ->setLabel(t('Published on')) ->setDescription(t('Keep the publication timestamp for each node.')) ->setRevisionable(TRUE) @@ -36,6 +38,7 @@ function publication_date_entity_base_field_info(\Drupal\Core\Entity\EntityTypeI 'type' => 'timestamp', 'weight' => 0, )) + ->setDisplayConfigurable('view', TRUE) ->setDisplayOptions('form', array( 'type' => 'datetime_timestamp', 'weight' => 10, @@ -47,39 +50,6 @@ function publication_date_entity_base_field_info(\Drupal\Core\Entity\EntityTypeI } /** - * 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; - } - // Otherwise, if no publication date has been set and the node is published - // then set the publication date to REQUEST_TIME. - elseif ($node->isPublished()) { - $published_at = REQUEST_TIME; - } - // Otherwise, if no publication date has been set and the node is unpublished - // then store the default publication date. - 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. @@ -108,17 +78,9 @@ function publication_date_clone_node_alter(&$node, $context) { } /** - * Implements hook_menu(). + * Implements hook_field_formatter_info_alter(). */ -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; +function publication_date_field_formatter_info_alter(array &$info) { + $info['timestamp']['field_types'][] = 'published_at'; + $info['timestamp_ago']['field_types'][] = 'published_at'; } diff --git a/src/Plugin/Field/FieldType/PublicationDateItem.php b/src/Plugin/Field/FieldType/PublicationDateItem.php index 5a0a919..ce3d5e9 100644 --- a/src/Plugin/Field/FieldType/PublicationDateItem.php +++ b/src/Plugin/Field/FieldType/PublicationDateItem.php @@ -84,10 +84,7 @@ class PublicationDateItem extends ChangedItem { * @inheritDoc */ public function isEmpty() { - if (isset($this->value) || isset($this->published_at_or_now)) { - return FALSE; - } - return TRUE; + return FALSE; } }