You are here

public function DefaultDateRecurOccurrenceHandler::onSave in Recurring Dates Field 8

React when a field item is saved.

Parameters

bool $update:

int $field_delta:

Overrides DateRecurOccurrenceHandlerInterface::onSave

File

src/Plugin/DateRecurOccurrenceHandler/DefaultDateRecurOccurrenceHandler.php, line 160

Class

DefaultDateRecurOccurrenceHandler
Provides the default occurrence handler.

Namespace

Drupal\date_recur\Plugin\DateRecurOccurrenceHandler

Code

public function onSave($update, $field_delta) {
  $entity_id = $this->item
    ->getEntity()
    ->id();
  $field_name = $this->item
    ->getFieldDefinition()
    ->getName();
  if ($this->item
    ->getEntity()
    ->getRevisionId()) {
    $revision_id = $this->item
      ->getEntity()
      ->getRevisionId();
  }
  else {
    $revision_id = $this->item
      ->getEntity()
      ->id();
  }
  if ($update) {
    $this->database
      ->delete($this->tableName)
      ->condition('entity_id', $entity_id)
      ->condition('field_delta', $field_delta)
      ->execute();
  }
  $fields = [
    'entity_id',
    'revision_id',
    'field_delta',
    $field_name . '_value',
    $field_name . '_end_value',
    'delta',
  ];
  $dates = $this
    ->getOccurrencesForCacheStorage();
  $delta = 0;
  $rows = [];
  foreach ($dates as $date) {
    $rows[] = [
      'entity_id' => $entity_id,
      'revision_id' => $revision_id,
      'field_delta' => $field_delta,
      $field_name . '_value' => $date['value'],
      $field_name . '_end_value' => $date['end_value'],
      'delta' => $delta,
    ];
    $delta++;
  }
  $q = $this->database
    ->insert($this->tableName)
    ->fields($fields);
  foreach ($rows as $row) {
    $q
      ->values($row);
  }
  $q
    ->execute();
}