ksm() should show Kint output without wrapping the parameters in an $args array but instead showing the actual variable names ($native_languages in this example). Secondly, the backtrace should not include the call to ksm itself.

issue

ksm($native_languages); is nice and short but it wraps everything in the $args array which I don't want to see.

Calling Kint::dump like this shows directly the variable I want to see, including its name, but it's clumsy:

use Drupal\Core\Render\Markup;
drupal_set_message(Markup::create(@Kint::dump($native_languages)));

This helps a little but the variable name it always prints is "$variable", not the name I want:

function my_ksm($variable) {
  drupal_set_message(Markup::create(@Kint::dump($variable)));
}

I understand that ksm() can take multiple arguments. I only want one.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kari.kaariainen created an issue. See original summary.

kari.kaariainen’s picture

Issue summary: View changes
AdamPS’s picture

Great idea. I think I figured out the solution. It does what you want and there is one extra benefit: the backtrace starts with the actual line of interest rather than always showing the function ksm.


/**
 * Prints single passed arguments to the 'message' area of the page.
 */
function ksm1($arg) {
  if (\Drupal::currentUser()->hasPermission('access kint')) {
    kint_require();
    Kint::$aliases['functions']['ksm1'] = 'ksm1';
    Kint::$returnOutput = TRUE;
    $msg = @Kint::dump($arg);
    Kint::$returnOutput = FALSE;
    drupal_set_message(Markup::create($msg));
  }
}

AdamPS’s picture

AdamPS’s picture

Status: Active » Needs review
kari.kaariainen’s picture

Works brilliantly! I would perhaps call it ksm_single (same kind of idea as with the kint_lite). That would also avoid any confusion with the letter L.

AdamPS’s picture

OK, I think I have a better version. Rather than creating a new function, I have fixed the existing ksm and kint. They should now produce identical output to calling Kint::dump directly for one or multiple parameters.

Status: Needs review » Needs work

The last submitted patch, 7: devel.ksm_.2879341-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

AdamPS’s picture

Status: Needs work » Needs review
FileSize
964 bytes

Status: Needs review » Needs work

The last submitted patch, 9: devel.ksm_.2879341-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

kari.kaariainen’s picture

This works too!

AdamPS’s picture

Status: Needs work » Needs review

Unfortunately I can't understand the test failure. It doesn't really seem related to the change that I made.

Status: Needs review » Needs work

The last submitted patch, 9: devel.ksm_.2879341-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jeffschuler’s picture

This is great. Thanks! The patch works for me.

I think this is what's going on with the testbot tests:

The Kint dumper's checkRequirements() function returns the output of kint_require().

The patch changes kint_require() to no longer return the output of require_once(), which would normally be 1. kint_require() will therefore implicitly return NULL.

checkRequirements() then fails and the Kint dumper is unavailable in the Devel settings.

When the test tries to change the dumper to Kint, it cannot, so the value is stuck at "drupal_variable", therefore failing the test.

nicolaj.knudsen@gmail.com’s picture

Patch works great, thank you so much!

It seems it even (partially?) fixes https://www.drupal.org/project/devel/issues/2643392 and https://www.drupal.org/project/devel/issues/2643338, since the first line of the callstack is now referring to the line where kint() is invoked.

Beautiful!

However something happened with the layout, the text is stretched in the grey line on the buttom with the trace information, but that is absolutely a minor side effect.

AdamPS’s picture

@jeffschuler thanks for the explanation - so I'll add a return statement and hopefully that fixes it

Status: Needs review » Needs work

The last submitted patch, 16: devel.ksm_.2879341-16.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Anybody’s picture

Very nice idea! +1 for this, this will be very helpful.
The patch works for me in manual testing.

willzyx’s picture

I'm not sure that the new function is needed.. you can obtain the same result by set kint as default dumper and call kpr(). I'm missing something?

AdamPS’s picture

Title: Function like ksm() but without the unnecessary $args wrapping » ksm() should not add unnecessary wrapping
Issue summary: View changes

@willzyx Thanks for your time.

I have now fixed the issue title which had got out of date. The patch does not add a new function but instead fixes ksm to display what it ought to.

Anybody’s picture

Status: Needs work » Reviewed & tested by the community
AdamPS’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
2.08 KB

Test failure was a glitch, now working.

@willzyx
I agree that there are two functions that are mostly the same, and that's true either with or without the patch:

  1. ksm()
  2. set kint as default dumper and call kpr()

Both of them have the same bug - I have added a new screenshot from (2). Notice that it always says $input instead of the variable name and the function name in the backtrace is always Kint->export. That means that every dump looks the same until you open it, so it's hard to know which is the one that you want.

Patch #16 fixes the bug for (1). New patch #22 fixes it for both.

AdamPS’s picture

FileSize
8.1 KB

Sorry forgot the screenshot

Anybody’s picture

Thank you. I didn't see the other problem indeed. RTBC by manual testing for us.

AdamPS’s picture

Thanks @Anybody. Please can you change the status to RTBC:-)

prinds’s picture

Status: Needs review » Reviewed & tested by the community

patch #22 works for me on drupal 8.5.6 and devel 8.x-1.2

a big improvement to the ksm() functionality..

ELC’s picture

Patch re-rolled for commit #2931217-39: Replace calls to deprecated function drupal_set_message with calls to the messenger service so it will now patch 8.x-2.x without error. Patch is otherwise identical.

There are no apparent negative side effects of this and it makes the ksm output much easier to deal with, especially when using it multiple times.

AdamPS’s picture

Version: 8.x-2.x-dev » 8.x-3.x-dev
Status: Reviewed & tested by the community » Needs work

Thanks @ELC. In fact the latest branch is now 8.x-3.x which includes a major change to remove the kint module and use composer to get the kint library. The patch needs some reworking because kint() is now a wrapper to \Drupal\devel\DevelDumperManager::dump(). We need to pass through a flag to indicate that the $args should be printed separately and it will be a bit tricky to find the best way to pass this.

There is a separate issue #2712489: Exclude internal functions from backtrace that handles the Kint::$aliases part of this patch so I think we should drop that part from this issue. Please could you take a look at that one and set it to RTBC?

AdamPS’s picture

Status: Needs work » Needs review
FileSize
1.88 KB

New patch for 3.x

Anybody’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @AdamPS, works perfectly. I guess the 2.x branch will be deprecated and we don't want a backport?

AdamPS’s picture

Thanks

I guess the 2.x branch will be deprecated and we don't want a backport?

Yes that's right as far as I know

moshe weitzman’s picture

Status: Reviewed & tested by the community » Fixed

Sorry for the delay here.

  • moshe weitzman committed 98399b6 on 8.x-3.x authored by AdamPS
    Issue #2879341 by AdamPS, ELC, kari.kaariainen: ksm() should not add...
Anybody’s picture

Thank you very much moshe! Can we perhaps have a new stable release including this very helpful fix?

Status: Fixed » Closed (fixed)

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