Skip to content

Instantly share code, notes, and snippets.

@crittermike
Created March 20, 2014 21:34
Show Gist options
  • Save crittermike/9674385 to your computer and use it in GitHub Desktop.
Save crittermike/9674385 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Features hooks for the uuid_fpp features component.
*/
/**
* Implements hook_features_export_options().
*/
function uuid_fpp_features_export_options() {
$options = array();
$query = 'SELECT f.fpid, f.title, f.bundle, f.uuid
FROM {fieldable_panels_panes} f ORDER BY f.bundle, f.title ASC';
$results = db_query($query);
foreach ($results as $fpp) {
$bundle_name = ucwords(str_replace('_', ' ', $fpp->bundle));
$options[$fpp->uuid] = $bundle_name . ' - ' . $fpp->title;
}
return $options;
}
/**
* Implements hook_features_export().
*/
function uuid_fpp_features_export($data, &$export, $module_name = '') {
$export['dependencies']['fieldable_panels_panes'] = 'fieldable_panels_panes';
$export['dependencies']['uuid_features'] = 'uuid_features';
uuid_features_load_module_includes();
$fpids = entity_get_id_by_uuid('fieldable_panels_pane', $data);
foreach ($fpids as $uuid => $fpid) {
// Load the FPP matching the $fpid.
$query = new EntityFieldQuery();
$fpp = $query
// We just want one FPP: the one matching
// the current $fpid.
->entityCondition('entity_type', 'fieldable_panels_pane')
->propertyCondition('fpid', $fpid)
->range(0, 1)
->execute();
$export['features']['uuid_fpp'][$uuid] = $uuid;
$pipe['fieldable_panels_pane'][$fpp['fieldable_panels_pane'][$fpid]->bundle] = $fpp['fieldable_panels_pane'][$fpid]->bundle;
// drupal_alter() normally supports just one byref parameter. Using
// the __drupal_alter_by_ref key, we can store any additional parameters
// that need to be altered, and they'll be split out into additional params
// for the hook_*_alter() implementations. The hook_alter signature is
// hook_uuid_fpp_features_export_alter(&$export, &$pipe, $fpp).
$data = &$export;
$data['__drupal_alter_by_ref'] = array(&$pipe);
drupal_alter('uuid_fpp_features_export', $data, $fpp);
}
return $pipe;
}
/**
* Implements hook_features_export_render().
*/
function uuid_fpp_features_export_render($module = 'foo', $data) {
$translatables = $code = array();
$code[] = ' $fpps = array();';
$code[] = '';
foreach ($data as $uuid) {
// @todo reset = TRUE as otherwise references (parent, fields) were destroyed.
$fpps = entity_uuid_load('fieldable_panels_pane', array($uuid), array(), TRUE);
if (!count($fpps)) {
continue;
}
$first_fpp = reset($fpps);
$export = clone $first_fpp;
// Do not export ids.
unset($export->vid);
unset($export->fpid);
// Enable file field exports.
uuid_features_file_field_export($export, 'fieldable_panels_pane');
$json = json_encode($export);
$export_array = json_decode($json, TRUE);
$code[] = ' $fpps[] = ' . features_var_export($export_array, ' ') . ';';
}
if (!empty($translatables)) {
$code[] = features_translatables_export($translatables, ' ');
}
$code[] = ' return $fpps;';
$code = implode("\n", $code);
return array('uuid_features_default_fpps' => $code);
}
/**
* Implements hook_features_revert().
*/
function uuid_fpp_features_revert($module) {
uuid_fpp_features_rebuild($module);
}
/**
* Implements hook_features_rebuild().
* Rebuilds terms based on UUID from code defaults.
*/
function uuid_fpp_features_rebuild($module) {
$fpps = features_get_default('uuid_fpp', $module);
if (!empty($fpps)) {
// Get info about current FPP types available in system.
$entity_info = entity_get_info("fieldable_panels_pane");
// Loop through the export.
foreach ($fpps as $data) {
// Double-check that FPP can be created/reverted.
if (!isset($entity_info['bundles'][$data['bundle']])) {
drupal_set_message('Bundle not found for fieldable panels pane of type ' . $data['bundle'] . '. Fieldable panels pane was not created/reverted.', 'warning');
}
else {
// If this is an update, there will be a by-UUID matching FPP.
$existing = entity_get_id_by_uuid('fieldable_panels_pane', array($data['uuid']));
if (!empty($existing)) {
$fpp = entity_load_single('fieldable_panels_pane', $existing[$data['uuid']]);
foreach ($data as $key => $value) {
$fpp->$key = $value;
}
}
else {
// Create a new FPP.
$fpp = entity_create('fieldable_panels_pane', $data);
}
if (!fieldable_panels_panes_save($fpp)) {
drupal_set_message('Failed to create ' . $data['bundle'] . ' fieldable panels pane ' . $data['label'], 'error');
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment