You are here

function views_service_import in Services 7

Same name and namespace in other branches
  1. 6 services/views_service/views_service.inc \views_service_import()
  2. 6.2 services/views_service/views_service.inc \views_service_import()

Import a view.

Parameters

$view_import: Array. The view object.

$view_name: String (optional). The view name.

Return value

Number. The new view ID

1 string reference to 'views_service_import'
views_service_service in services/views_service/views_service.module
Implementation of hook_service().

File

services/views_service/views_service.inc, line 96
@author Services Dev Team

Code

function views_service_import($view_import, $view_name = '') {

  // Include the necessary files
  require_once drupal_get_path('module', 'views') . '/includes/admin.inc';

  // If this view exists, delete it so it can be re-imported.
  $existing_view = views_get_view($view_name);
  if ($existing_view) {
    $existing_view
      ->delete();

    // This forces the static cache in views_get_view() to be reset.
    $existing_view = views_get_view($view_name, TRUE);
  }

  // Import the the views using the same form as in-site import
  $form_state['values']['name'] = $view_name;
  $form_state['values']['view'] = $view_import;
  $form_state['values']['op'] = t('Import');
  drupal_execute('views_ui_import_page', $form_state);

  // Check if there is a any error
  if ($errors = form_set_error()) {
    return services_error($errors, 406);
  }

  // At this point, the new view was only cached and now its time
  // to save it and return the new View ID
  $view = $form_state['view'];
  $view
    ->save();
  return $view->vid;
}