3.Execute the `drush content-fixtures:load` command to execute the `load()` method and create the Articles.
> :information_source: When we execute `drush content-fixtures:load` all the `load` methods of all services tagged with `content_fixture` will be executed.
> :warning: The load command will remove all content from the site before inserting the new content, but don't worry, the command will ask you if you want to proceed or not.
### Splitting fixtures into separate files
If we have a simple fixture, it will be fine to put it in just one class, but if we have multiple fixtures and class ends up being too long, hard to debug and extend in the future,
then it will be better to separate these fixtures across multiple classes.
#### I-Sharing objects between fixtures.
We can create an object using one fixture, then pass it to the next fixtures. This is especially useful if we want to set up references between objects created in multiple fixtures.
The easiest way of achieving this is to extend `AbstractFixture` class, that will provide you with some additional
sugar - this abstract class implements `SharedFixtureInterface` that will give you a way of sharing created objects
between fixtures.
The easiest way of achieving this is to extend `AbstractFixture` class that implements `SharedFixtureInterface`, which will provide you with some additional
methods that make it possible to share objects across fixtures:
-`addReference()` : exposes the object to the fixtures that will load after the one using this method.
-`getReference()` : allows you to access any object that has been exposed by the `addReference` method.
### Order of execution of fixtures
For example:
For this to work, you have to be able to decide on the order of execution of fixtures, and you can do
this in two ways:
1. Create a fixture class that extends the `AbstractFixture` class, this will allow you to use the `addReference` method later on.
```php
// see full code example at: /content_fixtures/modules/content_fixtures_example/src/Fixture/splitting_fixtures/UserFixture.php
2. Use the `addReference()` method to store the created $user object under the reference name `user`. It will make it accessible to the next fixtures, by using that reference name.
```php
// see full code example at: /content_fixtures/modules/content_fixtures_example/src/Fixture/splitting_fixtures/UserFixture.php
3. The object we just shared, will now be accessible for any other fixture that will run later, by using the `getReference()`method.Wejustneedtoaddressitbyitsreferencename.
```php
// see full code example at: /content_fixtures/modules/content_fixtures_example/src/Fixture/splitting_fixtures/ArticleDependentOnUserFixture.php
use Drupal\content_fixtures\Fixture\AbstractFixture;
class ArticleDependentOnUserFixture extends AbstractFixture {
'title' => 'Fixture title - Splitting fixture tutorial',
// This is another nice thing about the AbstractFixture class: it gives
// us a random string generator.
'body' => $this->getRandom()->paragraphs(2),
'uid' => $user->id(),
]);
$node->save();
}
}
```
#### II-Loading the fixture files in a specific order
The problem with the previous step that what if the NodeFixture has been loaded before the UserFixture
this will cause an error because the NodeFixture depends on the UserFixture (`'uid' => $user->id()`) so we need to
make sure that the `drush content-fixtures:load` will load the UserFixture first.
Fortunately, we can control the fixtures' execution order, by using one of these two methods (method 2 is recommended):
1. By implementing `OrderedFixtureInterface` - this will allow you to assign a value to each fixture, that will be used
for ordering.
```php
use Drupal\content_fixtures\Fixture\OrderedFixtureInterface;
class NodeFixture extends AbstractFixture implements OrderedFixtureInterface {
public function getOrder() {
return 2;
}
}
```
2. By implementing `DependentFixtureInterface`, that will allow you to declare dependencies between fixtures, and order
of execution will be calculated by using this information.
```php
// see the full example at : src/web/modules/contrib/content_fixtures/modules/content_fixtures_example/src/Fixture/splitting_fixtures/ArticleDependentOnUserFixture.php
use Drupal\content_fixtures\Fixture\DependentFixtureInterface;
### Groups
class ArticleDependentOnUserFixture extends AbstractFixture implements DependentFixtureInterface {
public function getDependencies(): array {
return [
UserFixture::class,
];
}
}
```
> :information_source: To display the fixtures' execution order just use : `drush content-fixture:list`
You can also implement `FixtureGroupInterface` in your fixture, to assign it to some custom groups. It will allow you
### Fixture Groups: Only executing some fixtures
The `drush content-fixtures:load` will load all existing fixtures, but what if we want to load only a specific set of fixtures?
To do that you can implement `FixtureGroupInterface` in your fixture, to assign it to some custom groups. It will allow you
to run fixtures by groups they belong to. This way you can have different set of fixtures for presentation, different
for development etc.
1. Make your class implement the `FixtureGroupInterface`. This interface has a method `getGroup()` that should return an array of arbitrary strings,
representing your groups, so just return names of groups you want your fixture to belong to.
```php
// see the full example at : /content_fixtures/modules/content_fixtures_example/src/Fixture/groups/ArticleBelongingToGroupFixture.php
class ArticleBelongingToGroupFixture implements FixtureInterface, FixtureGroupInterface {
public function getGroups(): array {
return [
'node_group',
];
}
}
```
```php
// see the full example at : /content_fixtures/modules/content_fixtures_example/src/Fixture/groups/PageBelongingToGroupFixture.php
class PageBelongingToGroupFixture.php implements FixtureInterface, FixtureGroupInterface {
public function getGroups(): array {
return [
'node_group',
];
}
}
```
2. In order to display all the fixtures that belong to the `node_group`, use : `drush content-fixtures:list --groups=node_group`
3. In order to load only the fixtures that belong to the `node_group`, use : `drush content-fixtures:load --groups=node_group`
### Execution
You need `drush` to run your fixtures. Module provides you with two commands:
*`content-fixtures:list`
*`content-fixtures:load`
You need `drush` to run your fixtures. This module provides you with three commands with some options:
*`drush content-fixtures:list`: list all fixtures (services tagged using the `content_fixture` tag).
*`drush content-fixtures:load`: Delete all content, then load all fixtures.
*`drush content-fixtures:load --groups=group1`: load all fixtures that belong to a specific group.
*`drush content-fixtures:load --groups=group1,group2,group3`: load all fixtures that belong to any of these groups.
*`drush content-fixtures:purge`: delete all existing content on website.
See: `drush help content-fixtures:list` and `drush help content-fixtures:load` .