public function MathfieldTestCase::createField in Math Field 7
Create a number field and instance.
Parameters
string $field_name: The name of the field to create.
string $label: The field label.
string $type: The field type. Default is 'number_integer'.
int $cardinality: The number of values. Default is 1.
string $widget: The form widget. Default is 'number'.
8 calls to MathfieldTestCase::createField()
- MathfieldListTestCase::testMathfieldRadioList in ./
mathfield.test - Tests using values from radio button groups in a math expression.
- MathfieldListTestCase::testMathfieldSelectList in ./
mathfield.test - Tests using values from select lists in a math expression.
- MathfieldTokenTestCase::testMathfieldAdvancedTokens in ./
mathfield.test - Tests advanced tokens in math expression fields.
- MathfieldTokenTestCase::testMathfieldBasicTokens in ./
mathfield.test - Tests basic tokens in math expression fields.
- MathfieldTokenTestCase::testMathfieldDefaultTokens in ./
mathfield.test - Tests using another math field as a tokens in a math expression.
File
- ./
mathfield.test, line 59 - Tests for mathfield.module.
Class
- MathfieldTestCase
- Base class for testing the mathfield module.
Code
public function createField($field_name, $label, $type, $cardinality = 1, $widget = 'number') {
// Create the field.
$field = array(
'field_name' => $field_name,
'type' => $type,
'cardinality' => $cardinality,
);
if (strpos($type, 'list_') !== FALSE) {
if ($type == 'list_float') {
// Generate a list of 10 floats.
for ($i = 0; $i < 10; $i++) {
$float = $this
->randFloat();
$field['settings']['allowed_values'][$float] = $float;
}
}
else {
// Generate a list of 10 integers.
for ($i = 0; $i < 10; $i++) {
$int = $this
->randInteger();
$field['settings']['allowed_values'][$int] = $int;
}
}
}
$field = field_create_field($field);
// Create the instance.
$instance = array(
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => 'math',
'label' => $label,
'weight' => mt_rand(0, 127),
'widget' => array(
'type' => $widget,
'label' => $label,
),
);
field_create_instance($instance);
}