1

I have been using a custom module that was posted here:

https://www.drupal.org/node/1875456

I slightly modified this module and created two copies of it, as I have two views and planning to add third one but it currently doesn't work properly so let me explain:

dvm_reports module#1 for Denmark

function dvm_reports_views_data_export_batch_alter(&$batch, &$batch_redirect) {
  $view = views_get_view($batch['produkty_denmark_xml']);

  // Add in another operation that will store the file on the server in a location
  // of our choosing.
  $batch['operations'][] = array(
    'dvm_reports_views_data_export_batch_finish', array($batch['eid']),
  );
}

function dvm_reports_views_data_export_batch_finish($eid) {
  $export = views_data_export_get($eid);
  $view = views_data_export_view_retrieve($eid);
  $view->set_display($export->view_display_id);
  $view->display_handler->batched_execution_state = $export;
  $view->display_handler->remove_index();

  // Get path to temp file
  $temp_file = $view->display_handler->outputfile_path();
  file_unmanaged_copy($temp_file, 'public://denmark.xml', FILE_EXISTS_REPLACE);
}

It starts to generate the file correctly when I go to domain.com/denmark

dvm_reports module#2 for Sweden (I copied the denmark module and renamed it from "dvm_reports" to "dvm_reports2")



function dvm_reports2_views_data_export_batch_alter(&$batch, &$batch_redirect) {
  $view = views_get_view($batch['produkty_json']);

  // Add in another operation that will store the file on the server in a location
  // of our choosing.
  $batch['operations'][] = array(
    'dvm_reports2_views_data_export_batch_finish', array($batch['eid']),
  );
}

function dvm_reports2_views_data_export_batch_finish($eid) {
  $export = views_data_export_get($eid);
  $view = views_data_export_view_retrieve($eid);
  $view->set_display($export->view_display_id);
  $view->display_handler->batched_execution_state = $export;
  $view->display_handler->remove_index();

  // Get path to temp file
  $temp_file = $view->display_handler->outputfile_path();
  file_unmanaged_copy($temp_file, 'public://produkty.json', FILE_EXISTS_REPLACE);
}

it starts to generate the file correctly when I go to domain.com/sweden

Problems:

I would like to combine those two modules into one but I don't know how to do it.

Please advice if you can

12
  • What is the actual "requirement" you're trying to resolve with that custom module you stumbled upon? Just a wild guess, something like creating an XML of JSON file to actually export the results of some data you extraced from your Drupal DB usiing a view? If not what is it then instead? Commented Mar 11, 2017 at 11:44
  • Hi, I have created a VIEW using the "views_data_export" mode but because my views have like 9000 nodes I can't just display it as normal link to a "Database views" because it would generate for to long and thats the reason why I'm using this method to save it to a local file
    – Gabe H
    Commented Mar 11, 2017 at 11:47
  • Hi, I never said there isn't a different solution than what I'm trying to do, but it is the only thing I know how to do.. without rebuilding my views from json / xml files and their templates from scratch thats why I'm trying to do it in that way. So completely different approach may be good but may require rebuilding everythink from scratch. I want to create JSON/XML from views_data_export module and it almost works minus the small problems which I don't know how to fix.
    – Gabe H
    Commented Mar 11, 2017 at 12:27
  • Welcome to Drupal Answers :) Asking for general advice on a number of different questions isn't suitable for Q+A. Please ask a single, specific, narrowly-focused question at a time, and rather than asking for general advice, detail exactly what steps you've taken, exactly which small part of the problem you're trying to solve right now, and where that's going wrong so far. That way we'll have a bunch of smaller, easier to address, more generic questions that will help a broader audience than those trying to the exact thing you're trying to do. Many thanks
    – Clive
    Commented Mar 11, 2017 at 12:34
  • Hi thanks for the welcome but.. 1) it is not "General advice" I asked very specific question related to very specific problem and I think I have described the process correctly 2) I pointed out what I'm trying to achieve in points 1,2,3,4 to be more specific, so you don't think anyone would help? :(
    – Gabe H
    Commented Mar 11, 2017 at 13:01

2 Answers 2

0

I suppose you should be able to merge them into one module by first checking if the view is available in the batch (stop otherwise) and provide the filename within this check. Code has not been tested though.

function dvm_reports_views_data_export_batch_alter(&$batch, &$batch_redirect) {
  // Check for Denmark.
  if ($view = views_get_view($batch['produkty_denmark_xml'])) {
    $filename = 'denmark.xml';
  }
  // Check for Sweden.
  elseif ($view = views_get_view($batch['produkty_json'])) {
     $filename = 'producty.json';
  }
  // Check for Norway.
  // ... Code goes here

  // Leave if nothing was found.
  else return;

  // Proceed if something was found.

  // Add in another operation that will store the file on the server in a location
  // of our choosing.
  $batch['operations'][] = array(
    'dvm_reports_views_data_export_batch_finish', array($batch['eid'], $filename),
  );
}

function dvm_reports_views_data_export_batch_finish($eid, $filename) {
  $export = views_data_export_get($eid);
  $view = views_data_export_view_retrieve($eid);
  $view->set_display($export->view_display_id);
  $view->display_handler->batched_execution_state = $export;
  $view->display_handler->remove_index();

  // Get path to temp file
  $temp_file = $view->display_handler->outputfile_path();
  file_unmanaged_copy($temp_file, 'public://'. $filename, FILE_EXISTS_REPLACE);
}

There might however be a bigger problem with your module. I cannot see your hook menu implementation, but if anonymous users are also able to access domain.com/denmark they could DDoS your site. Prevent that.

7
  • Hi Neograph734, thank you for your answer :) it really seems like it should work but it doesn't I mean if I go to /sweden or /denmark it does start generating a view but it doesn't save it in public:// I'm not sure why it may be like that the url looks like this after finishing export: sweden?download=1&eid=28&token=E8TFFtDTQnuGOO8tbeshmy6oN_jfbdPTKWkSQiYjqoA But it doesn't save it to a file I'm not sure why (regarding hook menu it is great idea, but I don't know how to do it as well as you may imagine )
    – Gabe H
    Commented Mar 11, 2017 at 14:35
  • That token already implies there is DDoS protection in place, so if you request the page without the token I suppose it would not generate (but it is out of scope of this question). Could you try replacing if ($view = views_get_view($batch['produkty_denmark_xml'])) with if isset($batch['produkty_denmark_xml'])) { (for all contries)? Loading the view does not seems to be required. Commented Mar 11, 2017 at 14:45
  • I have added it but now I can't activate module I get internal server error 500 whenever I try to enable it, it is possible to enable other module paste.ee/p/BMwNd
    – Gabe H
    Commented Mar 11, 2017 at 15:07
  • That would imply a syntax error in this module... I see I missed a bracket after the if: if (isset($batch['produkty_denmark_xml'])) { Commented Mar 11, 2017 at 15:11
  • 1
    Thank you for your help and effort Neograph734 I really appreciate it when I mentioned it doesn't work I had on mind that "it still doesn't generate a file or files" as my response saying "it still doesn't work" was not clear enough
    – Gabe H
    Commented Mar 11, 2017 at 16:02
0

try this:

function dvm_reports_views_data_export_batch_alter(&$batch, &$batch_redirect) {
  $view = views_get_view($batch['view_name']);

  drupal_set_message($batch['view_name']);

  // Add in another operation that will store the file on the server in a location
  // of our choosing.
  if($batch['view_name']== "produkty_denmark_xml"){

    $batch['operations'][] = array(
    'dvm_reports_views_data_export_batch_finish', array($batch['eid']),
    );
  }else if($batch['view_name']== "produkty_json"){

    $batch['operations'][] = array(
    'dvm_reports_views_data_export_batch_finish2', array($batch['eid']),
    );
  }
}

function dvm_reports_views_data_export_batch_finish($eid) {
  $export = views_data_export_get($eid);
  $view = views_data_export_view_retrieve($eid);
  $view->set_display($export->view_display_id);
  $view->display_handler->batched_execution_state = $export;
  $view->display_handler->remove_index();

  // Get path to temp file
  $temp_file = $view->display_handler->outputfile_path();
  file_unmanaged_copy($temp_file, 'public://denmark.xml', FILE_EXISTS_REPLACE);
}
function dvm_reports_views_data_export_batch_finish2($eid) {
  $export = views_data_export_get($eid);
  $view = views_data_export_view_retrieve($eid);
  $view->set_display($export->view_display_id);
  $view->display_handler->batched_execution_state = $export;
  $view->display_handler->remove_index();

  // Get path to temp file
  $temp_file = $view->display_handler->outputfile_path();
  file_unmanaged_copy($temp_file, 'public://produkty.json', FILE_EXISTS_REPLACE);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.