Can we check and see if the entity should be deleted please?

Say you have an Event entity and you want to prevent them from being deleted if there are already sessions scheduled or something. I propose we add a canBeDeleted() method. Our placeholder method would just return TRUE, but entities could override it with something like

  /**
   * {@inheritdoc}
   */
  public function canBeDeleted() {
    $messenger = \Drupal::messenger();

    if (!empty($this->getSessions())) {
      $messenger->addMessage('I\'m sorry. I can\'t delete events which already have sessions.', 'error');
      return FALSE;
    }

    return TRUE;
  }
CommentFileSizeAuthor
entity_delete_op-can-delete.patch2.87 KBTim Bozeman
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Tim Bozeman created an issue. See original summary.

  • Tim Bozeman authored 8cbc86a on 8.x-1.x
    Issue #3078764 by Tim Bozeman: Check if an entity can be deleted
    
Tim Bozeman’s picture

Wow! What a great idea! Thanks!

Tim Bozeman’s picture

Status: Needs review » Fixed
Tim Bozeman’s picture

Status: Fixed » Closed (fixed)