Problem/Motivation

If one of the promoted nodes is deleted, the listing page returns the following PHP error:

Error: Call to a member function label() on null in Drupal\vais_promos\VaisPromoListBuilder->buildRow() (line 107 of modules/contrib/vais_promos/src/VaisPromoListBuilder.php).

This is because the code doesn't do a check that the node exists before calling label() on it and there's no hook_node_delete() to clean up promotions when the associated node is deleted.

$promoContent = $this->entityTypeManager->getStorage('node')->load($entity->promo_content->target_id);
$promoReferenceLink = Link::createFromRoute($promoContent->label(), 'entity.node.canonical', ['node' => $entity->promo_content->target_id]);

Steps to reproduce

Promote a node and then delete it.

Proposed resolution

Recommend adding a check that the node exists before calling label and also adding a hook_node_delete call to delete the promotion if the node it is associated with is deleted.

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

jastraat created an issue. See original summary.

timozura made their first commit to this issue’s fork.

jastraat’s picture

Status: Active » Reviewed & tested by the community

The updated code successfully:

  • prevents an error on the list page if a deleted node is referenced by a promotion
  • automatically deletes the associated promotion when its node is deleted
  • does not display the promotion at the top of searches when the associated node is unpublished or deleted

  • timozura committed ffc80c01 on 1.0.x authored by jastraat
    Issue #3564136: Delete promo when associated node is deleted.
    
timozura’s picture

Version: 1.0.x-dev » 1.0.0-beta5
Status: Reviewed & tested by the community » Fixed

Fix Added to 1.0.0-beta5 release.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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