Last active
February 23, 2019 18:17
-
-
Save DanLaufer/3a3e7883bd4e4c88e0cbabd04cdddd45 to your computer and use it in GitHub Desktop.
Drupal 8 - Export content as JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$query = \Drupal::entityQuery('node'); | |
$node_ids = $query | |
->condition('status', 1) | |
->exists('field_sidebar') | |
->execute(); | |
$all_nodes_JSON = []; | |
foreach($node_ids as $node_id) { | |
$node = \Drupal\node\Entity\Node::load($node_id); | |
$sidebar_paragraphs = $node->get('field_sidebar')->referencedEntities(); | |
foreach($sidebar_paragraphs as $parag) { | |
if($parag->getParagraphType()->id == 'related_links') { | |
$related_links = $parag->get('field_related_links')->referencedEntities(); | |
$related_nids = []; | |
foreach($related_links as $linked_entity) { | |
$related_node_JSON = [ | |
'id' => $linked_entity->id(), | |
'url' => 'https://www.mysite.com/node/'.$linked_entity->id() | |
]; | |
array_push($related_nids, $related_node_JSON); | |
} | |
$this_node_JSON = [ | |
'nid' => $node_id, | |
'title' => $node->getTitle(), | |
'type' => $node->getType(), | |
'url' => 'https://www.mysite.com/node/'.$node_id, | |
'related_links' => $related_nids | |
]; | |
array_push($all_nodes_JSON, $this_node_JSON); | |
break; | |
} | |
} | |
} | |
$final_JSON = json_encode($all_nodes_JSON); | |
print_r($final_JSON); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment