You are here

public function SmartDateRule::getRuleInstances in Smart Date 8.2

Same name and namespace in other branches
  1. 3.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getRuleInstances()
  2. 3.0.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getRuleInstances()
  3. 3.1.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getRuleInstances()
  4. 3.2.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getRuleInstances()
  5. 3.3.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getRuleInstances()
  6. 3.4.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getRuleInstances()

Provide a formatted array of instances, with any overrides applied.

File

modules/smart_date_recur/src/Entity/SmartDateRule.php, line 157

Class

SmartDateRule
Defines the Smart date rule entity.

Namespace

Drupal\smart_date_recur\Entity

Code

public function getRuleInstances($before = NULL, $after = NULL) {
  $instances = $this
    ->makeRuleInstances($before, $after)
    ->toArray();
  $overrides = $this
    ->getRuleOverrides();
  $formatted = [];
  foreach ($instances as $instance) {
    $index = $instance
      ->getIndex();

    // Check for an override.
    if (isset($overrides[$index])) {

      // Check for rescheduled, overridden, or cancelled
      // and don't use default value.
      $override = $overrides[$index];
      if ($override->entity_id
        ->getString()) {

        // Overridden, retrieve appropriate entity.
        $override_type = 'overridden';
        $override = $entity_storage
          ->load($override['entity_id']);
        $field = $override
          ->get($field_name);

        // TODO: drill down and retrieve, replace values.
      }
      elseif ($override->value
        ->getString()) {

        // Rescheduled, use values from override.
        $formatted[$index] = [
          'value' => $override->value
            ->getString(),
          'end_value' => $override->end_value
            ->getString(),
          'oid' => $override
            ->id(),
        ];
      }
      else {

        // Cancelled.
      }
      continue;
    }

    // Use the generated instance as-is.
    $formatted[$index] = [
      'value' => $instance
        ->getStart()
        ->getTimestamp(),
      'end_value' => $instance
        ->getEnd()
        ->getTimestamp(),
    ];
  }

  // Return the assembled array.
  return $formatted;
}