View source
<?php
function bundle_clone_menu() {
$items['admin/structure/bundle_clone'] = array(
'title' => 'Bundle clone',
'description' => 'Clone any entity\'s bundle fields into a new bundle of the same entity',
'page callback' => 'bundle_clone_list',
'access arguments' => array(
'administer site configuration',
),
);
$items['admin/structure/bundle_clone/%'] = array(
'title' => 'Bundle clone',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'bundle_clone_admin',
3,
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'bundle_clone.clone.inc',
);
$items['admin/structure/bundle_clone/%/clone'] = array(
'title' => 'Clone fields',
'access arguments' => array(
'administer site configuration',
),
'file' => 'bundle_clone.clone.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/bundle_clone/%/display'] = array(
'title' => 'Update display',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'bundle_clone_admin_display',
3,
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'bundle_clone.display.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function bundle_clone_list() {
$bundles = field_info_bundles();
$items = array();
foreach ($bundles as $entity_type => $entity_bundles) {
if (count($entity_bundles) > 1) {
$items[] = l($entity_type, 'admin/structure/bundle_clone/' . $entity_type);
}
}
return array(
'#theme' => 'item_list',
'#items' => $items,
'#title' => t('Entities'),
);
}
function bundle_clone_config(&$form, $entity_type) {
$entity_bundles = field_info_bundles($entity_type);
$form['config'] = array(
'#type' => 'fieldset',
'#title' => $entity_type,
'#tree' => TRUE,
'#prefix' => l('Back to entity select', 'admin/structure/bundle_clone'),
);
$form['config']['entity_type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
$form['config']['bundles'] = array(
'#type' => 'value',
'#value' => $entity_bundles,
);
$form['config'] += bundle_clone_entity_items($entity_bundles);
}
function bundle_clone_entity_items($bundles) {
$items = array();
$options = array(
'none' => t('None'),
);
foreach ($bundles as $name => $info) {
$options[$name] = check_plain($info['label']);
}
$items['source'] = array(
'#type' => 'select',
'#title' => 'Source',
'#options' => $options,
);
$items['targets'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => 'Targets',
'#options' => $options,
);
return $items;
}
function bundle_clone_validate_selection(&$form, &$form_state) {
$source = $form_state['values']['config']['source'];
$targets = $form_state['values']['config']['targets'];
if ($source == 'none') {
form_set_error('config][source', t('NO source bundle been selected'));
}
if (count($targets) == 1) {
$target = key($targets);
if ($target == 'none') {
form_set_error('config][target', t('NO target bundle been selected'));
}
if ($source == $target) {
form_set_error('config', t('Source being same as Target is an illogical choice'));
}
}
}