Provides Drupal services and Drush 9 commands for easy creation of dummy data.
This package requires PHP 7.1 and Drupal 8.7.7 or higher. It can be installed using Composer:
composer require wieni/wmdummy_dataTo learn more about how to define your factory and state classes, please refer to the
wieni/wmmodel_factory docs.
In order to have access to the following functionality, you need to extend
EntityFactoryBase or EntityStateBase instead of the classes
provided by wmmodel_factory.
Generate content using wieni/wmcontent
To generate content for an entity using the wmcontent module, make your generator implement
ContentGenerateInterface.
<?php
namespace Drupal\my_module\Entity\ModelFactory\Factory\Node;
use Drupal\wmcontent\Entity\WmContentContainer;
use Drupal\wmdummy_data\EntityFactoryBase;
/**
* @EntityFactory(
* entity_type = "node",
* bundle = "page",
* )
*/
class PageFactory extends EntityFactoryBase implements ContentGenerateInterface
{
public function make(): array
{
return [
'title' => $this->faker->sentence(4),
'menu_link' => null,
];
}
public function generateContent(WmContentContainer $container): array
{
$entityType = $container->getChildEntityType();
$bundles = $container->getChildBundles() ?: $container->getChildBundlesAll();
$amount = $this->faker->numberBetween(1, 10);
return array_map(
fn () => $this->faker->entityWithType($entityType, $this->faker->randomElement($bundles)),
array_fill(0, $amount, null)
);
}
}To populate entity reference fields with new or existing entities, use the following methods in your generator:
$this->faker->entity()(passing the class name of an entity bundle class)$this->faker->entityWithType()(passing entity type ID and optionally bundle as strings)
<?php
namespace Drupal\my_module\Entity\ModelFactory\Factory\Node;
use Drupal\my_module\Entity\TaxonomyTerm\Tag;
use Drupal\wmdummy_data\EntityFactoryBase;
/**
* @EntityFactory(
* entity_type = "node",
* bundle = "page",
* )
*/
class PageFactory extends EntityFactoryBase
{
public function make(): array
{
return [
'title' => $this->faker->sentence(4),
'menu_link' => null,
'field_tag' => [
'entity' => $this->faker->entity(Tag::class),
],
];
}
}To get access to existing links to Vimeo and YouTube videos in your generators, use the following methods:
$this->faker->vimeoUrl$this->faker->youTubeUrl
<?php
namespace Drupal\my_module\Entity\ModelFactory\Factory\ContentBlock;
use Drupal\wmdummy_data\EntityFactoryBase;
/**
* @EntityFactory(
* entity_type = "content_block",
* bundle = "video",
* )
*/
class VideoFactory extends EntityFactoryBase
{
public function make(): array
{
$data = [
'field_video_title' => $this->faker->optional()->sentence($this->faker->numberBetween(4, 8)),
'field_video_type' => $this->faker->randomElement(['youtube', 'vimeo']),
];
switch ($data['field_video_type']) {
case 'vimeo':
$data['field_video_vimeo'] = $this->faker->vimeoUrl;
break;
case 'youtube':
$data['field_video_youtube'] = $this->faker->youTubeUrl;
break;
}
return $data;
}
}To get a random element from an array with some elements having a bigger chance to be returned than others, use the following method in your generators:
$this->faker->randomElementWithWeight()passing an array with the values as keys and weights as values, in the form of integers.
<?php
namespace Drupal\my_module\Entity\ModelFactory\Factory\Node;
use Drupal\node\NodeInterface;
use Drupal\wmdummy_data\EntityFactoryBase;
/**
* @EntityFactory(
* entity_type = "node",
* bundle = "page",
* )
*/
class PageFactory extends EntityFactoryBase
{
public function make(): array
{
return [
'title' => $this->faker->sentence(4),
'menu_link' => null,
'status' => $this->faker->randomElementWithWeight([
NodeInterface::NOT_PUBLISHED => 70,
NodeInterface::PUBLISHED => 30,
]),
];
}
}To generate random strings of HTML, use one of the following methods in your generators:
$this->faker->htmlBlock(generates a block of HTML with headings, paragraphs, lists and a table)$this->faker->htmlHeading(generates a random heading between levels H1 and H6, a number can also be passed to choose the level yourself)$this->faker->htmlParagraph(aptag with random text, also containingstrongandatags)$this->faker->htmlOrdenedList(aoltag with random list items)$this->faker->htmlUnordenedList(aultag with random list items)$this->faker->htmlEmbed(aniframetag with a random url, not necessarily a real one)$this->faker->htmlAnchor(anatag with a random url, not necessarily a real one)$this->faker->htmlTable(atabletag with a heading and a couple of rows/columns)
This package provides a couple of Drush commands for managing dummy data:
wmdummy-data:generate: Generate entitieswmdummy-data:delete: Delete generated entities
For more information about command aliases, arguments, options & usage
examples, call the command with the -h / --help argument
If you prefer working with the Drupal administration interface over using the CLI, you can use the form at
/admin/config/development/wmdummy-data. This page can also be found through the administration menu (Configuration >
Development > Dummy data).
Only users with the generate dummy data and/or delete dummy data permissions can use these features through the
administration interface.
You can subscribe to the following events to attach custom logic to the dummy data generation process:
Will be triggered after an entity is generated.
Will be triggered after an entity is generated and persisted to the database.
All notable changes to this project will be documented in the CHANGELOG file.
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
Distributed under the MIT License. See the LICENSE file for more information.