0

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();
     }
   }
 }

  • Hi John and welcome! If I get you right, you want to create a new ScheduledUpdate entity of type Promote Event and set the node->promote_event field to point to the new entity. Is my understanding correct? – Stefanos Petrakis Nov 30 '19 at 22:56
  • Yes! I believe you are right. It's different than the way I was thinking about it, but yes, you are correct. – John Nemec Dec 2 '19 at 0:05
0

Here is one example of how to add a ScheduledUpdate to your 'host' entity, in your case a node.

$scheduled_update = ScheduledUpdate::create([
  'type' => 'node__sticky',
  'status' => ScheduledUpdateInterface::STATUS_UNRUN,
  'sticky' => NodeInterface::STICKY,
  'update_timestamp' => $timestamp,
  'entity_ids' => [$eventId],
]);
$scheduled_update->save();
$node->promote_event = $scheduled_update->id();
$node->save();

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.