You are here

function ds_export in Display Suite 6.3

Same name and namespace in other branches
  1. 6 includes/ds.tools.inc \ds_export()
  2. 6.2 includes/ds.tools.inc \ds_export()

Export functionality.

1 string reference to 'ds_export'
_ds_ui_menu in includes/ds.registry.inc
Return menu items and import default settings.

File

includes/ds.tools.inc, line 128
Tools for Display suite like export & import.

Code

function ds_export(&$form_state) {
  $form = array();
  $options = array();
  $ds_variables = array();
  $step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] + 1 : 1;
  switch ($step) {

    // Show selection form.
    case 1:
      foreach (module_implements('ds_api') as $module) {
        $api_info = ds_api_info($module);
        $module = $api_info['module'];

        // Content types.
        foreach ($api_info['types']() as $tkey => $type) {
          $global_exclude = variable_get($module . '_type_' . $tkey, FALSE);

          // Views displays is special case.
          if ($module == 'vd') {
            if ($global_exclude == TRUE) {
              continue;
            }
          }
          $options[$module . '-' . $type->type] = check_plain($type->name);
          if ($global_exclude == TRUE) {
            $ds_variables[] = $module . '_type_' . $tkey;
          }
        }

        // Plugins.
        $plugins = variable_get($module . '_plugin_settings', array());
        if (!empty($plugins)) {
          $ds_variables[] = $module . '_plugin_settings';
        }

        // Build modes
        $build_modes = variable_get($module . '_build_modes', array());
        if (!empty($build_modes)) {
          $ds_variables[] = $module . '_build_modes';
        }

        // Build modes exclude.
        $ds_variables[] = $module . '_buildmodes_exclude';
      }
      if (!empty($options)) {
        $form['#prefix'] = t('Select one or more types to export.');
        $form['types'] = array(
          '#title' => 'Types',
          '#type' => 'checkboxes',
          '#options' => $options,
          '#required' => TRUE,
        );
        $form['module'] = array(
          '#title' => t('Module'),
          '#type' => 'textfield',
          '#description' => t('Fill in a name for your module (optional)'),
        );
      }
      else {
        $form['info'] = array(
          '#type' => 'item',
          '#value' => t('No object types found to export.'),
        );
      }
      break;

    // Show export.
    case 2:
      $export = ds_export_build($form_state['values']['types']);
      if (!empty($export['exclude']['fields'])) {
        drupal_set_message(t('Following fields were not included in the export because they are default fields: %fields', array(
          '%fields' => implode(', ', $export['exclude']['fields']),
        )));
      }
      if (!empty($export['ds'])) {
        $module_name = !empty($form_state['values']['module']) ? $form_state['values']['module'] : 'YOURMODULENAME';
        $form['export_hooks'] = array(
          '#title' => t('Display suite hooks'),
          '#description' => t('These are the hooks you need to implement for default data. You are free to return all data in this hook, this is just a suggestion.'),
          '#type' => 'textarea',
          '#cols' => 60,
          '#rows' => 8,
          '#value' => "<?php\n",
        );
        $form['export_data'] = array(
          '#title' => t('Display suite export data'),
          '#description' => t('You can paste this data into @module_name/@module_name.ds_default.inc. If you only want to store this data to import later, omit the function declaration.', array(
            '@module_name' => $module_name,
          )),
          '#value' => "<?php\n/**\n * @file\n * Display suite default settings.\n */\n",
          '#type' => 'textarea',
          '#cols' => 60,
          '#rows' => 15,
        );

        // Display settings.
        if (isset($export['ds']['settings'])) {
          $export_value = ds_var_export($export['ds']['settings']);
          $form['export_hooks']['#value'] .= "\n/**\n * Implementation of hook_ds_default_settings().\n */\nfunction " . $module_name . "_ds_default_settings() {\n  include_once('" . $module_name . ".ds_default.inc');\n  return _" . $module_name . "_ds_default_settings();\n}\n";
          $form['export_data']['#value'] .= "\nfunction _" . $module_name . "_ds_default_settings() {\n  \$data = " . $export_value . ";\n  return \$data;\n}\n";
        }

        // Fields.
        if (isset($export['ds']['fields'])) {
          if (!empty($export['ds']['fields'])) {
            $export_value = ds_var_export($export['ds']['fields']);
            $form['export_hooks']['#value'] .= "\n/**\n * Implementation of hook_ds_fields().\n */\nfunction " . $module_name . "_ds_fields() {\n  include_once('" . $module_name . ".ds_default.inc');\n  return _" . $module_name . "_ds_fields();\n}\n";
            $form['export_data']['#value'] .= "\nfunction _" . $module_name . "_ds_fields() {\n  \$data = " . $export_value . ";\n  return \$data;\n}\n";
          }
        }
      }
      else {
        $form['info'] = array(
          '#type' => 'item',
          '#value' => t('No settings found to export.'),
        );
      }
      break;
  }
  if ($step == 1 && !empty($options)) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Export'),
    );
    if (!empty($ds_variables)) {
      $form['other'] = array(
        '#type' => 'item',
        '#value' => t('Other variables you might consider to have in code (likely your install file): %variables. You can look this up in your database or use <a href="http://drupal.org/project/variable_dump">http://drupal.org/project/variable_dump</a> to create variable exports.', array(
          '%variables' => implode(', ', $ds_variables),
        )),
      );
    }
  }
  $form['step'] = array(
    '#type' => 'value',
    '#value' => $step,
  );
  return $form;
}