View source
<?php
namespace Drupal\Tests\cck_select_other\TestSite;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Random;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\TestSite\TestSetupInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;
class SelectOtherInstallTestScript implements TestSetupInterface {
protected $randomGenerator;
protected $modules = [
'node',
'field',
'field_ui',
'options',
'text',
'cck_select_other',
];
public function setup() {
\Drupal::service('module_installer')
->install($this->modules);
$typeId = $this
->createContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$options = $this
->createOptions();
list($default_value, $label) = $this
->getRandomOption($options);
$storage_values = [
'field_name' => 'field_random_choice',
'settings' => [
'allowed_values' => $options,
],
'cardinality' => 1,
];
$config_values = [
'field_name' => 'field_random_choice',
'label' => 'Random choice',
'required' => 0,
'default_value' => [
[
'value' => $default_value,
],
],
];
$this
->createSelectOtherListField('node', $typeId, 'list_string', $storage_values, $config_values);
\Drupal::cache()
->deleteAll();
}
protected function createContentType(array $values = []) {
if (!isset($values['type'])) {
$id = $this
->randomMachineName();
}
else {
$id = $values['type'];
}
$values += [
'type' => $id,
'name' => $id,
];
$type = NodeType::create($values);
$type
->save();
$formDisplay = EntityFormDisplay::create([
'targetEntityType' => 'node',
'bundle' => $id,
'mode' => 'default',
'status' => TRUE,
]);
$formDisplay
->save();
$viewDisplay = EntityViewDisplay::create([
'targetEntityType' => 'node',
'bundle' => $id,
'mode' => 'default',
'status' => TRUE,
]);
$viewDisplay
->save();
return $id;
}
protected function createSelectOtherListField($entity_type = 'node', $bundle = 'page', $type = 'list_string', array $fieldInfo = [], array $instanceInfo = []) {
$random = $this
->getRandomGenerator();
if (!isset($fieldInfo['field_name'])) {
$fieldInfo['field_name'] = strtolower($random
->name(8, TRUE));
}
$storage_values = NestedArray::mergeDeep($fieldInfo, [
'entity_type' => $entity_type,
'type' => $type,
]);
$fieldStorage = FieldStorageConfig::create($storage_values);
$fieldStorage
->save();
if (!isset($instanceInfo['label'])) {
$instanceInfo['label'] = $random
->string(15);
}
$field_values = NestedArray::mergeDeep($instanceInfo, [
'field_name' => $fieldStorage
->getName(),
'entity_type' => $entity_type,
'bundle' => $bundle,
]);
$field = FieldConfig::create($field_values);
$field
->save();
$display_id = $entity_type . '.' . $bundle . '.default';
$formDisplay = EntityFormDisplay::load($display_id);
$formDisplay
->setComponent($fieldStorage
->getName(), [
'type' => 'cck_select_other',
'weight' => -1,
]);
$formDisplay
->save();
$viewDisplay = EntityViewDisplay::load($display_id);
$viewDisplay
->setComponent($fieldStorage
->getName(), [
'type' => 'cck_select_other',
'weight' => -1,
]);
$viewDisplay
->save();
return $fieldStorage;
}
public function randomMachineName($length = 8) {
return $this
->getRandomGenerator()
->name($length, TRUE);
}
protected function getRandomGenerator() {
if (!is_object($this->randomGenerator)) {
$this->randomGenerator = new Random();
}
return $this->randomGenerator;
}
public function createOptions($num = 5, $type = 'list_string') {
$options = [];
for ($i = 0; $i < $num; $i++) {
if ($type === 'list_string') {
$label = $this
->getRandomGenerator()
->word(10);
$key = strtolower($label);
}
else {
$label = $i;
$key = $i;
}
$options[$key] = $label;
}
return $options;
}
public function getRandomOption(array $options) {
$option = array_rand($options);
return [
$option,
$options[$option],
];
}
}