327 lines
15 KiB
HTML
327 lines
15 KiB
HTML
<!DOCTYPE html>
|
||
<html class="no-js" lang="en-GB">
|
||
<head>
|
||
<title>Simplifying Drupal Migrations with xautoload | Oliver Davies</title>
|
||
|
||
<meta charset="UTF-8">
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
|
||
|
||
<meta property="og:url" content="https://www.oliverdavies.uk/blog/2016/05/03/simplifying-drupal-migrations-with-xautoload/">
|
||
<meta property="og:title" content="Simplifying Drupal Migrations with xautoload"/>
|
||
|
||
|
||
|
||
<meta property="og:image" content="https://www.oliverdavies.uk/assets/images/me-precedent.jpg"/>
|
||
<meta property="og:image:height" content="327"/>
|
||
<meta property="og:image:type" content="image/jpg">
|
||
<meta property="og:image:width" content="327"/>
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="https://www.oliverdavies.uk/assets/css/main.css">
|
||
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=57" sizes="57x57">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=114" sizes="114x114">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=72" sizes="72x72">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=144" sizes="144x144">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=60" sizes="60x60">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=120" sizes="120x120">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=76" sizes="76x76">
|
||
<link rel="apple-touch-icon" href="/assets/images/me-precedent.jpg?s=152" sizes="152x152">
|
||
|
||
<link rel="icon" href="/assets/images/me-precedent.jpg?s=160" sizes="160x160">
|
||
<link rel="icon" href="/assets/images/me-precedent.jpg?s=96" sizes="96x96">
|
||
<link rel="icon" href="/assets/images/me-precedent.jpg?s=32" sizes="32x32">
|
||
<link rel="icon" href="/assets/images/me-precedent.jpg?s=16" sizes="16x16">
|
||
</head>
|
||
<body class="">
|
||
<nav class="navbar navbar-inverse navbar-fixed-top">
|
||
<div class="container">
|
||
<div class="navbar-header">
|
||
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
|
||
<span class="sr-only">Toggle navigation</span>
|
||
<span class="icon-bar"></span>
|
||
<span class="icon-bar"></span>
|
||
<span class="icon-bar"></span>
|
||
</button>
|
||
<a class="navbar-brand" href="https://www.oliverdavies.uk/">Oliver Davies</a>
|
||
</div>
|
||
|
||
<div id="navbar" class="collapse navbar-collapse" role="navigation">
|
||
<ul class="nav navbar-nav">
|
||
<li class="">
|
||
<a href="/">About</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
<a href="/experience">Experience</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
<a href="/testimonials">Testimonials</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
<a href="/talks">Talks</a>
|
||
</li>
|
||
|
||
<li class="active">
|
||
<a href="/blog">Blog</a>
|
||
</li>
|
||
|
||
<li class="">
|
||
<a href="/contact">Contact</a>
|
||
</li>
|
||
</ul>
|
||
</div> </div>
|
||
</nav>
|
||
|
||
<div class="container">
|
||
<div class="row">
|
||
<main class="col-md-9">
|
||
<h1>Simplifying Drupal Migrations with xautoload</h1>
|
||
|
||
<p class="posted text-light">3rd May 2016</p>
|
||
|
||
<h2 id="what-is-xautoload%3F">What is xautoload?</h2>
|
||
|
||
<p><a href="https://www.drupal.org/project/xautoload">xautoload</a> is a Drupal module that enables the autoloading of PHP classes, in the same way that you would do so in a <a href="http://getcomposer.org">Composer</a> based project such as Drupal 8 or Symfony.</p>
|
||
|
||
<p>It supports both the <a href="http://www.php-fig.org/psr/psr-0/">PSR-0</a> and <a href="http://www.php-fig.org/psr/psr-4/">PSR-4</a> standards, as well as providing a wildcard syntax for Drupal’s <code>file[]</code> syntax in .info files.</p>
|
||
|
||
<p>To use it, download and enable it from Drupal.org as you would for any other module, and then add it as a dependency within your module. The xautoload project page suggests including a minimum version in this format:</p>
|
||
|
||
<pre><code class="language-ini">dependencies[] = xautoload (>= 7.x-5.0)
|
||
</code></pre>
|
||
|
||
<p>This will ensure that the version of xautoload is 7.x-5.0 or newer.</p>
|
||
|
||
<h2 id="how-to-use-it">How to use it</h2>
|
||
|
||
<h3 id="wildcard-syntax-for-.info-files">Wildcard syntax for .info files</h3>
|
||
|
||
<p>Here is an example .info file for a migrate module.</p>
|
||
|
||
<pre><code class="language-ini">; foo_migrate.info
|
||
|
||
name = Foo Migration
|
||
core = 7.x
|
||
package = Foo
|
||
|
||
files[] = includes/user.inc
|
||
files[] = includes/nodes/article.inc
|
||
files[] = includes/nodes/page.inc
|
||
</code></pre>
|
||
|
||
<p>In this example, each custom migration class is stored in it’s own file within the <code>includes</code> directory, and each class needs to be loaded separately using the <code>files[] = filename</code> syntax.</p>
|
||
|
||
<p>One thing that the xautoload module does to enable for the use of wildcards within this syntax. By using wildcards, the module file can be simplified as follows:</p>
|
||
|
||
<pre><code class="language-ini">files[] = includes/**/*.inc
|
||
</code></pre>
|
||
|
||
<p>This will load any .inc files within the <code>includes</code> directory as well as any sub-directories, like 'node' in the original example.</p>
|
||
|
||
<p>This means that any new migration classes that are added will be automatically loaded, so you don’t need to declare each include separately within foo_migrate.info again. The great thing about this approach is that it works with the existing directory and file structure.</p>
|
||
|
||
<h3 id="use-the-psr-4-structure">Use the PSR-4 structure</h3>
|
||
|
||
<p>If you want to use the <a href="http://www.php-fig.org/psr/psr-4/">PSR-4</a> approach, you can do that too.</p>
|
||
|
||
<p>In order to do so, you’ll need to complete the following steps:</p>
|
||
|
||
<ol>
|
||
<li>Rename the <code>includes</code> directory to <code>src</code>.</li>
|
||
<li>Ensure that there is one PHP class per file, and that the file extension is <code>.php</code> rather than <code>.inc</code>.</li>
|
||
<li>Ensure that the name of the file matches the name of the class - <code>FooArticleNodeMigration</code> would be in a file called <code>FooArticleNodeMigration.php</code>.</li>
|
||
<li>Add a namespace to each PHP file. This uses the same format as Drupal 8, including the machine name of the module. For example, <code>Drupal\foo_migrate</code>.
|
||
|
||
<ul>
|
||
<li>If the class is within a sub-directory, then this will also need to be included within the namespace - e.g. <code>Drupal\foo_migrate\Node</code>.</li>
|
||
<li>You’ll also need to import any class names that you are referencing, including class names that are you extending, by adding <code>use</code> statements at the top of the file. You may be able to prefix it with <code>\</code> instead (e.g. <code>\DrupalNode6Migration</code>), but I prefer to use imports.</li>
|
||
</ul></li>
|
||
</ol>
|
||
|
||
<p>Now your class may look something like this:</p>
|
||
|
||
<pre><code class="language-php"><?php
|
||
|
||
namespace Drupal\foo_migrate\Node;
|
||
|
||
use DrupalNode6Migration;
|
||
|
||
class FooArticleNodeMigration extends DrupalNode6Migration {
|
||
...
|
||
}
|
||
</code></pre>
|
||
|
||
<p>With these steps completed, any imports within your .info file can be removed as they are no longer needed and any classes will be loaded automatically.</p>
|
||
|
||
<p>Within <code>foo_migrate.migrate.inc</code>, I can now reference any class names using their full namespace:</p>
|
||
|
||
<pre><code class="language-php">$node_arguments['ArticleNode'] = array(
|
||
'class_name' => 'Drupal\foo_migrate\Node\FooArticleNodeMigration',
|
||
'source_type' => 'story',
|
||
'destination_type' => 'article',
|
||
);
|
||
</code></pre>
|
||
|
||
<h2 id="resources">Resources</h2>
|
||
|
||
<ul>
|
||
<li><a href="https://www.drupal.org/project/xautoload">xautoload module</a></li>
|
||
<li><a href="https://www.drupal.org/project/migrate">migrate module</a></li>
|
||
<li><a href="https://www.drupal.org/project/migrate_d2d">migrate_d2d module</a></li>
|
||
<li><a href="http://www.php-fig.org/psr/psr-0/">PSR-0</a></li>
|
||
<li><a href="http://www.php-fig.org/psr/psr-4/">PSR-4</a></li>
|
||
</ul>
|
||
|
||
<p class="tags">
|
||
Tags:
|
||
<a href="https://www.oliverdavies.uk/blog/tags/autoloading">autoloading</a>, <a href="https://www.oliverdavies.uk/blog/tags/drupal">drupal</a>, <a href="https://www.oliverdavies.uk/blog/tags/drupal-planet">drupal-planet</a>, <a href="https://www.oliverdavies.uk/blog/tags/drupal-7">drupal-7</a>, <a href="https://www.oliverdavies.uk/blog/tags/php">php</a> </p>
|
||
<div class="post-pager is-flex">
|
||
<div class="is-half">
|
||
<a href="/blog/2016/02/15/announcing-the-drupal-vm-generator/">
|
||
« Announcing the Drupal VM Generator
|
||
</a>
|
||
</div>
|
||
|
||
<div class="is-half text-right">
|
||
<a href="/blog/2016/07/15/building-gmail-filters-with-php/">
|
||
Building Gmail Filters with PHP »
|
||
</a>
|
||
</div>
|
||
</div>
|
||
<div class="about-author">
|
||
<h2>About the Author</h2>
|
||
|
||
<img src="//www.oliverdavies.uk/assets/images/me-precedent.jpg" alt="Picture of Oliver" class="img-circle">
|
||
|
||
<p>Oliver Davies is a Web Developer, System Administrator and Drupal specialist based in the UK. He is a Senior Developer at <a href="https://microserve.io">Microserve</a> and also provides freelance consultancy services for Drupal websites, PHP applications and Linux servers.</p>
|
||
</div>
|
||
</main>
|
||
|
||
<div class="col-md-3">
|
||
<div class="panel badges text-center">
|
||
<a href="https://assoc.drupal.org/membership" title="I’m a Drupal Association member." class="is-block">
|
||
<img
|
||
src="//www.oliverdavies.uk/assets/images/badges/da-individual-member.png"
|
||
alt="Drupal Association Individual Member"
|
||
width="200"
|
||
/>
|
||
</a>
|
||
|
||
<div class="is-flex align-items-center">
|
||
<img
|
||
src="//www.oliverdavies.uk/assets/images/badges/acquia-d8-developer.png"
|
||
alt="Acquia Certified Developer - Drupal 8 Exam Badge"
|
||
class="is-2"
|
||
/>
|
||
|
||
<img
|
||
src="//www.oliverdavies.uk/assets/images/badges/acquia-d8-back-end.png"
|
||
alt="Acquia Certified Back End Specialist - Drupal 8 Exam Badge"
|
||
class="is-2"
|
||
/>
|
||
</div>
|
||
|
||
<a href="http://conference.phpnw.org.uk/phpnw17" class="is-block">
|
||
<img src="//www.oliverdavies.uk/assets/images/badges/phpnw17.png" alt="">
|
||
</a>
|
||
</div>
|
||
<div class="availability panel panel-default">
|
||
<div class="panel-heading">Availability</div>
|
||
|
||
<div class="panel-body">
|
||
<p>
|
||
<i class="fa fa-thumbs-o-up text-warning"></i>
|
||
|
||
|
||
Currently have limited part-time capacity
|
||
</p>
|
||
<p>
|
||
<i class="fa fa-thumbs-o-down text-danger"></i>
|
||
|
||
Currently no spare full-time capacity.
|
||
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<div class="latest-posts panel panel-default">
|
||
<div class="latest-posts__heading panel-heading">Latest blog posts</div>
|
||
|
||
<ul class="list-group">
|
||
<li class="post list-group-item">
|
||
<span class="post__title">
|
||
<a href="/blog/2017/07/13/publishing-sculpin-sites-with-github-pages/">
|
||
Publishing Sculpin Sites with GitHub Pages
|
||
</a>
|
||
</span> -
|
||
<span class="post__date">13th July, 2017</span>
|
||
</li>
|
||
<li class="post list-group-item">
|
||
<span class="post__title">
|
||
<a href="/blog/2017/06/09/introducing-the-drupal-meetups-twitterbot/">
|
||
Introducing the Drupal Meetups Twitterbot
|
||
</a>
|
||
</span> -
|
||
<span class="post__date">9th June, 2017</span>
|
||
</li>
|
||
<li class="post list-group-item">
|
||
<span class="post__title">
|
||
<a href="/blog/2017/05/20/turning-drupal-module-into-feature/">
|
||
Turning Your Custom Drupal Module into a Feature
|
||
</a>
|
||
</span> -
|
||
<span class="post__date">20th May, 2017</span>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
</div> </div>
|
||
<footer class="container">
|
||
<p class="copyright">
|
||
© 2010-2017 Oliver Davies. Built with <a href="https://sculpin.io">Sculpin</a>.
|
||
</p>
|
||
|
||
<div class="meetups">
|
||
<h2>Things that I organise</h2>
|
||
<ul>
|
||
<li class="meetups--drupal-bristol">
|
||
<a href="http://www.drupalbristol.org.uk" title="Drupal Bristol">
|
||
<img
|
||
src="//www.oliverdavies.uk/assets/images/meetups/drupal-bristol.jpeg"
|
||
alt="Drupal Bristol logo"
|
||
>
|
||
</a>
|
||
</li>
|
||
<li class="meetups--drupalcamp-bristol">
|
||
<a href="http://www.drupalcampbristol.co.uk" title="DrupalCamp Bristol">
|
||
<img
|
||
src="//www.oliverdavies.uk/assets/images/meetups/drupalcamp-bristol.png"
|
||
alt="DrupalCamp Bristol logo"
|
||
>
|
||
</a>
|
||
</li>
|
||
<li class="meetups--phpsw">
|
||
<a href="http://phpsw.uk" title="PHPSW">
|
||
<img
|
||
src="//www.oliverdavies.uk/assets/images/meetups/phpsw.jpeg"
|
||
alt="PHPSW logo"
|
||
>
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</footer>
|
||
|
||
<script src="https://www.oliverdavies.uk/assets/js/site.js"></script>
|
||
|
||
<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-11967257-1', 'auto'); ga('send', 'pageview');</script>
|
||
|
||
</body>
|
||
</html>
|