After upgrading from 2.0.2 to 2.0.3, I encountered a type of a page on the site generating this error message at the top:

Warning: array_key_exists(): The first argument should be either a string or an integer in Drupal\easy_breadcrumb\EasyBreadcrumbBuilder->build() (line 492 of modules/contrib/easy_breadcrumb/src/EasyBreadcrumbBuilder.php). 

This site in question and its configuration are all pretty new to me, but it seemed to be showing on "result" pages provided by the drupal/quiz (example URL apparently being something like /quiz/4/result/455) and I was able to run a quick ksm($title) after line 490 that revealed that it's likely that TranslatableMarkup and its use of arguments to form the page title was presumably part of the culprit here:

Screenshot showing kint output of page title value revealing it to be TranslateableMarkup

Presumably of course newly in 2.0.3 via #3272480: Titles to be replaced while generating segments doesn't work in all the scenarios.

Sorry that at the moment I don't know (or didn't have time to figure out) a number of things (much less attempt to work on a patch), including:

  • If the same kind of problem happens with other kinds of objects beyond just TranslatableMarkup or other cases that might also affected by passing argument values or whatever
  • What all the context/conditions/functionality around line 492 are, and whether other areas of the code might be similarly affected
  • If quiz is doing something to cause this that they shouldn't be doing on their end (but ... shrug? since other contrib or custom modules might be doing something similar anyway, and I'd assume (maybe incorrectly?) that easy_breadcrumb could perhaps fail or otherwise deal with this kind of case more gracefully?)

Thanks!

Command icon 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

maxstarkenburg created an issue. See original summary.

maxstarkenburg’s picture

maxstarkenburg’s picture

Title: As of 2.0.3: Warning: array_key_exists(): The first argument should be either a string or an integer" when encountering page titled via TranslatableMarkup » As of 2.0.3: "Warning: array_key_exists(): The first argument should be either a string or an integer" when encountering page titled via TranslatableMarkup
maxstarkenburg’s picture

Issue summary: View changes
maxstarkenburg’s picture

Issue summary: View changes
greg boggs’s picture

This is a super helpful start Max! Thank you.

cpierce42’s picture

StatusFileSize
new898 bytes

I too am looking into this. A very similar problem to to this as I am going from php 7.4 to 8.0.13 (Acquia Hosting)
Line 492 changing it to include a check for string or int:
`if (!empty($title) && (is_string($title) || is_numeric($title)) && array_key_exists($title, $replacedTitles)) {`
fixes the problem for me. This error is related to TMGMT returning an object instead of a string for `$entity->label()` on line 490

greg boggs’s picture

For TMGMT, you should be able to update your TMGMT to fix the problem, but also our code should be robust enough not to get warnings when other modules have a bug.

cpierce42’s picture

Status: Active » Needs review

Hi Greg! Thanks for your quick response. I am encountering this issue not as a warning but as a error on administration pages using breadcrumbs. (specifically when requesting translation jobs).

Turning off the breadcrumb setting `use on admin pages` is not possible for us, but is a 'no-code' work around that might work for others.

With respect to your comment, TMGMT is fully updated to my knowledge and the error occurs.
Using

  • Drupal 9.4.1
  • php 8.0.13 (Acquia hosting)
  • The most current TMGMT to my knowledge `^1.13`

* Additional edit: forgot to mark my patch as needing review

greg boggs’s picture

Hrm, ok this may be a different page in tmgmt, here's the recent issue for that:

https://www.drupal.org/project/tmgmt/issues/3291991

greg boggs’s picture

There should be code for when a title is an object instead of a string... Not sure why it's not working in this case.

yes_max’s picture

StatusFileSize
new1.07 KB

This one is working for me on 2.0.3, and its a pretty simple change. Hope it can help someone else.

greg boggs’s picture

looks good. Can you open the change as a merge request?

omkar-pd made their first commit to this issue’s fork.

omkar-pd’s picture

Created MR of patch #12.

martijn de wit’s picture

Status: Needs review » Reviewed & tested by the community

Patch from #12 fixed our problem.
Before this patch we encountered the same error.

First we used the patch from #7. But our team thinks #12 is a nicer solution.

Merge request seems to look fine.

  • Greg Boggs committed 3f9a050 on 2.x authored by omkar-pd
    Issue #3293237 by omkar-pd, cpierce42, thisismax, maxstarkenburg, Greg...
greg boggs’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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

megachriz’s picture

I see something on this merge request that looks wrong.
From https://git.drupalcode.org/project/easy_breadcrumb/-/blob/52971f62f5f114...

if ($entity instanceof EntityInterface && $entity->hasLinkTemplate('canonical')) {
  $title = $this->normalizeText($this->getTitleString($route_request, $route_match, $replacedTitles));
  // Add this entity's cacheability metadata.
  $breadcrumb->addCacheableDependency($entity);
  $title = (string) $entity->label();
  // If the title is to be replaced replaces the title.
  if (!empty($title) && array_key_exists($title, $replacedTitles)) {
    $title = $replacedTitles[$title];
  }
  if ($title && $this->config->get(EasyBreadcrumbConstants::TRUNCATOR_MODE)) {
    $title = $this->truncator($title);
  }
  break;
}

$title first gets set on line 501:

$title = $this->normalizeText($this->getTitleString($route_request, $route_match, $replacedTitles));

But then gets overwritten on line 504:

$title = (string) $entity->label();

So line 501 (with the normalizeText() call) looks redundant. That, or a check is missing that makes the right title to be picked.

apfelkomplott’s picture

I agree with MegaChriz that it looks like there went something wrong in the merge, but I think the
$title = (string) $entity->label();
is the redundant one, since the getTitleString()-method also considers Breadcrumb-Overrides (which would be overridden by the entity-label afterwards.