You are here

public function Instances::applyChanges in Smart Date 3.1.x

Same name and namespace in other branches
  1. 8.2 modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::applyChanges()
  2. 3.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::applyChanges()
  3. 3.0.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::applyChanges()
  4. 3.2.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::applyChanges()
  5. 3.3.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::applyChanges()
  6. 3.4.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::applyChanges()

Use the overrides for this RRule object to update the parent entity.

Parameters

\Drupal\smart_date_recur\Entity\SmartDateRule $rrule: The rule whose overrides will be applied to the parent entity.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse A redirect to the view of the parent entity.

1 string reference to 'Instances::applyChanges'
smart_date_recur.routing.yml in modules/smart_date_recur/smart_date_recur.routing.yml
modules/smart_date_recur/smart_date_recur.routing.yml

File

modules/smart_date_recur/src/Controller/Instances.php, line 351

Class

Instances
Provides listings of instances (with overrides) for a specified rule.

Namespace

Drupal\smart_date_recur\Controller

Code

public function applyChanges(SmartDateRule $rrule) {

  // Get all the necessary data elements from the rrule object.
  if (!($entity = $rrule
    ->getParentEntity())) {
    return $this
      ->returnError();
  }
  $rid = $rrule
    ->id();
  $field_name = $rrule->field_name
    ->getString();

  // Retrieve all existing values for the field.
  $values = $entity
    ->get($field_name)
    ->getValue();
  $first_instance = FALSE;

  // Go through the existing values and remove all this rule's instances.
  foreach ($values as $index => $value) {
    if ($value['rrule'] == $rid) {
      if (!$first_instance) {

        // Save the first instance to use as a template.
        $first_instance = $value;
      }

      // Remove all existing values for this rrule, so they can be replaced.
      unset($values[$index]);
    }
  }

  // Retrieve all instances for this rule, with overrides applied.
  $instances = $rrule
    ->getRuleInstances();
  foreach ($instances as $rrule_index => $instance) {

    // Apply instance values to our template, and add to the field values.
    $first_instance['value'] = $instance['value'];
    $first_instance['end_value'] = $instance['end_value'];

    // Calculate the duration, since it isn't returned.
    $first_instance['duration'] = ($instance['end_value'] - $instance['value']) / 60;
    $first_instance['rrule_index'] = $rrule_index;
    $values[] = $first_instance;
  }

  // Add to the entity, and save.
  $entity
    ->set($field_name, $values);
  $entity
    ->save();

  // Redirect to the entity view.
  return new RedirectResponse($entity
    ->toUrl()
    ->toString());
}