PHP code example of wieni / wmdummy_data

1. Go to this page and download the library: Download wieni/wmdummy_data library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wieni / wmdummy_data example snippets




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)
        );
    }
}



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),
            ],
        ];
    }
}



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;
    }
}

$this->faker->randomElementWithWeight()



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,
            ]),
        ];
    }
}