Score:2

Feeds Documentation for importing data programatically

us flag

Team,

I need a help.

I was surfing internet, but couldn't get a luck to get a working example/documentation to import data programmatically and store into our custom tables.

I'm using Drupal 9 version and feeds module, and for importing data related to users, content type, feeds modules help me a lot. But we have some custom tables as well, and stuck here to import. I have followed below link, but have stuck in coding part to get & store the values in custom table.

https://www.drupal.org/docs/contributed-modules/feeds/creating-a-custom-feeds-workflow

And, everywhere, I'm getting the links for import using front-end, but not via programatically.

https://ostraining.com/blog/drupal/feeds/

https://www.specbee.com/blogs/how-import-external-feeds-your-drupal-9-website-feeds-module

https://metadrop.net/en/articles/import-your-content-feeds-drupal

I have asked some of my known Drupalers, and in my circle, we are not having a handy documentation or code example for importing data and storing in custom table. My friends recommended some other contrib modules for this case, but I would like to know if I can achieve this using Feeds.

I just need to import some data using csv file and store it in custom tables. Please do share if any working code example is there with anybody or any links over internet you have seen which I missed, please do share, kindly help.

Thanks in advance.

ru flag
I’m voting to close this question because questions asking to find, recommend, or suggest tutorials, documentation, or other off-site resource are off-topic.
Balde Binos avatar
br flag
I’m voting to NOT closing this question because I to struggled to find some way to achieve what @Siva is looking for, even after a searching an insane search for information. @Siva’s question seems to me a perfectly valid question,
MegaChriz avatar
cn flag
I've got this question before (but can't find where). Anyway, I recommended then to use phpMyAdmin for importing data into custom tables if it was just being an one-time import. For a solution within Drupal you could consider developing the Data module further. That module used to have Feeds integration in older versions of it, but not yet in D8+.
MegaChriz avatar
cn flag
I do agree that the documentation is lacking on creating custom processors. But that's because I didn't have a need myself yet to create them. So I can fully understand that can be a daunting task, especially if you're not so familiar with the Feeds code base. If you are interested to get more knowlegdeable about Feeds, you could join our weekly meeting on Slack in the #feeds channel.
Siva avatar
us flag
Thanks @MegaChriz , I will join the channel
Siva avatar
us flag
In my case, I want to implement this import functionality and provide permission to some roles who will be importing the data periodically. So, I'm searching from Drupal side solution.
MegaChriz avatar
cn flag
@Siva Okay, cool! Meeting will start at 19:00 UTC today. I think that the first step is to revive the Data module and make it compatible with Drupal 10. After that we're going to need to develop some Feeds plugins. We need FeedsTarget and maybe FeedsProcessor plugins. I can give guidance along the way.
Score:0
br flag

I will assume a scenario where you just need to import some simple data from a csv file into Drupal. No multi value fields, no reference fields (including image fields), etc. Such scenario would require a much complicated script.

The solution I’m suggesting (bellow) assumes that you have drush installed and you are willing to run a php script under drush.

Something like the following might work:

<?php

// php -d memory_limit=-1 vendor/bin/drush php:script import_nodes.php

// general options
$csv_file_name = '/the/path/to/file.csv';
$type = 'some_type';

// get csv
$items_to_import = get_array_from_csv ($csv_file_name);

// start import
foreach ($items_to_import as $item) {

        // set title
        $title = $item['title'];

        // create node
        $node = \Drupal::entityTypeManager()
                    ->getStorage('node')
                    ->create([
                        'type' => $type,
                        'title' => $title,
                    ]);

        // set field_foo
        $field_foo = $item['field_foo'];
        $node->set('field_foo', $field_foo);

        // set field_bar
        $field_bar = $item['field_bar'];
        $node->set('field_bar', $field_bar);

        $node->save();
        unset ($node);

}

function get_array_from_csv ($filePath) {

    $dataArray = [];

    $rows = array_map ('str_getcsv', file ($filePath));
    $header = array_shift ($rows);

    foreach($rows as $row) {

        $dataArray[] = array_combine ($header, $row);

    }

    return $dataArray;

}

Run it (from your site directory) with the command:

php vendor/bin/drush php:script import_nodes.php

Or (if on low allocated memory for php) with the command:

php -d memory_limit=-1 vendor/bin/drush php:script import_nodes.php
Siva avatar
us flag
Thanks @Blade Binos, Surely I will try this, since I'm not getting any code example for D9 Feeds module.
Balde Binos avatar
br flag
Great. After, please report the results.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.