You are here

public static function WeatherForecast::baseFieldDefinitions in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Entity/WeatherForecast.php \Drupal\weather\Entity\WeatherForecast::baseFieldDefinitions()

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/WeatherForecast.php, line 29

Class

WeatherForecast
Defines the Weather forecast entity.

Namespace

Drupal\weather\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = [];
  $fields['id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('ID'))
    ->setDescription(t('ID.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['geoid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Geoid'))
    ->setRequired(TRUE)
    ->setDescription('GeoID of the location')
    ->setSetting('target_type', 'weather_place');
  $fields['time_from'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Start time of forecast'))
    ->setDescription('Start time of forecast')
    ->setRequired(TRUE)
    ->setSetting('max_length', 20);
  $fields['time_to'] = BaseFieldDefinition::create('string')
    ->setLabel(t('End time of forecast'))
    ->setDescription('End time of forecast')
    ->setRequired(TRUE)
    ->setSetting('max_length', 20);
  $fields['period'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Period of day'))
    ->setDescription('Period of day')
    ->setRequired(TRUE)
    ->setSetting('max_length', 1);
  $fields['symbol'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Symbol to use for weather display'))
    ->setDescription('Symbol to use for weather display')
    ->setRequired(TRUE)
    ->setSetting('max_length', 3);
  $fields['precipitation'] = BaseFieldDefinition::create('float')
    ->setLabel(t('Precipitation'))
    ->setDescription('Amount of precipitation in mm')
    ->setDefaultValue(NULL);
  $fields['wind_direction'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Wind direction'))
    ->setDescription(t('Wind direction in degrees'))
    ->setDefaultValue(NULL);
  $fields['wind_speed'] = BaseFieldDefinition::create('float')
    ->setLabel(t('Wind speed'))
    ->setDescription('Wind speed in m/s')
    ->setDefaultValue(NULL);
  $fields['temperature'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Temperature'))
    ->setDescription(t('Temperature in degree celsius'))
    ->setDefaultValue(NULL);
  $fields['pressure'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Pressure'))
    ->setDescription(t('Pressure in hPa'))
    ->setDefaultValue(NULL);
  return $fields;
}