You are here

function farm_location_asset_base_fields in farmOS 2.x

Define asset location base fields.

1 call to farm_location_asset_base_fields()
farm_location_entity_base_field_info in modules/core/location/farm_location.module
Implements hook_entity_base_field_info().

File

modules/core/location/farm_location.base_fields.inc, line 14
Code for creating farmOS entity location base field definitions.

Code

function farm_location_asset_base_fields() {
  $fields = [];

  // Current location field.
  // This is computed based on an asset's movements.
  $options = [
    'type' => 'entity_reference',
    'label' => t('Current location'),
    'target_type' => 'asset',
    'multiple' => TRUE,
    'computed' => AssetLocationItemList::class,
    'hidden' => 'form',
    'weight' => [
      'view' => 95,
    ],
  ];
  $fields['location'] = \Drupal::service('farm_field.factory')
    ->baseFieldDefinition($options);

  // Current geometry field.
  // This is computed based on an asset's movements or its intrinsic geometry.
  $options = [
    'type' => 'geofield',
    'label' => t('Current geometry'),
    'computed' => AssetGeometryItemList::class,
    'hidden' => 'form',
    'weight' => [
      'view' => 95,
    ],
  ];
  $fields['geometry'] = \Drupal::service('farm_field.factory')
    ->baseFieldDefinition($options);

  // Intrinsic geometry field.
  // This is added as a bundle field definition to all asset types rather than
  // a base field definition so that data is stored in a dedicated database
  // table.
  $options = [
    'type' => 'geofield',
    'label' => t('Intrinsic geometry'),
    'description' => t('Add geometry data to this asset to describe its intrinsic location. This will only be used if the asset is fixed.'),
    'weight' => [
      'form' => 96,
    ],
    'hidden' => 'view',
    'populate_file_field' => 'file',
  ];
  $fields['intrinsic_geometry'] = \Drupal::service('farm_field.factory')
    ->bundleFieldDefinition($options);

  // Location boolean field.
  $options = [
    'type' => 'boolean',
    'label' => t('Is location'),
    'description' => t('If this asset is a location, then other assets can be moved to it.'),
    'default_value_callback' => 'farm_location_is_location_default_value',
    'weight' => [
      'form' => 95,
    ],
    'view_display_options' => [
      'label' => 'inline',
      'type' => 'hideable_boolean',
      'settings' => [
        'format' => 'default',
        'format_custom_false' => '',
        'format_custom_true' => '',
        'hide_if_false' => TRUE,
      ],
      'weight' => 95,
    ],
  ];
  $fields['is_location'] = \Drupal::service('farm_field.factory')
    ->baseFieldDefinition($options);

  // Fixed boolean field.
  $options = [
    'type' => 'boolean',
    'label' => t('Is fixed'),
    'description' => t('If this asset is fixed, then it can have an intrinsic geometry. If the asset will move around, then it is not fixed and geometry will be determined by movement logs.'),
    'default_value_callback' => 'farm_location_is_fixed_default_value',
    'weight' => [
      'form' => 95,
    ],
    'view_display_options' => [
      'label' => 'inline',
      'type' => 'hideable_boolean',
      'settings' => [
        'format' => 'default',
        'format_custom_false' => '',
        'format_custom_true' => '',
        'hide_if_false' => TRUE,
      ],
      'weight' => 96,
    ],
  ];
  $fields['is_fixed'] = \Drupal::service('farm_field.factory')
    ->baseFieldDefinition($options);
  return $fields;
}