You are here

function bat_booking_add_event_reference_field in Booking and Availability Management Tools for Drupal 8

Same name and namespace in other branches
  1. 7 modules/bat_booking/bat_booking.module \bat_booking_add_event_reference_field()

Add "Event" reference field.

1 call to bat_booking_add_event_reference_field()
bat_booking_entity_insert in modules/bat_booking/bat_booking.module
Implements hook_entity_insert().

File

modules/bat_booking/bat_booking.module, line 316

Code

function bat_booking_add_event_reference_field($bundle) {
  $field_name = 'booking_event_reference';
  $field_storage = FieldStorageConfig::loadByName('bat_booking', $field_name);
  $field = FieldConfig::loadByName('bat_booking', $bundle, $field_name);
  if (empty($field_storage)) {
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'bat_booking',
      'type' => 'entity_reference',
      'cardinality' => 1,
      'locked' => 1,
      'settings' => [
        'target_type' => 'bat_event',
      ],
    ]);
    $field_storage
      ->save();
  }
  if (empty($field)) {
    $field = FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'bat_booking',
      'label' => 'Event',
      'bundle' => $bundle,
      'required' => FALSE,
    ]);
    $field
      ->save();
  }
}