blog-banner

Create Feed Importer With Custom Module

    Drupal family members would be well aware of the power of the Feeds module. More than many times, thereof has served us in creating new content with less work and time. Recently I looked at the feeds module from an unseen angle, "How to create a feeds importer from my custom module?".  To make my requirement clear please visit Quiz Questions Import module's page and if possible use this to get a clear picture of what this blog can do for you.

    The Quiz Questions Import module on enabling creates a list of feeds importers, and these importers can be used to import content in CSV format. I don't want to get too deep into the concept of creating such a module but will limit it to the exact API needed to make this work.

    1. Create a new custom feed importer in Structure -> Feeds importer
    2. Select the Fetcher, Parser, and Preprocessor as per your requirement
    3. Once done, export this feeds configuration (Which can be found in the export tab)
    4. Paste this code into your custom module

    Many who have gone through the above steps would have failed to attain success, In simple terms of enabling this custom module, you would not find any importer being created. To accomplish all we need is hook_ctools_plugin_api().

    1. /**
    2.  * Implements hook_ctools_plugin_api()
    3.  */
    4. function qq_import_ctools_plugin_api() {
    5.   $args = func_get_args();
    6.   $module = array_shift($args);
    7.   $api = array_shift($args);
    8.   if ($module == "feeds" && $api == "feeds_importer_default") {
    9.     return array("version" => 1);
    10.   }
    11. }

    The above piece of code has been taken from the Quiz Question Import module, replace qq_import with your module's name. Apart from that, no other real change is needed. Now disable and if needed uninstall your custom module and enable it again. Voila, feed importer in /import page.