function bat_event_series_type_add_event_state_reference in Booking and Availability Management Tools for Drupal 8
Create a field of type 'Bat Event State Reference' to reference an Event State.
1 call to bat_event_series_type_add_event_state_reference()
- EventSeriesType::save in modules/
bat_event_series/ src/ Entity/ EventSeriesType.php - Saves an entity permanently.
File
- modules/
bat_event_series/ bat_event_series.module, line 21
Code
function bat_event_series_type_add_event_state_reference($bundle) {
$field_name = 'event_state_reference';
$field_storage = FieldStorageConfig::loadByName('bat_event_series', $field_name);
$field = FieldConfig::loadByName('bat_event_series', $bundle, $field_name);
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'bat_event_series',
'type' => 'entity_reference',
'cardinality' => 1,
'locked' => 1,
'settings' => [
'target_type' => 'state',
],
]);
$field_storage
->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'entity_type' => 'bat_event_series',
'label' => 'State',
'bundle' => $bundle,
'required' => TRUE,
'settings' => [
'handler' => 'default',
'handler_settings' => [],
],
]);
$field
->save();
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('bat_event_series.' . $bundle . '.default');
if (!$form_display) {
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'bat_event_series',
'bundle' => $bundle,
'mode' => 'default',
'status' => TRUE,
]);
}
$form_display
->setComponent($field_name, [
'type' => 'entity_reference_autocomplete',
'weight' => 3,
]);
$form_display
->save();
}
}