protected function FarmFieldFactory::buildFieldDefinition in farmOS 2.x
Builds a field definition with farmOS opinions.
Parameters
\Drupal\Core\Field\BaseFieldDefinition &$field: A base field definition object.
array $options: An array of options.
2 calls to FarmFieldFactory::buildFieldDefinition()
- FarmFieldFactory::baseFieldDefinition in modules/
core/ field/ src/ FarmFieldFactory.php - Generate a base field definition.
- FarmFieldFactory::bundleFieldDefinition in modules/
core/ field/ src/ FarmFieldFactory.php - Generates a bundle field definition.
File
- modules/
core/ field/ src/ FarmFieldFactory.php, line 53
Class
- FarmFieldFactory
- Factory for generating farmOS field definitions.
Namespace
Drupal\farm_fieldCode
protected function buildFieldDefinition(BaseFieldDefinition &$field, array $options = []) {
// Set label.
if (!empty($options['label'])) {
$field
->setLabel($options['label']);
}
// Set description.
if (!empty($options['description'])) {
$field
->setDescription($options['description']);
}
// Set computed.
if (!empty($options['computed'])) {
$field
->setComputed(TRUE);
$field
->setClass($options['computed']);
}
// Make the field required, if specified.
if (!empty($options['required'])) {
$field
->setRequired(TRUE);
}
// Make the field revisionable, unless told otherwise.
if (empty($options['revisionable'])) {
$field
->setRevisionable(TRUE);
}
else {
$field
->setRevisionable(FALSE);
}
// Set cardinality, if specified.
if (!empty($options['cardinality'])) {
$field
->setCardinality($options['cardinality']);
}
elseif (!empty($options['multiple'])) {
$field
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
}
else {
$field
->setCardinality(1);
}
// Only make the field translatable if specified.
if (empty($options['translatable'])) {
$field
->setTranslatable(FALSE);
}
else {
$field
->setTranslatable(TRUE);
}
// Set the default value callback, if specified.
if (!empty($options['default_value_callback'])) {
$field
->setDefaultValueCallback($options['default_value_callback']);
}
// Add third-party settings, if specified.
if (!empty($options['third_party_settings'])) {
$field
->setSetting('third_party_settings', $options['third_party_settings']);
}
// Delegate to per-type helper functions to fill in more details.
switch ($options['type']) {
case 'boolean':
$this
->modifyBooleanField($field, $options);
break;
case 'entity_reference':
$this
->modifyEntityReferenceField($field, $options);
break;
case 'entity_reference_revisions':
$this
->modifyEntityReferenceRevisionsField($field, $options);
break;
case 'file':
case 'image':
$this
->modifyFileField($field, $options);
break;
case 'fraction':
$this
->modifyFractionField($field, $options);
break;
case 'geofield':
$this
->modifyGeofieldField($field, $options);
break;
case 'id_tag':
$this
->modifyIdTagField($field, $options);
break;
case 'inventory':
$this
->modifyInventoryField($field, $options);
break;
case 'list_string':
$this
->modifyListStringField($field, $options);
break;
case 'string':
case 'string_long':
$this
->modifyStringField($field, $options);
break;
case 'text_long':
$this
->modifyTextField($field, $options);
break;
case 'timestamp':
$this
->modifyTimestampField($field, $options);
break;
default:
throw new FieldException('Unsupported field type.');
}
// Hide the field in form and view displays, if specified.
// The hidden option can either be set to TRUE, which will hide it in both
// form and view displays, or it can be set to "form" or "view", which will
// only hide it in the form or view display.
if (!empty($options['hidden'])) {
$display_options = [
'region' => 'hidden',
];
if ($options['hidden'] === TRUE || $options['hidden'] === 'form') {
$field
->setDisplayOptions('form', $display_options);
}
if ($options['hidden'] === TRUE || $options['hidden'] === 'view') {
$field
->setDisplayOptions('view', $display_options);
}
}
// Make the form and view displays configurable.
$field
->setDisplayConfigurable('form', TRUE);
$field
->setDisplayConfigurable('view', TRUE);
// Override form and view display options, if specified.
foreach ([
'form',
'view',
] as $display_type) {
$key = $display_type . '_display_options';
if (isset($options[$key])) {
$field
->setDisplayOptions($display_type, $options[$key]);
}
}
}