Problem/Motivation
When the Devel module is enabled and Debug Mode is checked in the settings of an expression field, evaluating the expression ends in a fatal error:
Error: Call to undefined method Drupal\Core\Logger\LoggerChannel::dump()
This happens on entity save, and also on every render when the formatter's Always Evaluate Expression? setting is enabled. The entity cannot be saved while debug mode is on.
evaluateExpression() in both ExpressionItem (line 180) and ExpressionItemTrait (line 164) calls:
\Drupal::logger('field_token_expression_debug')->dump($debug);
LoggerChannel implements the PSR-3 LoggerInterface and has no dump() method.
Up to and including 2.0.2, these lines called dpm($debug, 'Field Token Expression Debug Output'). The call was changed in the Drupal 11 upgrade commit (67d5f37, taken from 8.x-1.x), so 2.0.3 and 2.1.0 are affected. 8.x-1.x contains the same code, but that series is no longer supported.
The problem was spotted while working on the coding standards cleanup in #2915427, and was left out of it because it is a behavior change.
Steps to reproduce
- Install and enable Devel.
- Add an Expression (integer) field to a content type, use
2 + 2as the expression, and check Debug Mode. - Create a node of that type and save it.
- The save fails with the error above.
The legacy field_expression field type fails the same way, through ExpressionItem.
Proposed resolution
Restore the original on-screen output through Devel's dumper service, which is what dpm() wraps, in both files:
\Drupal::service('devel.dumper')->message($debug, 'Field Token Expression Debug Output');
Also remove the misleading // Use logger()->dump() for debugging. comment above it.
This matches what the Debug Mode setting promises ("debug messages will be output with some feedback about the expression"). The call is already guarded by moduleExists('devel'), so the service is always available when it runs. Calling the service directly instead of dpm() also avoids the DrupalPractice "The use of function dpm() is discouraged" warning reported in #2915427.
Alternative considered: logging through the existing channel with ->debug() and print_r($debug, TRUE). That would remove the dependency on Devel, but debug mode is only offered when Devel is enabled, and the output would go to the log instead of the screen, which no longer matches the setting description.
Remaining tasks
- Merge request with the fix in
ExpressionItemandExpressionItemTrait. - Manual test with Devel enabled, for both the 2.x numeric field types and the legacy
field_expressiontype. - Review.
User interface changes
With debug mode on, the original expression, the token-replaced expression and the result are shown as a status message again, as before 2.0.3.
API changes
None.
Data model changes
None.
Issue fork field_expression-3629878
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
orkutmuratyilmaz