public function CalendarController::updateNodePublishDate in Content Planner 8
Update creation date of a given Node.
Parameters
\Drupal\node\NodeInterface $node:
string $date:
Return value
\Zend\Diactoros\Response\JsonResponse
Throws
\Drupal\Core\Entity\EntityStorageException
1 string reference to 'CalendarController::updateNodePublishDate'
- content_calendar.routing.yml in modules/
content_calendar/ content_calendar.routing.yml - modules/content_calendar/content_calendar.routing.yml
File
- modules/
content_calendar/ src/ Controller/ CalendarController.php, line 141
Class
- CalendarController
- Class CalendarController.
Namespace
Drupal\content_calendar\ControllerCode
public function updateNodePublishDate(NodeInterface $node, $date) {
$data = [
'success' => FALSE,
'message' => NULL,
];
// Get content type config entities.
$content_type_config_entities = $this->contentTypeConfigService
->loadAllEntities();
// Check for allowed types, marked in the Content Calendar settings.
if (!array_key_exists($node
->getType(), $content_type_config_entities)) {
$data['message'] = $this
->t('Action is not allowed for Nodes of type @type', [
'@type' => $node
->getType(),
]);
return new JsonResponse($data);
}
// First – Update created on date!
// Get the Node's "created on" date
$created_on_timestamp = $node
->get('created')
->getValue();
$created_on_timestamp_value = $created_on_timestamp[0]['value'];
// Return a date object
$original_created_on_datetime = DateTimeHelper::convertUnixTimestampToDatetime($created_on_timestamp_value);
// Extract hour, minutes and seconds.
$hour = $original_created_on_datetime
->format('H');
$minutes = $original_created_on_datetime
->format('i');
$seconds = $original_created_on_datetime
->format('s');
// Create a new datetime object from the given date.
$new_created_on_datetime = \DateTime::createFromFormat('Y-m-d', $date);
// Set hour, minutes and seconds.
$new_created_on_datetime
->setTime($hour, $minutes, $seconds);
// Set created time.
$node
->set('created', $new_created_on_datetime
->getTimestamp());
// Second - Update publish on date! (only if publish on date is set)
// Get publish on timestamp.
$publish_on_timestamp = $node
->get('publish_on')
->getValue();
$publish_on_timestamp_value = $publish_on_timestamp[0]['value'];
// Only change scheduler publish on timestamp, when "publish on" is set
if ($publish_on_timestamp_value) {
// Get the Node's publish ondate and return a datetime object.
$original_publish_datetime = DateTimeHelper::convertUnixTimestampToDatetime($publish_on_timestamp_value);
// Extract hour, minutes and seconds.
$hour = $original_publish_datetime
->format('H');
$minutes = $original_publish_datetime
->format('i');
$seconds = $original_publish_datetime
->format('s');
// Create a new datetime object from the given date.
$new_publish_datetime = \DateTime::createFromFormat('Y-m-d', $date);
// Set hour, minutes and seconds.
$new_publish_datetime
->setTime($hour, $minutes, $seconds);
// Set publish on datetime.
$node
->set('publish_on', $new_publish_datetime
->getTimestamp());
// Set created on datetime.
$node
->set('created', $new_publish_datetime
->getTimestamp());
}
// Save.
if ($node
->save() == SAVED_UPDATED) {
$data['success'] = TRUE;
$data['message'] = $this
->t('The creation date for Node @id has been updated', [
'@id' => $node
->id(),
]);
}
return new JsonResponse($data);
}