You are here

public function FivestarItem::postSave in Fivestar 8

Defines custom post-save behavior for field values.

This method is called during the process of saving an entity, just after values are written into storage. This is useful mostly when the business logic to be implemented always requires the entity identifier, even when storing a new entity. For instance, when implementing circular entity references, the referenced entity will be created on pre-save with a dummy value for the referring entity identifier, which will be updated with the actual one on post-save.

In the rare cases where item properties depend on the entity identifier, massaging logic will have to be implemented on post-save and returning TRUE will allow them to be rewritten to the storage with the updated values.

Parameters

bool $update: Specifies whether the entity is being updated or created.

Return value

bool Whether field items should be rewritten to the storage as a consequence of the logic implemented by the custom behavior.

Overrides FieldItemBase::postSave

File

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

Class

FivestarItem
Plugin implementation of the 'fivestar' field type.

Namespace

Drupal\fivestar\Plugin\Field\FieldType

Code

public function postSave($update) {
  $entity = $this
    ->getEntity();
  $field_definition = $this
    ->getFieldDefinition();
  $field_settings = $field_definition
    ->getSettings();
  $vote_manager = \Drupal::service('fivestar.vote_manager');
  $target_entity = $this
    ->getTargetEntity($entity, $field_settings);
  $vote_rating = $entity
    ->get($field_definition
    ->getName())->rating ?: 0;
  $owner = $this
    ->getVoteOwner($entity, $field_settings['rated_while']);
  if ($update) {

    // Delete previous votes.
    $criteria = [
      'entity_id' => $entity
        ->id(),
      'entity_type' => $entity
        ->getEntityTypeId(),
      'type' => $field_settings['vote_type'],
      'user_id' => $owner
        ->id(),
    ];
    if ($owner
      ->isAnonymous()) {
      $ip_address = \Drupal::request()
        ->getClientIp();
      $criteria['vote_source'] = hash('sha256', serialize($ip_address));
    }
    foreach ($vote_manager
      ->getVotesByCriteria($criteria) as $vote) {
      $vote
        ->delete();
    }

    // Delete votes from target entity.
    if (!empty($target_entity)) {
      $criteria['entity_id'] = $target_entity
        ->id();
      $criteria['entity_type'] = $target_entity
        ->getEntityTypeId();
      foreach ($vote_manager
        ->getVotesByCriteria($criteria) as $target_vote) {
        $target_vote
          ->delete();
      }
    }
  }

  // Add new vote.
  $vote_manager
    ->addVote($entity, $vote_rating, $field_settings['vote_type'], $owner
    ->id());
  if (!empty($target_entity) && $entity
    ->isPublished()) {
    $vote_manager
      ->addVote($target_entity, $vote_rating, $field_settings['vote_type'], $owner
      ->id());
  }

  // No changes made to the Fivestar field item in this method.
  return FALSE;
}