You are here

function field_tools_bundle_export_form in Field tools 7

Form to export all fields of a bundle.

Parameters

$entity_type: The machine name of the entity.

$bundle: The machine name of the bundle, or a bundle object if the particular entity type has a menu loader for bundles.

1 string reference to 'field_tools_bundle_export_form'
field_tools_menu in ./field_tools.module
Implements hook_menu().

File

./field_tools.admin.inc, line 737
Contains admin callbacks for the Field tools module.

Code

function field_tools_bundle_export_form($form, $form_state, $entity_type, $bundle) {

  // Get the bundle name if the bundle name is really a bundle object.
  $bundle_name = field_extract_bundle($entity_type, $bundle);
  $instances = field_info_instances($entity_type, $bundle_name);
  $options_fields = array();
  foreach ($instances as $field_name => $field) {
    $options_fields[$field_name] = $field['label'];
  }
  asort($options_fields);
  $form['fields'] = array(
    '#title' => t('Fields to export'),
    '#type' => 'checkboxes',
    '#options' => $options_fields,
    '#description' => t("Select fields that you want to export."),
  );
  if (module_exists('field_group')) {
    $groups = field_group_info_groups($entity_type, $bundle_name, 'form');
    $options_groups = array();
    foreach ($groups as $group_name => $group) {
      $options_groups[$group_name] = $group->label;
    }
    asort($options_groups);
    $form['groups'] = array(
      '#title' => t('Groups to export'),
      '#type' => 'checkboxes',
      '#options' => $options_groups,
      '#description' => t("Select groups that you want to export."),
    );
  }

  // Check if the form has already been submitted, if so, storage will be
  // filled and the export code can be built and put into a textarea.
  if (isset($form_state['storage']['fields'])) {
    $export_fields = array_filter($form_state['storage']['fields']);
    $export = array();
    foreach ($export_fields as $field_name) {
      $instance = $instances[$field_name];
      $export[] = field_tools_get_field_code($instance);
    }
    if (module_exists('field_group')) {
      $export_groups = array_filter($form_state['storage']['groups']);
      $groups = field_group_info_groups($entity_type, $bundle_name, 'form');
      foreach ($export_groups as $group_name) {
        $group = $groups[$group_name];
        $export[] = field_tools_get_group_code($group);
      }
    }
    $code = implode("\n", $export);
    $form['code'] = field_tools_textarea(t('Export code'), $code);
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Get export code'),
  );
  return $form;
}