You are here

function twitter_profile_widget_entity_delete in Twitter Profile Widget 8

Implements hook_entity_delete().

When a twitter widget is deleted, we want to clean up any blocks that reference that entity ID, as they will no longer work.

File

./twitter_profile_widget.module, line 82
Contains twitter_profile_widget.module.

Code

function twitter_profile_widget_entity_delete(EntityInterface $entity) {

  // Only act on twitter_widget entities.
  if ($entity
    ->getEntityTypeId() == 'twitter_widget' && $entity
    ->bundle() == 'twitter_widget') {
    $config = \Drupal::config('twitter_profile_widget.settings');
    if ($config
      ->get('integrate_blocks')) {
      $entity_type = 'twitter_widget';
      $ids = \Drupal::entityQuery('block_content')
        ->condition('type', $entity_type)
        ->condition('field_twitter_widget.target_id', $entity
        ->get('id')->value)
        ->execute();
      if (!empty($ids)) {
        $storage_handler = \Drupal::entityTypeManager()
          ->getStorage('block_content');
        $entities = $storage_handler
          ->loadMultiple($ids);
        $storage_handler
          ->delete($entities);
        \Drupal::logger('twitter_profile_widgets')
          ->notice('Any Twitter blocks associated with "%name" have been deleted.', [
          '%name' => $entity
            ->get('name')->value,
        ]);
      }
    }
  }
}