Forked from tommy-thomas/Example Drupal Migration Sub Class for OI HTML Content
Created
November 6, 2015 14:00
-
-
Save dwaghmare/3f415e0f891be3c381d0 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 | |
/* | |
* https://gist.github.com/marktheunissen/2596787 | |
*/ | |
function oi_migrate_migrate_api() { | |
$api = array( | |
'api' => 2, | |
'groups' => array( | |
'oi' => array( | |
'title' => t('OI Migrations'), | |
'default_format' => 'filtered_html', | |
), | |
), | |
'migrations' => array( | |
'OIPage' => array( | |
'class_name' => 'OIPageMigration', | |
'group_name' => 'oi', | |
), | |
), | |
); | |
return $api; | |
} | |
/** | |
* | |
* Dynamic migration for html files from server | |
* @author tommyt | |
* | |
*/ | |
class OIPageMigration extends Migration { | |
public $base_dir; | |
public $testMenuCount = 0; | |
/** | |
* Constructor. | |
*/ | |
public function __construct() { | |
parent::__construct(); | |
// dercription | |
$this->description = t('POC for migrating html content into Drupal.'); | |
// A map of source HTML filename -> destination node id. | |
$this->map = new MigrateSQLMap($this->machineName, | |
array( | |
'sourceid' => array( | |
'type' => 'varchar', | |
'length' => 255, | |
'not null' => TRUE, | |
) | |
), | |
MigrateDestinationNode::getKeySchema() | |
); | |
// The source fields. | |
$fields = array( | |
'title' => t('Title'), | |
'body' => t('Body'), | |
'taxonomy_reference' => t('Section taxonomy reference'), | |
'source_path' => t('Source Path'), | |
'path' => t('Node path alias'), | |
'menu' => t('Menu item'), | |
'uid' => t('User id'), | |
); | |
// Since the base directory of the HTML files can change depending on the | |
// environment, we keep it in a variable. There is no interface for this, | |
// set it using drush vset. | |
//$this->base_dir = variable_get(my_html_source_path, ''); | |
//$xml_folder = DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_example') . '/xml/'; | |
//$this->base_dir = '/html_source/research/projects'; | |
$this->base_dir = DRUPAL_ROOT . '/' . drupal_get_path('module', 'oi_migrate') . '/html_source/'; | |
// Match HTML files. | |
// $regex = '/.*\.html/'; | |
$regex = '/(.*\.htm$|.*\.html$|.*\.shtml$)/i'; | |
// The source of the migration is HTML files from the old site. | |
$list_files = new MigrateListFiles(array($this->base_dir), $this->base_dir, $regex); | |
$item_file = new MigrateItemFile($this->base_dir); | |
$this->source = new MigrateSourceList($list_files, $item_file, $fields); | |
// The destination is the basic page content type. | |
$this->destination = new MigrateDestinationNode('page'); | |
// Map the fields, pretty straightforward in this case. | |
$this->addFieldMapping('uid', 'uid'); | |
$this->addFieldMapping('title', 'title'); | |
$this->addFieldMapping('field_section_taxonomy_id', 'taxonomy_reference'); | |
$this->addFieldMapping('field_source_path', 'source_path'); | |
$this->addFieldMapping('menu', 'menu'); | |
$this->addFieldMapping('body', 'body') | |
->arguments(array('format' => 'full_html')); | |
$this->addFieldMapping('pathauto')->defaultValue(0); | |
} | |
/** | |
* Prepare a row. | |
*/ | |
public function prepareRow($row) { | |
// Set to admin for now. | |
$row->uid = 1; | |
// Create a new SourceParser to handle HTML content. | |
$source_parser = new SourceParser(substr($row->sourceid, 1), $row->filedata); | |
$row->body = $source_parser->getContent(); | |
// The title is the html page title. | |
$row->title = $source_parser->getPageTitle(); | |
// The source_path is the path/filename | |
$row->source_path = $row->sourceid; | |
// experimenting with some string processing | |
$path_array = explode("/", $row->sourceid); | |
$raw_page = count($path_array) >= 0 ? $path_array[count($path_array)-1] : $path_array[0]; | |
$page_array = explode(".",$raw_page); | |
$link_title = $page_array[0] == "index" ? ucfirst($path_array[count($path_array)-2]) : ucfirst($page_array[0]); | |
// path alias | |
$row->path = substr($row->sourceid, 0, strripos($row->sourceid, "/")+1)."/".strtolower($link_title); | |
// add $row to menu | |
$row->menu = array( | |
'link_title' => $link_title, | |
'menu_name' => 'menu-research', | |
'plid' => 0, | |
'enabled' => 1, | |
); | |
} | |
// Post processing of node($page) after import and creation entity | |
public function complete(stdClass $page, stdClass $row) { | |
// dpm($page->nid); | |
$node = node_load( $page->nid ); | |
$path_array = explode("/", $row->sourceid); | |
$taxonomy_id = in_array($path_array[count($path_array)-2], array('research','projects')) ? 4 : 5; | |
$node->field_section_taxonomy_id[$node->language][0]['tid'] = $taxonomy_id; | |
// dpm($node); | |
node_save($node); | |
// $page->field_testpostimportfield[LANGUAGE_NONE][0]['value'] = $page->nid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment