public static function CivicrmEntity::baseFieldDefinitions in CiviCRM Entity 8.3
Provides base field definitions for an entity type.
Implementations typically use the class \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; for example a 'name' field could be defined as the following:
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'));
By definition, base fields are fields that exist for every bundle. To provide definitions for fields that should only exist on some bundles, use \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().
The definitions returned by this function can be overridden for all bundles by hook_entity_base_field_info_alter() or overridden on a per-bundle basis via 'base_field_override' configuration entities.
Parameters
\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.
Return value
\Drupal\Core\Field\FieldDefinitionInterface[] An array of base field definitions for the entity type, keyed by field name.
Overrides ContentEntityBase::baseFieldDefinitions
See also
\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()
\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()
File
- src/
Entity/ CivicrmEntity.php, line 100
Class
- CivicrmEntity
- Entity class for CiviCRM entities.
Namespace
Drupal\civicrm_entity\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = [];
$civicrm_entity_info = SupportedEntities::getInfo()[$entity_type
->id()];
$civicrm_required_fields = !empty($civicrm_entity_info['required']) ? $civicrm_entity_info['required'] : [];
$field_definition_provider = \Drupal::service('civicrm_entity.field_definition_provider');
$civicrm_fields = \Drupal::service('civicrm_entity.api')
->getFields($entity_type
->get('civicrm_entity'), 'create');
foreach ($civicrm_fields as $civicrm_field) {
// Apply any additional field data provided by the module.
if (!empty($civicrm_entity_info['fields'][$civicrm_field['name']])) {
$civicrm_field = $civicrm_entity_info['fields'][$civicrm_field['name']] + $civicrm_field;
}
$fields[$civicrm_field['name']] = $field_definition_provider
->getBaseFieldDefinition($civicrm_field);
$fields[$civicrm_field['name']]
->setRequired(isset($civicrm_required_fields[$civicrm_field['name']]));
if ($values = \Drupal::service('civicrm_entity.api')
->getCustomFieldMetadata($civicrm_field['name'])) {
$fields[$civicrm_field['name']]
->setSetting('civicrm_entity_field_metadata', $values);
$fields[$civicrm_field['name']]
->setRequired((bool) $civicrm_field['is_required']);
}
}
// Placing the bundle field here is a bit of a hack work around.
// \Drupal\Core\Entity\ContentEntityStorageBase::initFieldValues will apply
// default values to all empty fields. The computed bundle field will
// provide a default value as well, for its related CiviCRM Entity field.
// By placing this field last, we avoid conflict on setting of the default
// value.
if ($entity_type
->hasKey('bundle')) {
$fields[$entity_type
->getKey('bundle')] = BaseFieldDefinition::create('string')
->setLabel($entity_type
->getBundleLabel())
->setRequired(TRUE)
->setReadOnly(TRUE)
->setClass(BundleFieldItemList::class);
}
// Provide a computed base field that takes the activity start time and
// appends the duration to calculated and end time.
if ($entity_type
->id() === 'civicrm_activity') {
$fields['activity_end_datetime'] = BaseFieldDefinition::create('datetime')
->setLabel(t('Activity End Date'))
->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME)
->setComputed(TRUE)
->setDisplayOptions('view', [
'type' => 'datetime_default',
'weight' => 0,
])
->setDisplayConfigurable('form', FALSE)
->setClass(ActivityEndDateFieldItemList::class);
}
return $fields;
}