1
\$\begingroup\$

At first I brute forced the migration to just get it finished. However, the procedural program mixed the events with the venues so I rewrote it, this time using classes which I'm just beginning to use.

function liveimport_import() {
    $resource =  'http://localhost:3000/api/events';
    $data = drupal_http_request($resource);
    $events = drupal_json_decode($data->data)['data'];

    foreach($events as $delta => $event) {
        $importer = new EventImporter($event);
        // build artist and add to event
    }
}

class NodeImporter {
    public static function nodeExists ($title, $bundle) {
        $query = new EntityFieldQuery();
        $query->entityCondition('entity_type', 'node')
            ->entityCondition('bundle', array($bundle))
            ->propertyCondition('title', $title . '%', 'like');

        $result = $query->execute();

        if (!empty($result)) {
            $node = node_load(array_keys($result['node'])[0]);
            return entity_metadata_wrapper('node', $node);
        } else {
            return false;
        }
    }

    public static function findGenres ($term) {
        $query = new EntityFieldQuery();
        $query->entityCondition('entity_type', 'taxonomy_term')
            ->entityCondition('bundle', 'genres')
            ->propertyCondition('name', $term. '%', 'LIKE');

        $result = $query->execute();
        return $result;
    }

    public static function importImage ($image_source_path) {
        $url = 'http://localhost:3000/' . drupal_encode_path($image_source_path);
        $directory = file_build_uri('/import/');
        $file = system_retrieve_file($url, $directory, TRUE, FILE_EXISTS_REPLACE);
        return $file;
    }
}

class EventImporter {

    public $event;

    public $wrapper;

    public function __construct($event) {
        $this->event = $event;
        $this->wrapEvent();
        $this->addProperties();
        $this->wrapper->save();
    }

    public function wrapEvent() {
        $new_node = entity_create('node', array('type' => 'events'));
        $this->wrapper = entity_metadata_wrapper('node', $new_node);
    }

    public function addProperties() {
        $this->setAuthor();
        $this->setTitle();
        $this->setDate();
        $this->addArtists();
        $this->addVenue();
    }

    public function setAuthor() {
        $this->wrapper->author = 1;
    }

    public function setTitle() {
        $this->wrapper->title = $this->event['name'];
    }

    public function setDate() {
        $date = strtotime($this->event['date']);
        $this->wrapper->field_date->set($date);
    }

    public function addArtists() {
        $artistIds = array();
        foreach($this->event['artists'] as $delta => $value) {
            $artist = new ArtistImport($value);
            $artistIds[] = $artist->getId();
        }
        $this->wrapper->field_artists = $artistIds;
    }

    public function addVenue() {
        $venue = new VenueImport($this->event['venue']);
        $venueId = $venue->getId();

        $this->wrapper->field_venue = $venueId;
    }

}

class ArtistImport {
    public $artist;

    public $wrapper;

    public function __construct($artist) {
        $this->artist = $artist;
        // if exists add wrapper and return this else set properties and things
        if ($wrapper = NodeImporter::nodeExists($this->artist['name'], 'artists')) {
            $this->wrapper = $wrapper;
        } else {
            $this->wrapArtist();
            $this->addProperties();
            $this->wrapper->save();
        }
    }

    public function getId () {
        return  $this->wrapper->getIdentifier();
    }

    public function wrapArtist() {
        $new_node = entity_create('node', array('type' => 'artists'));
        $this->wrapper = entity_metadata_wrapper('node', $new_node);
    }

    public function addProperties() {
        $this->setAuthor();
        $this->setTitle();
        $this->setBody();
        $this->setGenres();
        $this->setFile();
        $this->setLinks();
        $this->setYoutube();
    }

    public function setAuthor() {
        $this->wrapper->author = 1;
    }

    public function setTitle() {
        // Title
        $this->wrapper->title = $this->artist['name'];
    }

    public function setBody() {
        if(!empty($this->artist['body'])) {
            $this->wrapper->field_about = $this->artist['body'];
        }
    }

    public function setGenres() {// field_genres
        $tids = array();
        foreach($this->artist['genres'] as $delta => $genre) {
            $result = NodeImporter::findGenres($genre);
            if (!empty($result)) {
                $tids[] = array_keys($result['taxonomy_term'])[0];
            }
        }
        $this->wrapper->field_genres->set($tids);
    }

    public function setFile() {
        // field_file
        if ($path = $this->artist['images']['original']) {
            $file = NodeImporter::importImage($path);
            $this->wrapper->field_image->file = $file;
        }
    }

    public function setLinks() {
        $links = array_intersect_key($this->artist['links'], variable_get('socialfield_services'));
        $links_array = array();
        foreach($links as $service => $url) {
            $links_array[] = array('service' => $service, 'url' => $url);
        }
        $this->wrapper->field_social = $links_array;
    }

    public function setYoutube() {
        // youtube
        $this->wrapper->field_youtube = array();
        foreach($this->artist['videos'] as $link) {
            $this->wrapper->field_youtube[] = array('input' => 'http://www.youtube.com/watch?v=' . $link, 'video_id' => $link);
        }
    }
}

class VenueImport {
    public $venue;

    public $wrapper;

    public function __construct($venue) {
        $this->venue = $venue;
        // if exists add wrapper and return this else set properties and things
        if ($wrapper = NodeImporter::nodeExists($this->venue['name'], 'venues')) {
            $this->wrapper = $wrapper;
        } else {
            $this->wrapVenue();
            $this->addProperties();
            $this->wrapper->save();
        }
    }

    public function getId () {
        return  $this->wrapper->getIdentifier();
    }

    public function wrapVenue() {
        $new_node = entity_create('node', array('type' => 'venues'));
        $this->wrapper = entity_metadata_wrapper('node', $new_node);
    }

    public function addProperties() {
        $this->setAuthor();
        $this->setTitle();
        $this->setBody();
        $this->setFile();
        $this->setLinks();
        $this->setPhone();
        $this->setAddress();
    }

    public function setAuthor() {
        $this->wrapper->author = 1;
    }

    public function setTitle() {
        // title
        $this->wrapper->title = $this->venue['name'];
    }

    public function setBody() {
        if (!empty($this->venue['body'])) {
            $this->wrapper->field_about = $this->venue['body'];
        }
    }

    public function setFile() {
        if ($path = $this->venue['images']['original']) {
            $file = NodeImporter::importImage($path);
            $this->wrapper->field_image->file = $file;
        }
    }

    public function setLinks() {
        // link
        $this->wrapper->field_social = array( array('service' => 'website', 'url' => $this->venue['website']));
    }

    public function setPhone() {
        // phone
        if (!empty($venue['phone'])) {
            $this->wrapper->field_phone = $this->venue['phone'];
        }
    }

    public function setAddress() {
        // address
        $this->wrapper->field_address->thoroughfare =$this->venue['address']['address1'];
        $this->wrapper->field_address->locality = $this->venue['address']['city'];
        $this->wrapper->field_address->administrative_area = 'FL';
        $this->wrapper->field_address->country = $this->venue['address']['country'];
        $this->wrapper->field_address->postal_code = $this->venue['address']['zipcode'];
    }
}

Here is the migrated JSON object coming out of MongoDB:

{
    venue: {
        __v: 0,
        _id: "5693db65f1ca8f9a380dc44f",
        name: "Amaturo Theater at Broward Center for the Performing Arts",
        phone: "(954) 522-5334",
        website: "https://www.browardcenter.org/",
        images: {
            thumbnail: "images/thumbnail/Amaturo Theater at Broward Center for the Performing Arts.jpg",
            original: "images/original/Amaturo Theater at Broward Center for the Performing Arts.jpg",
            large: "images/large/Amaturo Theater at Broward Center for the Performing Arts.jpg"
        },
        address: {
            address1: "201 SW 5th Ave",
            city: "Fort Lauderdale",
            state: "FL",
            country: "US",
            zipcode: "33312",
            coordinates: {
                lat: "26.119927",
                lng: "-80.14919380000003"
            }
        }
    },
    name: "Symphony of the Americas: Roberto Cani, violin",
    date: "2016-01-13T01:15:00.000Z",
    _id: "56949b7c4cb7518d69679349",
    __v: 0,
    info: {
        marketId: "15",
        price: "50 - 75",
        currency: "USD",
        ticketSaleStart: "2015-09-07T14:00:00Z",
        timezone: "America/New_York",
        link: "/Symphony-of-the-Americas-tickets/artist/889537",
        localTimeFormatted: "Tue, 01/12/16<br>08:15 PM",
        dmaid: [
            "334",
            "412"
        ]
    },
    artists: [{
        __v: 0,
        _id: "5693daeef1ca8f9a380dc44e",
        body: "Roberto Cani made his debut at Gaveau Hall in Paris under direction of maestro Daniele Gatti on 16 January 1987. He has concertized in his native Italy, Russia, Poland, Croatia, Germany, Austria, Belgium, Spain, Serbia, France, Taiwan, Japan, and South Africa. He has performed with La Scala Orchestra under Riccardo Muti, Bolgograd Symphony Orchestra, Moscow Symphony Orchestra, RAI Symphony Orchestra, Haydn Orchestra, Orchestra Cantelli, Missouri Chamber Orchestra, Central Symphony Orchestra of Oregon, and the Italian Consort. He has played in venues such as Los Angeles's Royce Hall, Moscow's Bolshoi Zal and Tchaikovsky Zal, Belgrade's Lisinski Hall, Bristol's St. Georges - Brandon Hill, Tokyo's Suntory Hall and Milan's La Scala.",
        name: "Symphony of the Americas: Roberto Cani, violin",
        links: {
            wikipedia: "https://en.wikipedia.org/wiki/Roberto_Cani"
        },
        genres: [
            "Classical"
        ],
        videos: [
            "vDE2jZMryv4",
            "agsnhhnlQhQ"
        ],
        images: {
            thumbnail: "images/thumbnail/Symphony of the Americas.jpg",
            original: "images/original/Symphony of the Americas.jpg",
            large: "images/large/Symphony of the Americas.jpg"
        }
    }]
}
\$\endgroup\$
  • \$\begingroup\$ Drupal 7, using the Entity API. I haven't used Drupal 8 yet. \$\endgroup\$ – Adam S Feb 6 '16 at 8:10
  • 1
    \$\begingroup\$ Have you looked at the migrate module? \$\endgroup\$ – Josh Beauregard Feb 29 '16 at 15:29
  • \$\begingroup\$ While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review \$\endgroup\$ – Mast Feb 29 '16 at 16:44
  • \$\begingroup\$ This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. - From Review \$\endgroup\$ – rolfl Feb 29 '16 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.