You are here

function flag_import_form_validate in Flag 7.3

Same name and namespace in other branches
  1. 6.2 includes/flag.export.inc \flag_import_form_validate()
  2. 7.2 includes/flag.export.inc \flag_import_form_validate()

Validate handler; Import a flag.

File

includes/flag.export.inc, line 100
Import/Export functionality provided by Flag module.

Code

function flag_import_form_validate($form, &$form_state) {
  $flags = array();
  ob_start();
  eval($form_state['values']['import']);
  ob_end_clean();
  if (!isset($flags) || !is_array($flags)) {
    form_set_error('import', t('A valid list of flags could not be found in the import code.'));
    return;
  }

  // Create the flag object.
  foreach ($flags as $flag_name => $flag_info) {

    // Backward compatibility: old exported flags have their names in $flag_info
    // instead, so we use the += operator to not overwrite it.
    $flag_info += array(
      'name' => $flag_name,
    );
    $new_flag = flag_flag::factory_by_array($flag_info);

    // Give new flags with the same name a matching FID, which tells Flag to
    // update the existing flag, rather than creating a new one.
    if ($existing_flag = flag_get_flag($new_flag->name)) {
      $new_flag->fid = $existing_flag->fid;
    }
    if ($errors = $new_flag
      ->validate()) {
      $message = t('The import of the %flag flag failed because the following errors were encountered during the import:', array(
        '%flag' => $new_flag->name,
      ));
      $message_errors = array();
      foreach ($errors as $field => $field_errors) {
        foreach ($field_errors as $error) {
          $message_errors[] = $error['message'];
        }
      }
      form_set_error('import', $message . theme('item_list', array(
        'items' => $message_errors,
      )));
    }
    else {

      // Save the new flag for the submit handler.
      $form_state['flags'][] = $new_flag;
    }
  }
}