function CommentHooks::fieldConfigDelete

Same name and namespace in other branches
  1. 11.x core/modules/comment/src/Hook/CommentHooks.php \Drupal\comment\Hook\CommentHooks::fieldConfigDelete()

Implements hook_ENTITY_TYPE_delete() for 'field_config'.

Attributes

#[Hook('field_config_delete')]

File

core/modules/comment/src/Hook/CommentHooks.php, line 149

Class

CommentHooks
Hook implementations for comment.

Namespace

Drupal\comment\Hook

Code

public function fieldConfigDelete(FieldConfigInterface $field) : void {
  if ($field->getType() == 'comment') {
    // Delete all comments that used by the entity bundle.
    $entity_query = \Drupal::entityQuery('comment')->accessCheck(FALSE);
    $entity_query->condition('entity_type', $field->getEntityTypeId());
    $entity_query->condition('field_name', $field->getName());
    $cids = $entity_query->execute();
    $comment_storage = \Drupal::entityTypeManager()->getStorage('comment');
    $comments = $comment_storage->loadMultiple($cids);
    $comment_storage->delete($comments);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.