2

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.

6
  • 2
    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.
    – Hudri
    Jan 19, 2023 at 8:55
  • 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
    Jan 31, 2023 at 8:47
  • 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.
    – MegaChriz
    Jan 31, 2023 at 8:56
  • Thanks @MegaChriz , I will join the channel
    – Siva
    Feb 1, 2023 at 12:17
  • 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.
    – Siva
    Feb 1, 2023 at 12:18

1 Answer 1

0

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
2
  • Thanks @Blade Binos, Surely I will try this, since I'm not getting any code example for D9 Feeds module.
    – Siva
    Jan 20, 2023 at 7:41
  • Great. After, please report the results. Jan 20, 2023 at 14:12

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