Skip to content

Commit

Permalink
Added Faker Profile config entity
Browse files Browse the repository at this point in the history
  • Loading branch information
baikho committed Oct 27, 2019
1 parent d257fac commit f04d372
Show file tree
Hide file tree
Showing 20 changed files with 539 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ CONFIGURATION

1. Navigate to settings form through `Admin > Configuration > Development > Faker`

or directly at path `/admin/config/development/faker`
or directly at path `/admin/config/development/faker-profile`
5 changes: 5 additions & 0 deletions faker.links.action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
entity.faker_profile.add_form:
route_name: 'entity.faker_profile.add_form'
title: 'Add Faker Profile'
appears_on:
- entity.faker_profile.collection
5 changes: 5 additions & 0 deletions faker.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
entity.faker_profile.collection:
title: 'Faker Profiles'
parent: system.admin_config_development
description: 'Configure Faker Profiles'
route_name: entity.faker_profile.collection
14 changes: 11 additions & 3 deletions faker.module
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ function faker_node_presave(EntityInterface $node) {
// Check for devel_generate details.
if ($devel_generate && isset($node->devel_generate)) {
// Check if Faker has been chosen in the form.
if (isset($node->devel_generate[FakerConstants::USE_FAKER]) && (bool) $node->devel_generate[FakerConstants::USE_FAKER] === TRUE) {
if (isset($node->devel_generate[FakerConstants::PROFILE]) && (bool) $node->devel_generate[FakerConstants::PROFILE] !== '_none_') {
// Specify title using Faker.
$node->setTitle(Factory::create()->country);
$node->setTitle(Factory::create($node->devel_generate[FakerConstants::LOCALE] ?? NULL)->country);
// Faker sample data population.
FakerHelper::populateFields($node);
try {
FakerHelper::populateFields(
$node,
$node->devel_generate[FakerConstants::PROFILE],
$node->devel_generate[FakerConstants::LOCALE] ?? NULL
);
} catch (\Exception $e) {
// Do nothing.
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions faker.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
administer faker profile configuration:
title: 'Manage Faker Profiles'
restrict access: TRUE
31 changes: 31 additions & 0 deletions faker.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
entity.faker_profile.collection:
path: '/admin/config/development/faker-profile'
defaults:
_entity_list: 'faker_profile'
_title: 'Faker Profile configuration'
requirements:
_permission: 'administer faker profile configuration'

entity.faker_profile.add_form:
path: '/admin/config/development/faker-profile/add'
defaults:
_entity_form: 'faker_profile.add'
_title: 'Add Faker Profile'
requirements:
_permission: 'administer faker profile configuration'

entity.faker_profile.edit_form:
path: '/admin/config/development/faker-profile/{faker_profile}'
defaults:
_entity_form: 'faker_profile.edit'
_title: 'Edit Faker Profile'
requirements:
_permission: 'administer faker profile configuration'

entity.faker_profile.delete_form:
path: '/admin/config/development/faker-profile/{faker_profile}/delete'
defaults:
_entity_form: 'faker_profile.delete'
_title: 'Delete Faker Profile'
requirements:
_permission: 'administer faker profile configuration'
6 changes: 3 additions & 3 deletions src/Annotation/FakerDataSampler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class FakerDataSampler extends Plugin {
public $label;

/**
* The plugin field type ID.
* The plugin field type IDs.
*
* @var string
* @var array
*/
public $fieldTypeId;
public $field_type_ids = [];

}
78 changes: 78 additions & 0 deletions src/Entity/FakerProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Drupal\faker\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\faker\FakerProfileInterface;

/**
* Defines the Faker Profile entity.
*
* @ConfigEntityType(
* id = "faker_profile",
* label = @Translation("Faker Profile"),
* handlers = {
* "list_builder" = "Drupal\faker\FakerProfileListBuilder",
* "form" = {
* "add" = "Drupal\faker\Form\FakerProfileForm",
* "edit" = "Drupal\faker\Form\FakerProfileForm",
* "delete" = "Drupal\Core\Entity\EntityDeleteForm",
* }
* },
* config_prefix = "faker_profile",
* admin_permission = "administer faker profile configuration",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "data_samplers" = "data_samplers",
* },
* config_export = {
* "id",
* "label",
* "data_samplers",
* },
* links = {
* "edit-form" = "/admin/config/development/faker-profile/{faker_profile}",
* "delete-form" = "/admin/config/development/faker-profile/{faker_profile}/delete",
* }
* )
*/
class FakerProfile extends ConfigEntityBase implements FakerProfileInterface {

/**
* The Faker Profile ID.
*
* @var string
*/
public $id;

/**
* The Faker Profile label.
*
* @var string
*/
public $label;

/**
* The Faker Profile data samplers.
*
* @var mixed
*/
public $data_samplers;

/**
* {@inheritdoc}
*/
public function getDataSamplers() {
return $this->data_samplers;
}

/**
* {@inheritdoc}
*/
public function setDataSamplers(array $data_samplers) {
$this->data_samplers = $data_samplers;
return $this;
}

}
9 changes: 7 additions & 2 deletions src/FakerConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
abstract class FakerConstants {

/**
* Faker checkbox form key.
* Faker Profile form key.
*/
public const USE_FAKER = 'use_faker';
public const PROFILE = 'faker_profile';

/**
* Faker locale form key.
*/
public const LOCALE = 'faker_locale';

}
7 changes: 5 additions & 2 deletions src/FakerDataSamplerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ abstract class FakerDataSamplerBase extends PluginBase implements FakerDataSampl
/**
* {@inheritdoc}
*/
public function getFieldTypeId() {
public function getFieldTypeIds() {
$plugin_definition = $this->getPluginDefinition();
return !empty($plugin_definition['fieldTypeId']) ? $plugin_definition['fieldTypeId'] : FALSE;
if (isset($plugin_definition['field_type_ids']) && $plugin_definition($plugin_definition['field_type_ids'])) {
return $plugin_definition['field_type_ids'];
}
return [];
}

}
8 changes: 5 additions & 3 deletions src/FakerDataSamplerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
interface FakerDataSamplerInterface {

/**
* Get relevant field type id.
* Get relevant field type ids.
*/
public function getFieldTypeId();
public function getFieldTypeIds();

/**
* Generate sample data with Faker.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* Field definition.
* @param string $faker_locale
* The locale to use if specified.
*/
public static function generateFakerValue(FieldDefinitionInterface $field_definition);
public static function generateFakerValue(FieldDefinitionInterface $field_definition, $faker_locale = NULL);

}
38 changes: 20 additions & 18 deletions src/FakerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ class FakerHelper {
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be enriched with sample field values.
* @param string $faker_profile_id
* The profile to use.
* @param string $faker_locale
* The locale to use if specified.
*
* @throws \Exception
* If plugin not found.
*/
public static function populateFields(EntityInterface $entity) {
public static function populateFields(EntityInterface $entity, $faker_profile_id, $faker_locale = NULL) {

// Check for fields that have a sampler plugin.
$field_type_sampler_manager = \Drupal::service('plugin.manager.faker_data_sampler');
$field_type_samplers_definitions = $field_type_sampler_manager->getDefinitions();
$faker_profile = \Drupal::entityTypeManager()->getStorage('faker_profile')->load($faker_profile_id);
$faker_profile_data_samplers = $faker_profile->getDataSamplers();

/** @var \Drupal\field\FieldConfigInterface[] $instances */
$instances = \Drupal::entityTypeManager()
Expand All @@ -43,27 +46,26 @@ public static function populateFields(EntityInterface $entity) {
}
$field_name = $field_storage->getName();
$field_definition = $entity->$field_name->getFieldDefinition();
$field_definition_type = $field_definition->getType();

// Keep track of the sampling type.
$faker_sampling = FALSE;

// Iterate through registered sampler definitions.
foreach ($field_type_samplers_definitions as $faker_sampler_id => $field_type_samplers_definition) {
// Match field type id with sampler.
if ($field_type_samplers_definition['fieldTypeId'] === $field_definition->getType()) {
/** @var \Drupal\faker\FakerDataSamplerInterface $faker_sampler */
$faker_sampler = $field_type_sampler_manager->createInstance($faker_sampler_id);
$values = [];
for ($delta = 0; $delta < $max; $delta++) {
$values[$delta] = $faker_sampler::generateFakerValue($field_definition);
}
$entity->$field_name->setValue($values);
$faker_sampling = TRUE;
break;
// Match field type id with sampler.
if (isset($faker_profile_data_samplers[$field_definition_type])) {
$faker_sampler_id = $faker_profile_data_samplers[$field_definition_type];
/** @var \Drupal\faker\FakerDataSamplerInterface $faker_sampler */
$faker_sampler = \Drupal::service('plugin.manager.faker_data_sampler')->createInstance($faker_sampler_id);
$values = [];
for ($delta = 0; $delta < $max; $delta++) {
$values[$delta] = $faker_sampler::generateFakerValue($field_definition, $faker_locale);
}
$entity->$field_name->setValue($values);
$faker_sampling = TRUE;
}

// Fallback to original core sample data population if faker sampling
// did not happen.
// did not happen or wasn't mapped to deviate from core sampling.
if ($faker_sampling === FALSE) {
$entity->$field_name->generateSampleItems($max);
}
Expand Down
30 changes: 30 additions & 0 deletions src/FakerProfileInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Drupal\faker;

use Drupal\Core\Config\Entity\ConfigEntityInterface;

/**
* Provides an interface defining the Faker Profile entity.
*/
interface FakerProfileInterface extends ConfigEntityInterface {

/**
* Get the Data Samplers.
*
* @return array
* An array of data sampler id keys.
*/
public function getDataSamplers();

/**
* Set the Data Samplers.
*
* @param array $data_samplers
* An array of data sampler id keys.
*
* @return $this
*/
public function setDataSamplers(array $data_samplers);

}
33 changes: 33 additions & 0 deletions src/FakerProfileListBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Drupal\faker;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;

/**
* Class FakerProfileListBuilder.
*
* @package Drupal\faker
*/
class FakerProfileListBuilder extends ConfigEntityListBuilder {

/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('Faker Profile');
$header['id'] = $this->t('Machine name');
return $header + parent::buildHeader();
}

/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
$row['id'] = $entity->id();
return $row + parent::buildRow($entity);
}

}
Loading

0 comments on commit f04d372

Please sign in to comment.