drupal/import_csv

There is no license information available for the latest version (1.0.x-dev) of this package.

Create nodes, users, taxonomy terms, etc. from CSV files.

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Type:drupal-recipe

1.0.x-dev 2024-10-23 22:41 UTC

This package is not auto-updated.

Last update: 2024-11-22 05:08:08 UTC


README

This recipe lets you import data into your Drupal site from CSV files (plain-text spreadsheets).

Installation

Installation is standard. You need Drupal core 10.3.0 or later. If you have installed drush, and this recipe is under the recipes/ directory, then you can install Drupal and apply the recipe:

drush site:install
drush recipe recipes/import_csv

To do

For now, if you use this recipe with Drupal 11, then you also have to patch the Migrate Source UI (migrate_source_ui) module. this issue for Migrate Source UI is now fixed, and it should be included in the next release.

In order to assign users and taxonomy terms from the other two migrations, the node migration requires the changes from this issue for Migrate Source CSV.

Usage

  1. Prepare an input file.
  2. Log in as an admin user.
  3. Visit Administration > Content > Migrate source UI (/admin/content/migrate_source_ui).
  4. Choose a migration from the select list.
  5. Upload your CSV file.
  6. Submit the form (Migrate).

Any spreadsheet program (or Google Sheets) can export in CSV format. For a friendly view of the examples, you can save them in .csv files and open them in a spreadsheet program.

The sections below (corresponding to the options in Step 4) specify the required column headers.

The id column

Each option has a required id column. Internally, Drupal keeps track of the values in this column. If you ever import an item using the same migration and the same id, then Drupal will either update the existing item or skip that row of data, depending on whether you select the "Update existing records" option before submitting the form.

Nodes

The source is a CSV file with the following columns:

  • id (required)
  • type (required)
  • title (required)
  • body (optional, defaults to '')
  • text_format (optional, defaults to basic_html)
  • user_id (optional, defaults to 0 for Anonymous)
  • tag_ids (optional, colon-separated)

The type should be the machine name of a content type, such as page or article (as in the Standard profile).

This recipe does not ensure that the basic_html text format is available. Either make sure that is available (e.g., by applying the core/recipes/basic_html_format_editor recipe) or treat the text_format column as required.

The user_id column accepts values from the id column of the import_csv_users migration, and the tag_ids column accepts values from the id column of the import_csv_terms migration. If you want to assign more than one tag, then separate them with colons (:).

The migration adds the tags to field_tags. If there is no such field on the type content type, then this has no effect. You are responsible for choosing terms from vocabularies that are allowed in the field_tags configuration. The Standard profile adds field_tags to the Article (article) content type, and configures it to use terms from the Tags (tags) vocabulary, but your site may be different.

For example, if you

  1. Install the Standard profile.
  2. Apply this recipe.
  3. Import terms and users from the example CSV files in the sections below.
  4. Import the following CSV file

then you will get two Basic page (page) nodes (authored by Anonymous and alice) and one Article (article) node (authored by bob) with two terms from the Tags vocabulary.

id,type,title,body,text_format,user_id,tag_ids
1,page,First Test Page,,,,
2,page,Second Test Page,Some <strong>bold</strong> and <em>italicized</em> text.,full_html,alice,
3,article,Test Article,,,bob,1:2

Taxonomy terms

The source is a CSV file with the following columns:

  • id (required)
  • name (required)
  • description (optional, defaults to none)
  • vocabulary (optional, defaults to tags)
  • parent (optional, defaults to none)

This recipe installs the same Tags vocabulary (machine name tags) as the Standard profile. That is used as the default. To create a term in another vocabulary, enter the machine name of the vocabulary in the vocabulary column.

The optional parent column references values from the id column in this or a previously imported CSV file.

For example, if you create the Test Vocabulary (machine name test_vocabulary), then you can import this CSV file. You will get two terms in the Tags vocabulary and five terms in the Test Vocabulary. Term 4 will have two child terms and one grandchild term.

id,name,description,vocabulary,parent
1,Term 1,,,
2,Term 2,Description of Term 2,tags,
3,Term 3,Description of Term 3,test_vocabulary,
4,Term 4,,test_vocabulary,
5,Term 5,,test_vocabulary,4
6,Term 6,,test_vocabulary,4
7,Term 7,,test_vocabulary,6

Users

The source is a CSV file with the following columns:

  • id (required)
  • email (required)
  • status (optional, defaults to Active)
  • roles (optional, colon-separated, defaults to none)
  • created (optional, defaults to current time)

The id value is also used as the username, so it must be a valid Drupal username.

If there is already a user account with the same username, then that row of the CSV file will be skipped, and the migration will save a migration message.

The status column should be "Active" or "Blocked" (or empty).

This recipe does not assume any particular roles exist. The roles column should contain machine names of roles on the site. For example, if you install the Standard profile, then use content_editor for the "Content editor" role. (Actually, the migration will convert the values to valid machine names, so "Content editor" also works.)

The created column can be any date format supported by the PHP strtotime() function.

For example, if your site has roles "Role 1", "Role 2", and "Role 3", then you can import this CSV file. The alice and bob accounts will have no user roles (except for the Authenticated role that every account gets). The actor account will have three roles. Its created time will be when the CSV file is imported.

id,email,status,roles,created
alice,alice@example.com,Active,,"July 17, 1978"
bob,bob@example.com,Blocked,,2011-01-05
actor,actor@example.com,,role_1:role_2:role_3,

Behind the scenes

These contrib modules do the real work: