Created
October 4, 2016 19:43
-
-
Save heyrocker/bf02b43c2343f13bc11f37607f6dc7c7 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
namespace Drupal\csv_content_import\Forms; | |
use Drupal\Core\Form\FormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Component\Utility\UrlHelper; | |
use Drupal\node\Entity\Node; | |
use Symfony\Component\Validator\Constraints\Null; | |
use Drupal\menu_link_content\Entity\MenuLinkContent; | |
/** | |
* Created by PhpStorm. | |
* User: humingtang | |
* Date: 7/18/16 | |
* Time: 10:58 AM | |
*/ | |
/** | |
* Contribute form. | |
*/ | |
class CSVImportForm extends FormBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFormId() { | |
return "simple-csv-import-content-form"; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
$form['csvimport_file'] = array( | |
'#type' => 'file', | |
'#title' => t('CSV Upload'), | |
'#description' => t('Upload a file, allowed extensions: csv'), | |
); | |
$form['submit'] = array( | |
'#type' => 'submit', | |
'#value' => t('Submit'), | |
); | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function validateForm(array &$form, FormStateInterface $form_state) { | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitForm(array &$form, FormStateInterface $form_state) { | |
$result = file_save_upload('csvimport_file',['file_validate_extensions' => 'csv'],"public://"); | |
$realpath = drupal_realpath($result[0]->getFileUri()); | |
//file saved to public, now start processing | |
//Step 1: read the FILE line BY LINE, OH YEAH! | |
//Step 2: process each line and create a CONTENT for each! OH YEAH! | |
//Step 3: DONE! OH YEAH! | |
$menu_hierarchy = array(); | |
$last_column = 0; | |
$short_title_column = 10; | |
$content_type_column = 11; | |
$file = fopen($realpath,"r"); | |
while(!feof($file)) | |
{ | |
$node_elements = array(); | |
$dataline = fgets($file); | |
print_r($dataline); | |
$record = explode(",",$dataline); | |
$record = array_filter($record); | |
$title_column = key($record); | |
if ($title_column == $last_column-1) { | |
array_pop($menu_hierarchy); | |
} elseif ($title_column == $last_column) { | |
// do nothing | |
} elseif ($title_column == $last_column +1) { | |
array_push($menu_hierarchy, $last_menu_item); | |
} else { | |
die("error"); | |
} | |
$node_elements['field_short_title'] = $record[$short_title_column]; | |
$node_elements['type'] = 'article'; | |
$node_elements['title'] = $record[$title_column]; | |
$node = Node::create($node_elements); | |
$node->save(); | |
$link = MenuLinkContent::create(array( | |
'menu_name' => 'main', | |
'weight' => $weight + 10, // need to figure this out | |
'title' => $node_elements['title'], | |
'bundle' => 'menu_link_content', | |
'description' => '', | |
)); | |
$link->link = 'entity:node/' . $node->id(); | |
if (!empty($menu_hierarchy)) { | |
$link->parent = end(array_values($menu_hierarchy)); | |
} | |
$link->save(); | |
$last_menu_item = 'menu_link_content:' . $link->uuid->value; | |
$last_column = $title_column; | |
} | |
fclose($file); | |
//delete file after processing | |
unlink($realpath); | |
drupal_set_message(t('Sites are imported'), 'status'); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment