~/drupal/drupal-code-export-content-nodes-json.md

Drupal 7 - Code for Exporting all your content nodes in json files

2 min readMay 22, 2020by Gorav Singal#drupal-7#drupal#php
// TL;DR

Query all node IDs, load each node with node_load, and write the serialized data to JSON files for later import into Drupal 8.

Drupal 7 - Code for Exporting all your content nodes in json files

Introduction

When I migrated all of my drupal-7 website to drupal-8, I wrote automation to import all my old nodes into new drupal-8. Drupal did not provide any best way to migrate all of my nodes. So, I wrote this automation.

In this post, I will describe how I exported all my nodes into JSON files. In another post, I will explain how I imported them, with their URL alias intact.

Php code

{% highlight php linenos %}

name; } return $txNames; } function getUser($uid) { $u = user_load($uid); return $u->mail; } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// // Execution starts from here $nid = 1905; $howMany = 100; $till = $nid + $howMany; $dir = '/var/www/html/sites/default/files/exports/nodes'; for ($i=$nid; $i < $till; $i++) { $nd = node_load($i); if (!isset($nd) || !isset($nd->nid)) { continue; } $nd->user = getUser($nd->uid); //switch node type switch($nd->type) { case 'add_drawings': $nd->field_drawing_tags = getTermNames($nd->field_drawing_tags); $nd->field_drawing_category = getTermNames($nd->field_drawing_category); $nd->field_drawing_software = getTermNames($nd->field_drawing_software); break; case 'article': $nd->field_tags = getTermNames($nd->field_tags); $nd->field_article_category = getTermNames($nd->field_article_category); break; case 'page': case 'youtube_videos': //nothing special break; case 'my_book_reference': $nd->field_book_category = getTermNames($nd->field_book_category); break; case 'my_toolbox_reference': $nd->field_tool_category = getTermNames($nd->field_tool_category); break; default: print "\nHow on earth this can be possible! Type: ".$nd->type."\n"; } $alias = drupal_get_path_alias('node/'.$i); $folder = $dir . '/' . $nd->type; if (!file_exists($folder)) { mkdir($folder, 0777, true); } $filepath = $folder . '/' . $nd->nid . '.json'; $nd->alias = $alias; $data = json_encode((array)$nd); //write writeFile($filepath, $data); print "Written nid: ".$nd->nid.', alias: '.$alias."\n"; //print $data."\n\n"; } {% endhighlight %} ## Explanation of code See the comment which says: *Execution starts from here*. Line no. 33 Note: I ran this code through the devel/PHP module. So, this has a small timeout value when running a PHP script. So, I'm running this with 100s of nodes at a time. So, that loop where I'm starting nid=1905, you can start it from *1* *Line no 37*, the directory where I want to save the JSON files. *Line no 45*, Fetching author of the node and saving user details in an attribute in JSON. *Line no 48*. A switch case on my content types. *Line no 50*, saving term name. As in new DB. The term id will not be the same, but my taxonomy term name was unique in a vocabulary. *Line no 77*, saving the alias of node. The rest of the code is pretty easy to understand.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Drupal 7&#58; How to save a node programmatically and add an image field from a public URL

Drupal 7&#58; How to save a node programmatically and add an image field from a public URL

Note: I have public URLs of these images, which I want to save. return…

Drupal&#58; How to block a user by email programatically

Drupal&#58; How to block a user by email programatically

Many times, while administering your drupal website, you must have encountered…

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

List all the Node ids which do not have images from my domain

List all the Node ids which do not have images from my domain

I use drupal-7 in my website. I used to write articles and put images in that…

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…