function views_ui_import_validate

Same name in other branches
  1. 7.x-3.x includes/admin.inc \views_ui_import_validate()

Validate handler to import a view

1 string reference to 'views_ui_import_validate'
views_ui_import_page in includes/admin.inc
Import a view from cut & paste

File

includes/admin.inc, line 817

Code

function views_ui_import_validate($form, &$form_state) {
    $view = '';
    views_include('view');
    // Be forgiving if someone pastes views code that starts with '<?php'.
    if (substr($form_state['values']['view'], 0, 5) == '<?php') {
        $form_state['values']['view'] = substr($form_state['values']['view'], 5);
    }
    ob_start();
    eval($form_state['values']['view']);
    ob_end_clean();
    if (!is_object($view)) {
        return form_error($form['view'], t('Unable to interpret view code.'));
    }
    if (empty($view->api_version) || $view->api_version < 2) {
        // Check for some value that would only exist on a Views 1 view.
        if (isset($view->url) || isset($view->page) || isset($view->block)) {
            views_include('convert');
            $view = views1_import($view);
            drupal_set_message(t('You are importing a view created in Views version 1. You may need to adjust some parameters to work correctly in version 2.'), 'warning');
        }
        else {
            form_error($form['view'], t('That view is not compatible with this version of Views.'));
        }
    }
    elseif ($view->api_version > views_api_version()) {
        form_error($form['view'], t('That view is created for the version @import_version of views, but you only have @api_version', array(
            '@import_version' => $view->api_version,
            '@api_version' => views_api_version(),
        )));
    }
    // View name must be alphanumeric or underscores, no other punctuation.
    if (!empty($form_state['values']['name']) && preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
        form_error($form['name'], t('View name must be alphanumeric or underscores only.'));
    }
    if ($form_state['values']['name']) {
        $view->name = $form_state['values']['name'];
    }
    $test = views_get_view($view->name);
    if (!$form_state['values']['name_override']) {
        if ($test && $test->type != t('Default')) {
            form_set_error('', t('A view by that name already exists; please choose a different name'));
        }
    }
    else {
        if ($test->vid) {
            $view->vid = $test->vid;
        }
    }
    views_include('analyze');
    if ($messages = views_analyze_view($view)) {
        foreach ($messages as $message) {
            if ($message['type'] == 'error') {
                drupal_set_message($message['message'], 'error');
            }
        }
    }
    $form_state['view'] =& $view;
}