function DevelGenerateBase::populateFields
Same name in other branches
- 4.x devel_generate/src/DevelGenerateBase.php \Drupal\devel_generate\DevelGenerateBase::populateFields()
Populate the fields on a given entity with sample values.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity to be enriched with sample field values.
array $skip: A list of field names to avoid when populating.
array $base: A list of base field names to populate.
11 calls to DevelGenerateBase::populateFields()
- BlockContentDevelGenerate::develGenerateContentAddBlock in devel_generate/
src/ Plugin/ DevelGenerate/ BlockContentDevelGenerate.php - Create one block. Used by both batch and non-batch code branches.
- BlockContentDevelGenerate::develGenerateContentAddBlockTranslation in devel_generate/
src/ Plugin/ DevelGenerate/ BlockContentDevelGenerate.php - Create translation for the given block.
- ContentDevelGenerate::addNodeComments in devel_generate/
src/ Plugin/ DevelGenerate/ ContentDevelGenerate.php - Create comments and add them to a node.
- ContentDevelGenerate::develGenerateContentAddNode in devel_generate/
src/ Plugin/ DevelGenerate/ ContentDevelGenerate.php - Create one node. Used by both batch and non-batch code branches.
- ContentDevelGenerate::develGenerateContentAddNodeTranslation in devel_generate/
src/ Plugin/ DevelGenerate/ ContentDevelGenerate.php - Create translation for the given node.
File
-
devel_generate/
src/ DevelGenerateBase.php, line 138
Class
- DevelGenerateBase
- Provides a base DevelGenerate plugin implementation.
Namespace
Drupal\devel_generateCode
public function populateFields(EntityInterface $entity, array $skip = [], array $base = []) : void {
if (!$entity->getEntityType()
->entityClassImplements(FieldableEntityInterface::class)) {
// Nothing to do.
return;
}
$instances = $this->entityFieldManager
->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle());
$instances = array_diff_key($instances, array_flip($skip));
foreach ($instances as $instance) {
$field_storage = $instance->getFieldStorageDefinition();
$field_name = $field_storage->getName();
if ($field_storage->isBaseField() && !in_array($field_name, $base)) {
// Skip base field unless specifically requested.
continue;
}
$max = $field_storage->getCardinality();
$cardinality = $max;
if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
// Just an arbitrary number for 'unlimited'.
$max = random_int(1, 3);
}
$entity->{$field_name}
->generateSampleItems($max);
}
}