You are here

public function DateRecurOccurrences::onSave in Recurring Dates Field 3.1.x

Same name and namespace in other branches
  1. 8.2 src/DateRecurOccurrences.php \Drupal\date_recur\DateRecurOccurrences::onSave()
  2. 3.x src/DateRecurOccurrences.php \Drupal\date_recur\DateRecurOccurrences::onSave()
  3. 3.0.x src/DateRecurOccurrences.php \Drupal\date_recur\DateRecurOccurrences::onSave()

Respond to a field value insertion or update.

Parameters

\Drupal\date_recur\Event\DateRecurValueEvent $event: The date recur event.

File

src/DateRecurOccurrences.php, line 95

Class

DateRecurOccurrences
Manages occurrences tables and the data that populates them.

Namespace

Drupal\date_recur

Code

public function onSave(DateRecurValueEvent $event) : void {

  /** @var \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem[]|\Drupal\date_recur\Plugin\Field\FieldType\DateRecurFieldItemList $list */
  $list = $event
    ->getField();
  $fieldDefinition = $list
    ->getFieldDefinition();
  $tableName = static::getOccurrenceCacheStorageTableName($fieldDefinition
    ->getFieldStorageDefinition());
  $isInsert = $event
    ->isInsert();
  if (!$isInsert) {

    // Delete all existing values for entity and field combination.

    /** @var string|int $entityId */
    $entityId = $list
      ->getEntity()
      ->id();
    $this->database
      ->delete($tableName)
      ->condition('entity_id', (string) $entityId)
      ->execute();
  }
  foreach ($list as $item) {
    $this
      ->saveItem($item, $tableName);
  }
}