Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting (Your Data) into Drupal 8

Getting (Your Data) into Drupal 8

Oliver Davies

January 19, 2017
Tweet

More Decks by Oliver Davies

Other Decks in Programming

Transcript

  1. Getting (Your Data)
    into Drupal 8

    View Slide

  2. opdavies
    • Web Developer and System Administrator
    • Senior Drupal Developer, Appnovation
    • Drupal core contributor, mentor, contrib module maintainer
    • Drupal Bristol, PHPSW, DrupalCamp Bristol co-organiser
    @opdavies | oliverdavies.uk

    View Slide

  3. Drupal 6 -> 7
    @opdavies | oliverdavies.uk

    View Slide

  4. Upgrade Path (Core)
    • Replace files in-place, run updates
    • Legacy data carried into new site
    @opdavies | oliverdavies.uk

    View Slide

  5. Migration (contrib)
    • Provided by migrate and migrate_d2d modules
    • Start from scratch, import data
    • Provide base classes to extend
    @opdavies | oliverdavies.uk

    View Slide

  6. Configuring a Migration
    // my_migration_module.migrate.inc
    function my_migration_module_migrate_api() {
    return array(
    'api' => 2,
    'groups' => array(
    'beer' => array(
    'title' => t('Beer Imports')
    ),
    'wine' => array(
    'title' => t('Wine Imports')
    ),
    )

    View Slide

  7. Configuring a Migration
    // my_migration_module.migrate.inc
    ...
    'migrations' => array(
    'BeerTerm' => array(
    'class_name' => 'BeerTermMigration',
    'group_name' => 'beer',
    ),
    'BeerUser' => array(
    'class_name' => 'BeerUserMigration',
    'group_name' => 'beer',
    ),
    )

    View Slide

  8. Adding a Migration
    class BeerTermMigration extends BasicExampleMigration {
    ...
    $this
    ->addFieldMapping('parent_name', 'style_parent')
    ->description(t('The incoming style_parent field is the name of the term parent'));
    $this->addFieldMapping(NULL, 'region')
    ->description('Will a field be added to the vocabulary for this?')
    ->issueGroup(t('Client Issues'))
    ->issuePriority(MigrateFieldMapping::ISSUE_PRIORITY_MEDIUM)
    ->issueNumber(770064);
    ...
    }

    View Slide

  9. Migrations in Drupal 7
    • A lot of verbose PHP code
    • Nested arrays
    • Descriptive, but long method names
    @opdavies | oliverdavies.uk

    View Slide

  10. Drupal 8
    @opdavies | oliverdavies.uk

    View Slide

  11. D8 Migrate
    • No upgrade path.
    • migrate, migrate_drupal, migrate_drupal_ui modules in
    core.
    • Additional modules in contrib.
    • PHP classes, annotations, YAML
    @opdavies | oliverdavies.uk

    View Slide

  12. View Slide

  13. Experimental Modules
    Like other features, new experimental modules can only be
    added in minor releases, but unlike other features, they may
    change between patch releases while they are still experimental,
    including API changes.
    — https://www.drupal.org/core/experimental
    @opdavies | oliverdavies.uk

    View Slide

  14. Building a Source Database
    @opdavies | oliverdavies.uk

    View Slide

  15. Sculpin => MySQL
    • Data extracted from YAML.
    • Imported into MySQL database.
    • Tables
    • venues
    • events
    • speakers
    • talks
    @opdavies | oliverdavies.uk

    View Slide

  16. Venues
    venues:
    sift:
    name: 'Sift Digital'
    website: http://www.siftdigital.com/
    proctors:
    name: 'Proctor & Stevenson'
    website: http://www.proctors.co.uk/

    View Slide

  17. View Slide

  18. Events
    events:
    - title: 'First Meetup!'
    date: '2011-04-19'
    link: 'https://groups.drupal.org/node/141239'
    location: sift
    - title: 'South West User Group May Meetup'
    date: '2011-05-25'
    link: 'https://groups.drupal.org/node/147324'
    location: proctors

    View Slide

  19. View Slide

  20. Adding the Source Database
    // settings.local.php
    $databases['default']['default'] = [
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'drupal',
    'username' => 'drupal',
    'password' => 'drupal',
    ];
    $databases['migrate']['default'] = [
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'migrate',
    'username' => 'migrate',
    'password' => 'migrate',
    ];

    View Slide

  21. Building a Custom Migration
    @opdavies | oliverdavies.uk

    View Slide

  22. Drupal 8 Module Structure
    ├── config
    │ └── install
    │ ├── migrate_plus.migration.event_node.yml
    │ ├── migrate_plus.migration.venue_term.yml
    │ └── migrate_plus.migration_group.drupalbristol.yml
    ├── drupalbristol_migrate.info.yml
    ├── drupalbristol_migrate.install
    └── src
    └── Plugin
    └── migrate
    └── source
    ├── EventNode.php
    ├── SpeakerNode.php
    ├── TalkNode.php
    └── VenueTerm.php

    View Slide

  23. drupalbristol_migrate.info.yml
    name: 'Drupal Bristol Migrate'
    description: 'Migrate content from Sculpin.'
    type: module
    core: '8.x'
    package: 'Drupal Bristol'
    dependencies:
    - drupal:migrate
    - migrate_plus:migrate_plus
    - migrate_tools:migrate_tools

    View Slide

  24. Adding a Migration Group
    # config/install/migrate_plus.migration_group.drupalbristol.yml
    id: drupalbristol
    label: 'Drupal Bristol'

    View Slide

  25. Adding a Migration Source (1)
    // src/Plugin/migrate/source/VenueTerm.php
    namespace Drupal\drupalbristol_migrate\Plugin\migrate\source;
    use Drupal\migrate\Plugin\migrate\source\SqlBase;
    /**
    * Source plugin for speakers.
    *
    * @MigrateSource(id="venue_term")
    */
    class VenueTerm extends SqlBase {
    }

    View Slide

  26. Adding a Migration Source (2)
    ...
    public function query() {
    return $this
    ->select('venues', 'v')
    ->fields('v', ['id', 'name', 'website']);
    }

    View Slide

  27. Adding a Migration Source (3)
    ...
    public function getIds() {
    return [
    'id' => [
    'type' => 'integer',
    'alias' => 'v'
    ]
    ];
    }

    View Slide

  28. Adding a Migration Source (4)
    ...
    public function fields() {
    return [
    'id' => $this->t('ID of the venue'),
    'name' => $this->t('Name of the venue'),
    ];
    }

    View Slide

  29. Adding a Migration Source (5)
    public function prepareRow(Row $row) {
    if (parent::prepareRow($row) === FALSE) {
    return FALSE;
    }
    $speakerIds = $row->getSourceProperty('speaker_ids');
    // Explode comma separated speaker IDs into an array.
    $row->setSourceProperty('speaker_ids', explode(',', $speakerIds));
    }

    View Slide

  30. Adding a Migration
    # config/install/migrate_plus.migration.venue_term.yml
    id: venue_term
    label: 'Venue terms'
    migration_group: drupalbristol
    source:
    plugin: venue_term
    destination:
    plugin: entity:taxonomy_term
    process:
    name: name
    vid:
    plugin: default_value
    default_value: venues
    field_website/uri: website

    View Slide

  31. View Slide

  32. View Slide

  33. View Slide

  34. View Slide

  35. Running a Migration
    @opdavies | oliverdavies.uk

    View Slide

  36. Running with Drush (1)
    drush migrate-status
    drush ms

    View Slide

  37. Running with Drush (2)
    drush migrate-import venue_term
    drush migrate-import --group=drupalbristol
    drush mi --all

    View Slide

  38. Running with Drush (3)
    drush migrate-rollback --group=drupalbristol
    drush mr --group=drupalbristol

    View Slide

  39. Running with Drush (4)
    drush migrate-stop (mst)
    drush migrate-reset-status (mrs)

    View Slide

  40. Demo
    @opdavies | oliverdavies.uk

    View Slide

  41. Take Aways
    • https://github.com/opdavies/drupalbristol_migrate
    • Similar but different
    • Migrate docs are excellent
    @opdavies | oliverdavies.uk

    View Slide

  42. Questions?
    @opdavies | oliverdavies.uk

    View Slide