Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set id of CSV migration row based on line number #10

Open
eiriksm opened this issue Jul 5, 2020 · 5 comments
Open

Set id of CSV migration row based on line number #10

eiriksm opened this issue Jul 5, 2020 · 5 comments

Comments

@eiriksm
Copy link
Owner

eiriksm commented Jul 5, 2020

https://eiriksm.dev/set-id-csv-migration-row-number

@DLatty-NJT
Copy link

Why is ID mandatory? What if i just want it auto-incremented? Seems there will be many collisions if you are required to specify. For certain data, I simply want the next available ID and on. Would I really have to write code to get the first node desc id - in order to do this? Seems like ids[] or similar should be a function.

@eiriksm
Copy link
Owner Author

eiriksm commented Mar 19, 2021

👋 Thanks for commenting!

The id in question is the migration row Id. In the article it has no relationship to the node id. You have to have a migration row Id to be able to re-run migrations and only migrate changes, for example. Sorry if that was not clear 😬🤓

@batigolix
Copy link

batigolix commented Feb 28, 2022

I am using drupal 9.3 & migrate_plus 5 and I cannot get this working. Do the examples in your blog work with the most recent versions?

I had to changed "keys" for "ids".:
You must declare "ids" as a unique array of fields in your source settings.
This may be because of recent changes in the csv source plugin or drupal 9 core migrate.

Also the ID you set in prepareRow() is not recognized. The migration fails before prepareRow() because it looks for a ID column in the CSV :

[error] Migratie mislukt met bronplugin-melding: 'my_id' is defined as a source ID but has no value. in /var/www/html/web/core/modules/migrate/src/Row.php regel 107
(apologies for the partial dutch message)

@eiriksm
Copy link
Owner Author

eiriksm commented Mar 2, 2022

I see. This was indeed tested on earlier versions so I would have to look at it again when I get the time, to update the article. If you find out what changed in the meantime, it would be fantastic if you are able to share that! 🤓

@AIeksandr
Copy link

AIeksandr commented Nov 25, 2023

If somebody still searching for resolving for latest module.

id: my_module_categories_csv
label: My module categories
migration_group: my_module
source:
  # We will use a custom source plugin, so we can set the 
  # ID from there.
  plugin: my_module_categories_csv
  header_row_count: 1
  ids:
    - id
  delimiter: ';'
  # ... And the rest of the file 
<?php

namespace Drupal\my_module\Plugin\Migrate\source;

use Drupal\migrate_source_csv\Plugin\migrate\source\CSV;

/**
 * Source plugin for Categories in csv.
 *
 * @MigrateSource(
 *   id = "my_module_categories_csv"
 * )
 */
class CategoriesCsv extends CSV {

  private $row = 0;

  /**
   * {@inheritdoc}
   */
  public function next() {
    $this->configuration[end($this->configuration['ids'])] = ++$this->row;
    parent::next();
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants