class BootstrapTourEntityController in Bootstrap Tour 7.2
Class BootstrapTourEntityController
Ensure Bootstrap Tours are imported and exported with tour steps.
Hierarchy
- class \DrupalDefaultEntityController implements DrupalEntityControllerInterface
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
Expanded class hierarchy of BootstrapTourEntityController
1 string reference to 'BootstrapTourEntityController'
- bootstrap_tour_entity_info in ./
bootstrap_tour.module - Implements hook_entity_info().
File
- includes/
bootstrap_tour.controller.inc, line 13 - Defines the inline entity form controller for BootstrapTourStep entities.
View source
class BootstrapTourEntityController extends EntityAPIControllerExportable {
public function export($entity, $prefix = '') {
$wrapper = entity_metadata_wrapper('bootstrap_tour', $entity);
if (field_info_instance('bootstrap_tour', 'bootstrap_tour_step_reference', 'bootstrap_tour')) {
$entity->steps = $wrapper->bootstrap_tour_step_reference
->value();
// Need to remove the step reference as they will be overriden anyway.
// Remove step keys as they are ignored anyway.
unset($entity->bootstrap_tour_step_reference);
foreach ($entity->steps as $step) {
unset($step->bootstrap_tour_step_id);
}
}
return parent::export($entity, $prefix);
}
public function import($data) {
return parent::import($data);
}
/**
* Add in a presave before any else to handle imported steps.
*/
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);
}
}