Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ExpressionEngine PHP Export into JSON
<?php
$channel = $_GET['id'];
$content = array();
$fields_query = $this->EE->db->query("SELECT * FROM exp_channel_fields LEFT JOIN exp_channels ON exp_channel_fields.group_id = exp_channels.field_group WHERE exp_channels.channel_id = '$channel'");
$entries_query = $this->EE->db->query("SELECT * FROM exp_channel_data cd INNER JOIN exp_channel_titles ct ON cd.entry_id = ct.entry_id WHERE cd.channel_id = '$channel'");
$fields = $fields_query->result_array();
foreach ($entries_query->result_array() as $id => $row) {
$content[$id] = array();
$content[$id]['title'] = $row['title'];
$content[$id]['url_title'] = $row['url_title'];
$content[$id]['status'] = $row['status'];
$content[$id]['enabled'] = ($row['status'] == 'closed') ? 0 : 1;
$content[$id]['entry_date'] = ($row['entry_date']) ? date('Y-m-d H:i:s', $row['entry_date']) : '';
$content[$id]['expiration_date'] = ($row['expiration_date']) ? date('Y-m-d H:i:s', $row['expiration_date']) : '';
$content[$id]['author_id'] = $row['author_id'];
// Handle category
$categories = $this->EE->db->query("SELECT * FROM exp_category_posts c JOIN exp_categories cp ON c.cat_id = cp.cat_id WHERE entry_id = ".$row['entry_id'])->result_array();
if ($categories) {
foreach ($categories as $category) {
$content[$id]['category_' . $category['group_id']][] = $category['cat_name'];
}
}
foreach ($fields as $field) {
// Support for P&T Matrix field
if ($field['field_type'] == 'matrix') {
$query = $this->EE->db->query("SELECT * FROM exp_matrix_cols WHERE field_id=".$field['field_id']);
foreach ($query->result_array() as $matrix_col) {
$query = $this->EE->db->query("SELECT * FROM exp_matrix_data WHERE field_id=".$field['field_id']." AND entry_id=".$row['entry_id']." ORDER BY row_order");
foreach ($query->result_array() as $key => $matrix_row) {
// Support for File field
if ($matrix_col['col_type'] == 'file') {
$this->EE->load->library('file_field');
$fileInfo = $this->EE->file_field->parse_field($matrix_row['col_id_'.$matrix_col['col_id']]);
$content[$id][$field['field_name']][$key][$matrix_col['col_name']] = $fileInfo['url'];
} else {
// Any other field
$content[$id][$field['field_name']][$key][$matrix_col['col_name']] = $matrix_row['col_id_'.$matrix_col['col_id']];
}
}
}
// Support for P&T Playa field
} elseif ($field['field_type'] == 'playa') {
// Change from "[93] [my-category] My Category" = "My Category"
$value = preg_replace("/\[[^]]+\]/", '', $row['field_id_' . $field['field_id']]);
$content[$id][$field['field_name']] = trim($value);
// Support for File field
} elseif ($field['field_type'] == 'file') {
$this->EE->load->library('file_field');
$fileInfo = $this->EE->file_field->parse_field($row['field_id_' . $field['field_id']]);
$content[$id][$field['field_name']] = $fileInfo['url'];
// Support for P&T Assets field
} elseif ($field['field_type'] == 'assets') {
$value = $row['field_id_' . $field['field_id']];
$content[$id][$field['field_name']] = $value;
} else {
// Any other field
$content[$id][$field['field_name']] = $row['field_id_' . $field['field_id']];
}
}
}
echo json_encode($content);
?>
@Nav33d

This comment has been minimized.

Copy link

commented Jul 18, 2017

@engram-design Is there a reason that image field is only returning the name of the image instead of the path like it is in the database {filedir_1}square/people-profile-pics/anna-staff.jpg.? It only returns anna-staff.jpg in that case.

Many thanks

@engram-design

This comment has been minimized.

Copy link
Owner Author

commented Jul 19, 2017

@Nav33d This script assumes you'll copy all the assets from the site manually, and place them in your folders in Craft. Running Index Assets will scan the file system (for each asset source) and add them to the database. Now with the assets it Craft, you only need to supply the filename for Feed Me to find the correct asset in Craft.

Essentially, I didn't want to complicate this script by adding find/replace functionality to swap out the file directories with what they should be. I could add this in if you like.

EDIT: Actually, I'm not sure I follow. My own testing shows a File file with an image will output {filedir_2}JCW1115-695.jpg and not JCW1115-695.jpg

@engram-design

This comment has been minimized.

Copy link
Owner Author

commented Aug 22, 2017

@Nav33d If you're interested, I've now added auto-processing of the URL to return its final URL, as opposed to just what's stored in EE.

@alexmglover

This comment has been minimized.

Copy link

commented Jan 11, 2018

How would you recommend handling the {page_xx} vars in the content?

This is great by the way, thank you.

@akukral

This comment has been minimized.

Copy link

commented Feb 27, 2018

Is there a way to auto-process asset names in a WYGWAM or Rich Text filed the same way you did with the File?
I'm getting embedded images like:

<img src="{assets_16862:http://siteurl.com/press/sl_3.jpg}" style="width:794px;height:448px;”>

But would love to instead receive:

<img src=“http://siteurl.com/press/sl_3.jpg#asset:16862" style="width:794px;height:448px;”>

or even:

<img src=“http://siteurl.com/press/sl_3.jpg" style="width:794px;height:448px;”>

I also second @alexmglover question for recommendations for dealing with {page_xx} links. Unless you think a Craft global find and replace would work.

@nathanedwards

This comment has been minimized.

Copy link

commented Dec 19, 2018

We're running through this script to migrate from EE to Craft and this script has been very useful, thanks.

We had "grid" fields though, which weren't exporting properly.

I don't have much Expression Engine experience, so I don't know if I have done this 'right' or not, I just know my resulting JSON looks okay at this point. I thought it might be helpful to some people...

I am well aware it's going to fail with asset fields, or anything more than just a string-in-a-field, lucky for us we weren't using it for more than that.

} elseif ($field['field_type'] == 'grid') {
    
    $labelsQuery = $this->EE->db->query("SELECT * FROM exp_grid_columns WHERE field_id=".$field['field_id']);
    $labels = $labelsQuery->result_array();
    
    $q = "SELECT * FROM exp_channel_grid_field_".$field['field_id']." WHERE entry_id=".$row['entry_id']." ORDER BY row_order ASC";
    $fieldQuery = $this->EE->db->query( $q );
    
    $gridrows = $fieldQuery->result_array();
    
    $result = [];
    foreach ( $labels as $label_id => $label ) {
        
        $rowValues = [];
        
        foreach ( $gridrows as $id => $gridrow ) {
            $val = $gridrow[ 'col_id_' . $label['col_id'] ];
            $result[$id][ $label['col_label'] ] = $val;
        }
    }
    
    $content[$id][$field['field_name']] = $result;
}
@Rocketpilot

This comment has been minimized.

Copy link

commented Apr 30, 2019

@engram-design this looks very useful. I just demoed on a big content site and the only issue I'm seeing right now is that it won't output anything other than "0" for relationship fields. Even just the output text value would be better than that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.