X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fdrupal%2Fconsole%2Fsrc%2FUtils%2FCreate%2FBase.php;fp=vendor%2Fdrupal%2Fconsole%2Fsrc%2FUtils%2FCreate%2FBase.php;h=660c778c7fca259ce4ad13b83e1fc7a56d348ebd;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/drupal/console/src/Utils/Create/Base.php b/vendor/drupal/console/src/Utils/Create/Base.php new file mode 100644 index 000000000..660c778c7 --- /dev/null +++ b/vendor/drupal/console/src/Utils/Create/Base.php @@ -0,0 +1,125 @@ +entityTypeManager = $entityTypeManager; + $this->entityFieldManager = $entityFieldManager; + $this->dateFormatter = $dateFormatter; + } + + /** + * @param $entity + * @return array + */ + private function getFields($entity) + { + $entityTypeId = $entity->getEntityType()->id(); + $bundle = $entity->bundle(); + + $fields = array_filter( + $this->entityFieldManager->getFieldDefinitions($entityTypeId, $bundle), function ($fieldDefinition) { + return $fieldDefinition instanceof FieldConfigInterface; + } + ); + + return $fields; + } + + /** + * @param + * @param $entity + */ + protected function generateFieldSampleData($entity) + { + $fields = $this->getFields($entity); + + /* @var \Drupal\field\FieldConfigInterface[] $fields */ + foreach ($fields as $field) { + $fieldName = $field->getFieldStorageDefinition()->getName(); + $cardinality = $field->getFieldStorageDefinition()->getCardinality( + ); + if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) { + $cardinality = rand(1, 5); + } + + $entity->$fieldName->generateSampleItems($cardinality); + } + } + + /** + * Returns the random data generator. + * + * @return \Drupal\Component\Utility\Random + * The random data generator. + */ + protected function getRandom() + { + if (!$this->random) { + $this->random = new Random(); + } + + return $this->random; + } + + /** + * Retrieve a random Uid of enabled users. + * + * @return array + */ + protected function getUserId() + { + if (!$this->users) { + $userStorage = $this->entityTypeManager->getStorage('user'); + + $this->users = $userStorage->loadByProperties(['status' => true]); + } + + return array_rand($this->users); + } +}