You are here

public static function DevelGenerateBase::populateFields in Devel 4.x

Same name and namespace in other branches
  1. 8.3 devel_generate/src/DevelGenerateBase.php \Drupal\devel_generate\DevelGenerateBase::populateFields()
  2. 8 devel_generate/src/DevelGenerateBase.php \Drupal\devel_generate\DevelGenerateBase::populateFields()
  3. 8.2 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.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

9 calls to DevelGenerateBase::populateFields()
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.
devel_generate_add_comments in devel_generate/devel_generate.module
Create comments and add them to a node.
ExampleDevelGenerate::generateElements in devel_generate/tests/modules/devel_generate_example/src/Plugin/DevelGenerate/ExampleDevelGenerate.php
Business logic relating with each DevelGenerate plugin.
MediaDevelGenerate::createMediaItem in devel_generate/src/Plugin/DevelGenerate/MediaDevelGenerate.php
Create one media item. Used by both batch and non-batch code branches.

... See full list

File

devel_generate/src/DevelGenerateBase.php, line 106

Class

DevelGenerateBase
Provides a base DevelGenerate plugin implementation.

Namespace

Drupal\devel_generate

Code

public static function populateFields(EntityInterface $entity) {
  $properties = [
    'entity_type' => $entity
      ->getEntityType()
      ->id(),
    'bundle' => $entity
      ->bundle(),
  ];
  $field_config_storage = \Drupal::entityTypeManager()
    ->getStorage('field_config');

  /* @var \Drupal\field\FieldConfigInterface[] $instances */
  $instances = $field_config_storage
    ->loadByProperties($properties);

  // @todo not implemented for Drush9+. Possibly remove.
  if ($skips = @$_REQUEST['skip-fields']) {
    foreach (explode(',', $skips) as $skip) {
      unset($instances[$skip]);
    }
  }
  foreach ($instances as $instance) {
    $field_storage = $instance
      ->getFieldStorageDefinition();
    $max = $cardinality = $field_storage
      ->getCardinality();
    if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {

      // Just an arbitrary number for 'unlimited'.
      $max = rand(1, 3);
    }
    $field_name = $field_storage
      ->getName();
    $entity->{$field_name}
      ->generateSampleItems($max);
  }
}