I’m attempting to populate an entity referenced field created by the Scheduled Updates module within a content type, Event. Within Event I have two Scheduled Updates fields to check and uncheck the Sticky at top of list option.
I also have a product, Promote Event, in which the user is required to add a Begin Promoting Date, an Entity Reference to an Event, and a Quantity. Once a user purchases the Promote Event, I’d like to populate the Scheduled Updates fields within the Event content type with the data used in the purchase.
I can’t seem to figure out how to programmatically populate the Scheduled Update fields within the Event content type after the purchase of Promote Event.
Within my custom module I have:
public function orderCompleteHandler(WorkflowTransitionEvent $event) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $event->getEntity();
// Order items in the cart.
$items = $order->getItems();
if($order->bundle() == 'promoted_event'){
$beginPromo = $items[0]->get('field_begin_promotion')->getValue();
$beginPromo = $beginPromo[0]['value'];
$quantity = $items[0]->get('quantity')->getValue();
$quantity = str_replace('.00', '', $quantity[0]['value']);
$eventId = $items[0]->get('field_event_to_promote')->getValue();
$eventId = $eventId[0]['target_id'];
$endPromo = date('Y-m-d', strtotime($beginPromo. ' + '. $quantity .' days'));
$node = Node::load($eventId);
// Get the promote_event Entity Reference
$promoteEvent = $node->get('promote_event')->first();
// Get unPromoteEvent
$unPromoteEvent = $node->get('unpromote_event_')->first();
if($promoteEvent){
} else {
$dateTime = new DrupalDateTime($beginPromo, 'UTC');
$timestamp = $dateTime->getTimestamp();
$node->promote_event[] = [
'user_id' => 1,
'type' => 'node__sticky',
'update_timestamp' => $timestamp
];
$node->save();
}
}
}