Publish post
This commit is contained in:
parent
58477cd20d
commit
e1d0b56014
|
@ -7,7 +7,6 @@ tags:
|
||||||
- drupal-planet
|
- drupal-planet
|
||||||
- drupal-7
|
- drupal-7
|
||||||
- php
|
- php
|
||||||
draft: yes
|
|
||||||
---
|
---
|
||||||
{% block excerpt %}
|
{% block excerpt %}
|
||||||
How to use the [xautoload][1] module to autoload migration classes within your Drupal 7 migration modules.
|
How to use the [xautoload][1] module to autoload migration classes within your Drupal 7 migration modules.
|
||||||
|
@ -16,11 +15,19 @@ How to use the [xautoload][1] module to autoload migration classes within your D
|
||||||
{% block content %}
|
{% block content %}
|
||||||
## What is xautoload?
|
## What is xautoload?
|
||||||
|
|
||||||
[xautoload][1] is a Drupal module that enables autoloading PHP classes, in the same way that you would do so in a [Composer][2] project such as Drupal 8 or Symfony.
|
[xautoload][1] is a Drupal module that enables the autoloading of PHP classes, in the same way that you would do so in a [Composer][2] based project such as Drupal 8 or Symfony.
|
||||||
|
|
||||||
It supports both the [PSR-0][3] and [PSR-4][4] standards, as well as providing a wildcard syntax for the Drupal `file[]` syntax in .info files.
|
It supports both the [PSR-0][3] and [PSR-4][4] standards, as well as providing a wildcard syntax for Drupal’s `file[]` syntax in .info files.
|
||||||
|
|
||||||
To use it, download and enable it from Drupal.org as you would for any other module.
|
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:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
dependencies[] = xautoload (>= 7.x-5.0)
|
||||||
|
```
|
||||||
|
|
||||||
|
This will ensure that the version of xautoload is 7.x-5.0 or newer.
|
||||||
|
|
||||||
|
## How to use it
|
||||||
|
|
||||||
### Wildcard syntax for .info files
|
### Wildcard syntax for .info files
|
||||||
|
|
||||||
|
@ -40,7 +47,7 @@ files[] = includes/nodes/page.inc
|
||||||
|
|
||||||
In this example, each custom migration class is stored in it’s own file within the `includes` directory, and each class needs to be loaded separately using the `files[] = filename` syntax.
|
In this example, each custom migration class is stored in it’s own file within the `includes` directory, and each class needs to be loaded separately using the `files[] = filename` syntax.
|
||||||
|
|
||||||
One thing that the xautoload module does is allow for the use of wildcards within .info files. By using this, the module file can be simplified as follows:
|
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:
|
||||||
|
|
||||||
```
|
```
|
||||||
files[] = includes/**/*.inc
|
files[] = includes/**/*.inc
|
||||||
|
@ -48,16 +55,16 @@ files[] = includes/**/*.inc
|
||||||
|
|
||||||
This will load any .inc files within the `includes` directory as well as any sub-directories, like 'node' in the original example.
|
This will load any .inc files within the `includes` directory as well as any sub-directories, like 'node' in the original example.
|
||||||
|
|
||||||
This means that any new migration classes that are added will be automatically loaded without needing to edit foo_migrate.info again, and without needing to re-structure your directories.
|
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.
|
||||||
|
|
||||||
### Use the PSR-4 structure
|
### Use the PSR-4 structure
|
||||||
|
|
||||||
If you want to use the [PSR-4][4] model, you can do that too.
|
If you want to use the [PSR-4][4] approach, you can do that too.
|
||||||
|
|
||||||
In order to do so, you’ll need to complete the following steps:
|
In order to do so, you’ll need to complete the following steps:
|
||||||
|
|
||||||
1. Rename the `includes` directory to `src`.
|
1. Rename the `includes` directory to `src`.
|
||||||
2. Ensure that there is one PHP class per file, and that the file ends with `.php` rather than `.inc`.
|
2. Ensure that there is one PHP class per file, and that the file extension is `.php` rather than `.inc`.
|
||||||
3. Ensure that the name of the file matches the name of the class - `FooArticleNodeMigration` would be in a file called `FooArticleNodeMigration.php`.
|
3. Ensure that the name of the file matches the name of the class - `FooArticleNodeMigration` would be in a file called `FooArticleNodeMigration.php`.
|
||||||
4. Add a namespace to each PHP file. This uses the same format as Drupal 8, including the machine name of the module. For example, `Drupal\foo_migrate`.
|
4. Add a namespace to each PHP file. This uses the same format as Drupal 8, including the machine name of the module. For example, `Drupal\foo_migrate`.
|
||||||
* If the class is within a sub-directory, then this will also need to be included within the namespace - e.g. `Drupal\foo_migrate\Node`.
|
* If the class is within a sub-directory, then this will also need to be included within the namespace - e.g. `Drupal\foo_migrate\Node`.
|
||||||
|
@ -76,6 +83,7 @@ class FooArticleNodeMigration extends DrupalNode6Migration {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
Within `foo_migrate.migrate.inc`, I can now reference any class names using their full namespace:
|
Within `foo_migrate.migrate.inc`, I can now reference any class names using their full namespace:
|
||||||
|
|
Loading…
Reference in a new issue