View source
<?php
function entity_features_get_controller($type) {
$static =& drupal_static(__FUNCTION__);
if (!isset($static[$type])) {
$info = entity_get_info($type);
$info += array(
'features controller class' => 'EntityDefaultFeaturesController',
);
$static[$type] = $info['features controller class'] ? new $info['features controller class']($type) : FALSE;
}
return $static[$type];
}
class EntityDefaultFeaturesController {
protected $type, $info;
public function __construct($type) {
$this->type = $type;
$this->info = entity_get_info($type);
$this->info['entity keys'] += array(
'module' => 'module',
'status' => 'status',
);
$this->statusKey = $this->info['entity keys']['status'];
$this->moduleKey = $this->info['entity keys']['module'];
if (!empty($this->info['bundle of'])) {
$entity_info = entity_get_info($this->info['bundle of']);
$this->bundleKey = $entity_info['bundle keys']['bundle'];
}
}
public function api() {
return array(
$this->type => array(
'name' => $this->info['label'],
'feature_source' => TRUE,
'default_hook' => isset($this->info['export']['default hook']) ? $this->info['export']['default hook'] : 'default_' . $this->type,
'base' => 'entity',
'file' => drupal_get_path('module', 'entity') . '/entity.features.inc',
),
);
}
public function export_options() {
$options = array();
foreach (entity_load_multiple_by_name($this->type, FALSE) as $name => $entity) {
$options[$name] = entity_label($this->type, $entity);
}
return $options;
}
public function export($data, &$export, $module_name = '') {
$pipe = array();
foreach (entity_load_multiple_by_name($this->type, $data) as $name => $entity) {
if ($entity->{$this->statusKey} & ENTITY_IN_CODE && $entity->{$this->moduleKey} != $module_name) {
$module = $entity->{$this->moduleKey};
$export['dependencies'][$module] = $module;
}
else {
$export['features'][$this->type][$name] = $name;
if (!empty($this->info['bundle of'])) {
$fields = field_info_instances($this->info['bundle of'], $entity->{$this->bundleKey});
foreach ($fields as $name => $field) {
$pipe['field'][] = "{$field['entity_type']}-{$field['bundle']}-{$field['field_name']}";
$pipe['field_instance'][] = "{$field['entity_type']}-{$field['bundle']}-{$field['field_name']}";
}
}
}
}
if ($data && !empty($this->info['module'])) {
$export['dependencies'][$this->info['module']] = $this->info['module'];
$export['dependencies']['entity'] = 'entity';
}
return $pipe;
}
public function export_render($module, $data, $export = NULL) {
$output = array();
$output[] = ' $items = array();';
foreach (entity_load_multiple_by_name($this->type, $data) as $name => $entity) {
$export = " \$items['{$name}'] = entity_import('{$this->type}', '";
$export .= addcslashes(entity_export($this->type, $entity, ' '), '\\\'');
$export .= "');";
$output[] = $export;
}
$output[] = ' return $items;';
$output = implode("\n", $output);
$hook = isset($this->info['export']['default hook']) ? $this->info['export']['default hook'] : 'default_' . $this->type;
return array(
$hook => $output,
);
}
public function revert($module = NULL) {
if ($defaults = features_get_default($this->type, $module)) {
entity_delete_multiple($this->type, array_keys($defaults));
}
}
}
function entity_features_api() {
$items = array();
foreach (entity_crud_get_info() as $type => $info) {
if (!empty($info['exportable']) && ($controller = entity_features_get_controller($type))) {
$items += $controller
->api();
}
}
return $items;
}
function entity_features_export_options($a1, $a2 = NULL) {
$entity_type = is_string($a1) ? $a1 : $a2;
return entity_features_get_controller($entity_type)
->export_options();
}
function entity_features_export($data, &$export, $module_name, $entity_type) {
return entity_features_get_controller($entity_type)
->export($data, $export, $module_name);
}
function entity_features_export_render($module, $data, $export, $entity_type) {
return entity_features_get_controller($entity_type)
->export_render($module, $data, $export);
}
function entity_features_revert($module, $entity_type) {
return entity_features_get_controller($entity_type)
->revert($module);
}
function entity_features_post_restore($op, $items = array()) {
if ($op == 'rebuild') {
entity_defaults_rebuild();
}
}