EntityPager is causing notice logs for undefined entity variable. This patch fixes the issue.

Notice: Undefined variable: entity in Drupal\entity_pager\EntityPager->getEntity() (line 104 of /app/web/modules/contrib/entity_pager/src/EntityPager.php)

Comments

Tristanwellz created an issue. See original summary.

Tristanwellz’s picture

Issue summary: View changes
bmunslow’s picture

StatusFileSize
new529 bytes

I'm experiencing the same issue:

Patch #1 by @Tristanwellz didn't fix the error in my case.

After all, there are chances that $entity can't be retrieved in this final check either:

    if (!isset($entity) && \Drupal::request()->attributes->has('entity')) {
      $entity = \Drupal::request()->attributes->get('entity');
    }

Which is what happens in my case, when entities are rendered in a preprocess query for entities indexed by Search API.

In short, I think it's necessary to add a final check before we return the $entity:

return isset($entity) ? $entity : null;

Attached patch with this solution fixes the problem for me.

nace_fr’s picture

Status: Active » Reviewed & tested by the community

Hi. I also have the same issue and I tested patch from #3. It applied successfully and solved the issue. Logs are clear. Changing status to RTBC.

wongjn’s picture

Version: 8.x-1.0-beta3 » 8.x-1.x-dev
Status: Reviewed & tested by the community » Needs work

Thank you for the contribution! I would like some tests to cover the case where this error shows up; can a test be added? Or if not, if someone can tell me how to recreate the error, I can look at writing the test myself.

chrisck’s picture

Another example of creating the error where the entity variable can't be retrieved is in layout builder. For example, I have a layout for node type Article.

  1. Enable layout builder
  2. Manage form display for Article (/admin/structure/types/manage/article/display)
  3. Select Use Layout Builder under Layout options
  4. Create new block view with "Format: Entity Pager" with circular enabled in Settings
  5. Add some fields, filters, sort criteria (optional)
  6. Add block view to Article layout

In layout builder, there is no entity variable as we're not viewing an entity/node. Entity pager does not know where we are in the pagination. So we get the following errors:

Notice: Undefined variable: entity in Drupal\entity_pager\EntityPager->getEntity() (line 119 of modules\contrib\entity_pager\src\EntityPager.php).
Notice: Undefined variable: entity in Drupal\entity_pager\EntityPager->getEntity() (line 123 of modules\contrib\entity_pager\src\EntityPager.php).

I've tested patch in #3 against 1.x-dev today and it is working. No more undefined variable errors.

chrisck’s picture

@Wongjn Can we set this to RTBC? I've outlined an example of how to recreate the error. The patch is a fairly simple one.

wongjn’s picture

Still needs tests please! If no one does write any, I can have a look soon.

chrisck’s picture

Issue tags: +Needs tests

  • Wongjn committed 6d23002 on 2.0.x
    Issue #3111423 by Tristanwellz, bmunslow, chrisck, Wongjn, nace_fr:...

  • Wongjn committed c80a36a on 8.x-1.x
    Issue #3111423 by Tristanwellz, bmunslow, chrisck, Wongjn, nace_fr:...
wongjn’s picture

Status: Needs work » Fixed
wongjn’s picture

Issue tags: -Needs tests

Status: Fixed » Closed (fixed)

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