You are here

public function BootstrapTourEntityController::invoke in Bootstrap Tour 7.2

Add in a presave before any else to handle imported steps.

Overrides EntityAPIControllerExportable::invoke

File

includes/bootstrap_tour.controller.inc, line 36
Defines the inline entity form controller for BootstrapTourStep entities.

Class

BootstrapTourEntityController
Class BootstrapTourEntityController

Code

public function invoke($hook, $entity) {

  // Handle imported steps
  if ($hook == 'presave' && !empty($entity->steps)) {
    $step_ids = array();
    if (!empty($entity->original)) {
      $step_ids = field_get_items('bootstrap_tour', $entity->original, 'bootstrap_tour_step_reference');
    }
    $steps = $entity->steps;
    unset($entity->steps);
    foreach ($steps as $index => $step) {
      $new_step = entity_create('bootstrap_tour_step', array(
        'path' => isset($step['path']) ? $step['path'] : '',
        'selector' => $step['selector'],
        'placement' => $step['placement'],
        'title' => $step['title'],
        'content' => $step['content'],
        'content_text_format' => $step['content_text_format'],
      ));

      // If a step of index ID existed, use existing id.
      if (!empty($step_ids[$index])) {
        $new_step->bootstrap_tour_step_id = $step_ids[$index]['target_id'];
        unset($new_step->is_new);
        unset($step_ids[$index]);
      }
      $new_step
        ->save();
      $entity->bootstrap_tour_step_reference[LANGUAGE_NONE][$index]['target_id'] = $new_step ? $new_step->bootstrap_tour_step_id : NULL;
    }

    // Delete any orphaned steps.
    if ($step_ids) {
      entity_delete_multiple('bootstrap_tour_step', $step_ids);
    }
  }
  parent::invoke($hook, $entity);
}