function uuid_paragraphs_features_rebuild in UUID Features Integration 7
Implements hook_features_rebuild().
1 call to uuid_paragraphs_features_rebuild()
- uuid_paragraphs_features_revert in includes/
uuid_paragraphs.features.inc - Implements hook_features_revert().
File
- includes/
uuid_paragraphs.features.inc, line 120 - Features hooks for the uuid_paragraphs features component.
Code
function uuid_paragraphs_features_rebuild($module) {
// Make sure all referenced entities exist.
if (module_exists('taxonomy')) {
// This module may not be installed.
taxonomy_features_rebuild($module);
}
field_features_rebuild($module);
uuid_node_features_rebuild($module);
cache_clear_all();
$paragraphs = features_get_default('uuid_paragraphs', $module);
if (!empty($paragraphs)) {
$entity_type = 'paragraphs_item';
foreach ($paragraphs as $paragraphs_item) {
try {
$fields = field_info_instances('paragraphs_item', $paragraphs_item['bundle']);
foreach ($fields as $field_name => $field_instance_config) {
$field_config = field_info_field($field_name);
if (isset($paragraphs_item[$field_name])) {
foreach ($paragraphs_item[$field_name] as $lang => $field_data) {
foreach ($field_data as $delta => $value) {
if ($field_config['type'] == 'entityreference' || $field_config['type'] == 'taxonomy_term_reference') {
$columns = array_keys($field_config['columns']);
$first_column = reset($columns);
if (isset($field_config['settings']['target_type'])) {
$target_entity = $field_config['settings']['target_type'];
}
elseif ($field_config['type'] == 'taxonomy_term_reference') {
$target_entity = 'taxonomy_term';
}
else {
throw new Exception('Unknown target.');
}
$ids = entity_get_id_by_uuid($target_entity, array(
$value[$first_column],
));
$paragraphs_item[$field_name][$lang][$delta][$first_column] = $ids ? reset($ids) : UUID_FEATURES_DEFAULT_FALLBACK_ID;
}
}
}
}
}
// Create a new paragraphs.
$paragraphs_item['original'] = TRUE;
$entity = entity_import('paragraphs_item', json_encode($paragraphs_item));
// Hack to ignore missing host entities.
/* @var ParagraphsItemEntity $entity */
$entity->is_new = FALSE;
// If this is an update, there will be a by-UUID matching paragraphs
// and we'll use that object for further processing.
$existing = entity_get_id_by_uuid('paragraphs_item', array(
$paragraphs_item['uuid'],
));
if (!empty($existing)) {
$entity = entity_load_single('paragraphs_item', $existing[$paragraphs_item['uuid']]);
foreach ($paragraphs_item as $key => $value) {
$entity->{$key} = $value;
}
}
drupal_alter('uuid_entity_features_rebuild', $entity_type, $entity, $paragraphs_item, $module);
uuid_features_file_field_import($entity, 'paragraphs_item', $module);
$entity
->save(TRUE);
} catch (Exception $e) {
}
}
module_invoke_all('uuid_entity_features_rebuild_complete', $entity_type, $paragraphs, $module);
}
uuid_node_features_rebuild($module);
}