I'm using the following code in my custom formatter:

$result = '';
foreach ($variables['#items'] as $delta => $item)
{
    if ($item['access']) {
        $nid = $item['nid'];

        $url = url(drupal_get_path_alias('node/' . $nid), array('absolute' => TRUE));

        $entity_type = 'node';
        $bundle = 'page';
        $nids = array($nid);

        $field_values = db_select('field_revision_field_optional', 'f')
          ->fields('f', array('entity_id', 'field_optional_value'))
          ->condition('entity_type', $entity_type, '=')
          ->condition('bundle', $bundle, '=')
          ->condition('entity_id', $nids, 'IN')
          ->condition('deleted', 0, '=')
          ->execute()
          ->fetchAllKeyed();

        $text = $field_values[$nid];
        $result .= '<a href="' . $url . '">' . $text . '</a>, ';
    }
}

return substr($result, 0, -2);

With having devel's logging mysql-queries enabled I get the following error (its number of duplicates depends on number of values printed by formatter):
Notice: Undefined index: args в функции DatabaseLog->findCaller() (строка 156 в файле /home/u9501/domains/progprofi.ru/includes/database/log.inc).

I don't know whose problem it is: mine, yours or devel module's?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Deciphered’s picture

Title: Error with devel's logging mysql-queries enabled » Undefined index: args in DatabaseLog->findCaller()
Project: Custom Formatters » Drupal core
Version: 7.x-2.x-dev » 7.x-dev
Component: Code » database system
Category: Support request » Bug report
Issue summary: View changes

Actually, it appears that the issue isn't the formatters, Custom Formatters or Devels fault. It looks to be an issue in Drupal itself.

The easiest way to reproduce the issue requires the Devel module:

  1. Install standard Drupal profile.
  2. Install and enable Devel module.
  3. Turn on Devel's query log (devel/settings)
  4. Execute the following code snippet from Devels execute PHP callback (devel/php):
    db_select('node', 'n')->fields('n')->execute();

The issue is that there is no check if args is set on the next stack, it's just assumed it always is, but in the case of eval() it isn't.

Deciphered’s picture

Status: Active » Needs review
FileSize
582 bytes

Tested and issue does not seem to be present in Drupal 8.

Patch for Drupal 7 attached.

realityloop’s picture

Status: Needs review » Reviewed & tested by the community

Using the steps to reproduce in #1 I was able to confirm the issue, and the patch in #2 resolved it.

This patch is very straightforward and follows the same logic that is already used for other variables within this part of the code. RTBC

David_Rothstein’s picture

Version: 7.x-dev » 8.0.x-dev
Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs backport to D7

The code in Drupal 8 looks extremely similar - are we really sure this doesn't need to be fixed there? (see Log::findCaller)

Not sure if a test makes sense here or not, but that would be one way to ensure it's fixed in Drupal 8 :)

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

daffie’s picture

Status: Needs review » Needs work

The code test db_select('node', 'n')->fields('n')->execute(); with logging enabled is not generating the error. Calling the method fields() with one parameter just get you all the fields from the node table.
I agree with with David Rothstein that it should properly be fixed in Drupal 8. If someone else has an idea for a test, please say so!

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Gribnif’s picture

I can still duplicate this bug in D8. The easiest way to do so is to run this code within /devel/php:

function foo() {
  Drupal\Core\Database\Database::getConnection()->query('SELECT * FROM users')->execute();
}
foo();

Basically, it happens whenever a function that is part of an eval() runs a database query. The same thing can be triggered when an entity uses the PHP Code text formatter in one of its fields.

Attached is a patch that fixes the issue.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Jorrit’s picture

Status: Needs work » Needs review

The line $stack[$i] += ['file' => '?', 'line' => '?', 'args' => []]; can be reduced to just $stack[$i] += ['file' => '?', 'line' => '?']; with this change.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

halth’s picture

Version: 8.6.x-dev » 8.8.x-dev

Hi @Gribnif
Thanks for you notes and for the patch attached.
I should say though, that I couldn't reproduce the issue on a fresh D8 install (8.8.x) by following the steps proposed by you on #8.

Can you give it a try and double check that you can reproduce the issue under these circumstances on a fresh D8 install?

Gribnif’s picture

I can no longer reproduce the problem, either. Given that it concerns the debug_backtrace() call, it could be that it only happens with certain versions of PHP.

halth’s picture

Cool. So how should we proceed with the issue? Do you think we should we close it? Does anyone in the comments have any thoughts on this?
I was facing this issue on an 8.7.x install while executing a query through the "Execute PHP" page, from devel_php.

Not sure if this is really a core issue, even though the fix proposed in the patches seem reasonable to me. That `isset()` call could potentially "close the door" for new trouble.

What do you guys think?

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mondrake’s picture

Version: 8.9.x-dev » 9.0.x-dev
Status: Needs review » Needs work
Issue tags: +Needs tests, +Novice

This change, below, in #2867788: Log::findCaller fails to report the correct caller function with non-core drivers. should have fixed the issued here, for D9 (and for D8 if the patch gets backported).

+++ b/core/lib/Drupal/Core/Database/Log.php
@@ -141,26 +150,62 @@ public function log(StatementInterface $statement, $args, $time) {
+    // Return the previous function call whose stack entry has a 'file' key,
+    // that is, it is not a callback or a closure.
+    for ($i = $n; $i < count($stack); $i++) {
+      if (!empty($stack[$i]['file'])) {
         return [
           'file' => $stack[$i]['file'],
           'line' => $stack[$i]['line'],
           'function' => $stack[$i + 1]['function'],
-          'class' => $stack[$i + 1]['class'],
-          'type' => isset($stack[$i + 1]['type']) ? $stack[$i + 1]['type'] : NULL,
-          'args' => $stack[$i + 1]['args'],
+          'class' => $stack[$i + 1]['class'] ?? NULL,
+          'type' => $stack[$i + 1]['type'] ?? NULL,
+          'args' => $stack[$i + 1]['args'] ?? [],
         ];
       }

However, a test could be added to make sure we do not regress here. The test could be added to Drupal\KernelTests\Core\Database\testContribDriverLog, by adding another sample backtrace to the providerContribDriverLog that has no args key, and checking that the returned entry has an empty array for the args key.

xjm’s picture

Version: 9.0.x-dev » 8.8.x-dev

Since we should fix this bug in D8 too, I'm filing it against 8.8.x (which is the current bugfix support branch). Patches can be tested against other branches as needed when they're uploaded.

The issue will be automatically updated to 8.9.x after the last 8.8.x bugfix release. Thanks!

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
DanielVeza’s picture

Version: 9.3.x-dev » 7.x-dev
Status: Needs work » Closed (outdated)
Issue tags: +Bug Smash Initiative

Now that D8 is out of support and this is fixed for D9 I'm moving this back to the 7 queue.