You are here

public static function DevelGenerateBase::populateFields in Devel 8

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

6 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.
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
TermDevelGenerate::generateTerms in devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php
Generates taxonomy terms for a list of given vocabularies.
UserDevelGenerate::generateElements in devel_generate/src/Plugin/DevelGenerate/UserDevelGenerate.php
Business logic relating with each DevelGenerate plugin

... See full list

File

devel_generate/src/DevelGenerateBase.php, line 94

Class

DevelGenerateBase
Provides a base DevelGenerate plugin implementation.

Namespace

Drupal\devel_generate

Code

public static function populateFields(EntityInterface $entity) {

  /** @var \Drupal\field\FieldConfigInterface[] $instances */
  $instances = entity_load_multiple_by_properties('field_config', array(
    'entity_type' => $entity
      ->getEntityType()
      ->id(),
    'bundle' => $entity
      ->bundle(),
  ));
  if ($skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_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);
  }
}