You are here

public function FivestarItem::delete in Fivestar 8

Defines custom delete behavior for field values.

This method is called during the process of deleting an entity, just before values are deleted from storage.

Overrides FieldItemBase::delete

File

src/Plugin/Field/FieldType/FivestarItem.php, line 314

Class

FivestarItem
Plugin implementation of the 'fivestar' field type.

Namespace

Drupal\fivestar\Plugin\Field\FieldType

Code

public function delete() {
  $del_entity = $this
    ->getEntity();
  $field_settings = $this
    ->getFieldDefinition()
    ->getSettings();
  $target_entity = $this
    ->getTargetEntity($del_entity, $field_settings);
  if (!$target_entity) {
    return;
  }
  $vote_storage = \Drupal::entityTypeManager()
    ->getStorage('vote');
  $votes = $vote_storage
    ->loadByProperties([
    'entity_type' => $del_entity
      ->getEntityTypeId(),
    'entity_id' => $del_entity
      ->id(),
  ]);
  foreach ($votes as $vote) {

    // Get target vote.
    $target_votes = $vote_storage
      ->loadByProperties([
      'entity_type' => $target_entity
        ->getEntityTypeId(),
      'entity_id' => $target_entity
        ->id(),
      'type' => $vote
        ->bundle(),
      'user_id' => $vote
        ->getOwnerId(),
      'value' => $vote
        ->getValue(),
      'vote_source' => $vote
        ->getSource(),
    ]);
    foreach ($target_votes as $target_vote) {
      $target_vote
        ->delete();
    }
  }
}