Site tokens are added into 3.x-dev based on this issue: https://www.drupal.org/project/rules/issues/2932325

But when I tried to used the token {{@rules.site_context:site.url}} in email message or site message as part of Action after a content item is updated, I get WSOD. Below are the error message and call stack.

It appears the ExecutionState only has node and node_unchanged so the getVariable call failed.

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\EntityStorageException: Unable to get variable '@rules'; it is not defined. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 846 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Drupal\rules\Plugin\RulesDataProcessor\TokenProcessor->process('{{@rules.site_context:site.url}}', Object) (Line: 320)
Drupal\rules\Plugin\RulesExpression\ActionExpression->processValue('{{@rules.site_context:site.url}}', Array, Object) (Line: 297)
Drupal\rules\Plugin\RulesExpression\ActionExpression->processData(Object, Object) (Line: 83)
Drupal\rules\Plugin\RulesExpression\ActionExpression->prepareContext(Object, Object) (Line: 110)
Drupal\rules\Plugin\RulesExpression\ActionExpression->executeWithState(Object) (Line: 33)
Drupal\rules\Plugin\RulesExpression\ActionSetExpression->executeWithState(Object) (Line: 121)
Drupal\rules\Plugin\RulesExpression\RuleExpression->executeWithState(Object) (Line: 33)
Drupal\rules\Plugin\RulesExpression\ActionSetExpression->executeWithState(Object) (Line: 147)
Drupal\rules\EventSubscriber\GenericEventSubscriber->onRulesEvent(Object, 'rules_entity_update:node', Object)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Kevin W created an issue. See original summary.

TR’s picture

That's correct. The global variables don't seem to work properly in tokens currently, and haven't for some time. I think the fault may lie in the typed_data module's data_fetcher or placeholder_resolver service, but I haven't had time to track it down.

I could use some help tracking down this problem and fixing it - it's very low on my priority list at this time.

dcam’s picture

Status: Active » Needs review
FileSize
1.54 KB

It took me a while, but I tracked down what the problem is. @TR is correct that it's in the placeholder_resolver service. PlaceholderResolver::scan() isn't set up to handle global context variables since they contain a service name. scan() breaks them up into chunks based on the first '.' character that it finds. That's usually part of the service name, so the resulting variable name is incorrect.

I'm going to submit an issue for that over at Typed Data, but before I figured all of that out I wrote a new test for Rules which checks to see if a global context variable token is replaced correctly. There was no test coverage for that before now. Even though the issue isn't in Rules it seems like a waste to just throw that test away, so I'm submitting it here. It won't pass until Typed Data is fixed, but it should afterward.

Status: Needs review » Needs work

The last submitted patch, 3: 3130002-3-test-only.patch, failed testing. View results

TR’s picture

Nice! It would be great to have a test for this in Rules.

I'll commit it after TypedData is fixed. I look forward to seeing your patch over there.

TR’s picture

Status: Needs work » Needs review

  • TR committed 707635d on 8.x-3.x authored by dcam
    Issue #3130002 by dcam: Could not use Site token {{@rules.site_context:...
TR’s picture

Test now passes. Committed. This should help to ensure that global context variables continue to work from here on out.

While working on #3138053: PlaceholderResolver::scan() does not handle global context variables, I wrote a test Rule that reacts on new content being added and displays a message with the values of ALL the global context variables defined by Rules. You may import the attached .yml and use it on your own site. This demonstrates that global context variables now work, and it shows you the syntax and the expected content of each of them.

I will be putting this example into the rules_example module in the next few days.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.