1

I'm trying to write a custom RulesAction to perform an operation after an order status is updated. Basically, I want to perform this operation after checkout is complete (but this event doesn't exist anymore in D8).

namespace Drupal\my_module\Plugin\RulesAction;

use Drupal\uc_order\OrderInterface;
use Drupal\uc_order\OrderStatusInterface;
use Drupal\rules\Core\RulesActionBase;

/**
 * Definition of the action performed.
 *
 * @RulesAction(
 *   id = "my_action_id",
 *   label = @Translation("My action label"),
 *   category = @Translation("My module"),
 *   context = {
 *     "entity" = @ContextDefinition("entity",
 *       label = @Translation("Entity"),
 *       description = @Translation("Specifies the entity, which should be saved permanently.")
 *     )
 *   }
 * )
 */
class MyActionClass extends RulesActionBase {

  /**
   * Def.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *    The order update triggered by rules.
   */
  protected function doExecute(OrderInterface $order) {
    // @TODO.

  }

}

My action do come in rules, I have set it up after the event: After updating order (rules_entity_update:uc_order)

When triggering it I get the error : If I set the context to entity (I've looked and no context is defined by ubercart for order):

The website encountered an unexpected error. Please try again later.Drupal\Core\Entity\EntityStorageException: Required context entity is missing for plugin my_action_id. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 783 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

If no context is set :

The website encountered an unexpected error. Please try again later. TypeError: Argument 1 passed to Drupal\my_module\Plugin\RulesAction\MyActionClass::doExecute() must implement interface Drupal\uc_order\OrderInterface, none given in Drupal\my_module\Plugin\RulesAction\MyActionClass->doExecute()

So my questions are :

  • Do I need a context for rules action to work ?
  • If yes, how can I find it and get it working ?

Here are the documentations I find (but rules is unfornately not very well documented for now):

Rules documentation for dev | RulesAction examples | Context documentation

| improve this question | | | | |
  • I don't understand the premise of the question. You say: > I'm trying to write a custom RulesAction to perform an operation after an order status is updated. Basically, I want to perform this operation after checkout is complete (but this event doesn't exist anymore in D8). But Ubercart DOES have Rules events for "Customer completes checkout" AND for "Order status gets updated", and has had those events present for a year before you posted this question. Ubercart also provides many Rules actions. The best way to learn how to write your own action to use with Ubercart is to start with one of th – anonymous Jun 22 '19 at 22:15
1

So the context is mandatory and it is actually important because it sets the data you want to fetch when the rule is triggered.

I just needed to set the context to the good entity as follow.

"entity" = @ContextDefinition("entity:uc_order",

The working annotation is the following.

/**
 * Definition of the action performed.
 *
 * @RulesAction(
 *   id = "my_action_id",
 *   label = @Translation("My action label"),
 *   category = @Translation("My module"),
 *   context = {
 *     "entity" = @ContextDefinition("entity:uc_order",
 *       label = @Translation("Entity"),
 *       description = @Translation("Specifies the entity, which should be saved permanently.")
 *     )
 *   }
 * )
 */

Then you are able to fetch the current uc_order in the administrative page.

| improve this answer | | | | |

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.