You are here

public static function SmartDateRule::baseFieldDefinitions in Smart Date 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::baseFieldDefinitions()
  2. 3.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::baseFieldDefinitions()
  3. 3.1.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::baseFieldDefinitions()
  4. 3.2.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::baseFieldDefinitions()
  5. 3.3.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::baseFieldDefinitions()
  6. 3.4.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::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

modules/smart_date_recur/src/Entity/SmartDateRule.php, line 644

Class

SmartDateRule
Defines the Smart date rule entity.

Namespace

Drupal\smart_date_recur\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['rule'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Rule'))
    ->setDescription(t('The Rule that will be used to generate instances.'))
    ->setSettings([
    'max_length' => 256,
    'text_processing' => 0,
  ])
    ->setDefaultValue('')
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -4,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -4,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setRevisionable(TRUE);

  // Separate storage for the frequency.
  $fields['freq'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Frequency'))
    ->setDescription(t('How often the date recurs.'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', 7)
    ->setRequired(TRUE);

  // Separate storage for the limit.
  $fields['limit'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Limit'))
    ->setDescription(t('A constraint on how long to recur.'))
    ->setSetting('max_length', 25)
    ->setSetting('is_ascii', TRUE);

  // Separate storage for extra parameters such as INTERVAL or BYMONTHDAY.
  // NOTE: The intention is to store these semicolon-separated.
  $fields['parameters'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Parameters'))
    ->setDescription(t('Additional parameters to define the recurrence.'))
    ->setSetting('is_ascii', TRUE);

  // TODO: Decide if this field is necessary, given the presence of the Limit.
  $fields['unlimited'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Unlimited'))
    ->setDescription(t('Whether or not the rule has a limit or end.'))
    ->setDefaultValue(TRUE)
    ->setReadOnly(TRUE)
    ->setRevisionable(TRUE);
  $fields['entity_type'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Entity type'))
    ->setDescription(t('The entity type on which the date is set.'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);
  $fields['bundle'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Bundle'))
    ->setDescription(t('The bundle on which the date is set.'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);
  $fields['field_name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Smart Date field name'))
    ->setDescription(t('The field name on which the date is set.'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', FieldStorageConfig::NAME_MAX_LENGTH);
  $fields['start'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Start timestamp value'))
    ->setRequired(TRUE);
  $fields['end'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('End timestamp value'))
    ->setRequired(TRUE);
  $fields['instances'] = BaseFieldDefinition::create('map')
    ->setLabel(t('Instances'))
    ->setDescription(t('A serialized array of the instances.'));
  return $fields;
}