You are here

public static function SmsMessage::baseFieldDefinitions in SMS Framework 8

Same name and namespace in other branches
  1. 2.x src/Entity/SmsMessage.php \Drupal\sms\Entity\SmsMessage::baseFieldDefinitions()
  2. 2.1.x src/Entity/SmsMessage.php \Drupal\sms\Entity\SmsMessage::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/SmsMessage.php, line 469

Class

SmsMessage
Defines the SMS message entity.

Namespace

Drupal\sms\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  // Identifiers.
  $fields['id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('SMS message ID'))
    ->setDescription(t('The SMS message ID.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The SMS message UUID.'))
    ->setReadOnly(TRUE);
  $fields['gateway'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Gateway Plugin'))
    ->setDescription(t('The gateway plugin instance.'))
    ->setSetting('target_type', 'sms_gateway')
    ->setReadOnly(TRUE)
    ->setRequired(TRUE);
  $fields['direction'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Transmission direction'))
    ->setDescription(t('Transmission direction, See SmsMessageInterface::DIRECTION_*.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', FALSE)
    ->setSetting('size', 'tiny')
    ->setRequired(TRUE);

  // Sender and receivers.
  $fields['sender_name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Sender name'))
    ->setDescription(t('The name of the sender.'))
    ->setRequired(FALSE);
  $fields['sender_phone_number'] = BaseFieldDefinition::create('telephone')
    ->setLabel(t('Sender phone number'))
    ->setDescription(t('The phone number of the sender.'))
    ->setDefaultValue('')
    ->setRequired(FALSE);
  $fields['sender_entity'] = BaseFieldDefinition::create('dynamic_entity_reference')
    ->setLabel(t('Sender entity'))
    ->setDescription(t('The entity who sent the SMS message.'))
    ->setRequired(FALSE);
  $fields['recipient_phone_number'] = BaseFieldDefinition::create('telephone')
    ->setLabel(t('Recipient phone number'))
    ->setDescription(t('The phone number of the recipient.'))
    ->setRequired(FALSE)
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED);
  $fields['recipient_entity'] = BaseFieldDefinition::create('dynamic_entity_reference')
    ->setLabel(t('Recipient entity'))
    ->setDescription(t('The entity who received the SMS message.'))
    ->setRequired(FALSE);

  // Meta information.
  $fields['options'] = BaseFieldDefinition::create('map')
    ->setLabel(t('Options'))
    ->setDescription(t('Options to pass to the gateway.'));
  $fields['automated'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Is automated'))
    ->setDescription(t('Whether this SMS message was generated automatically. 0=generated by user action, 1=generated automatically.'))
    ->setDefaultValue(TRUE)
    ->setRequired(TRUE)
    ->setSetting('on_label', t('Automated'))
    ->setSetting('off_label', t('Not automated'));
  $fields['queued'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Queued'))
    ->setDescription(t('Whether the SMS message is in the queue to be processed.'))
    ->setDefaultValue(FALSE)
    ->setRequired(TRUE)
    ->setSetting('on_label', t('Queued'))
    ->setSetting('off_label', t('Not queued'));

  // Dates.
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Creation date'))
    ->setDescription(t('The time the SMS message was created.'))
    ->setRequired(TRUE);
  $fields['send_on'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Send date'))
    ->setDescription(t('The time to send the SMS message.'))
    ->setRequired(TRUE);
  $fields['processed'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Processed'))
    ->setDescription(t('The time the SMS message was processed. This value does not indicate whether the message was sent, only that the gateway accepted the request.'))
    ->setRequired(FALSE);

  // Message contents.
  $fields['message'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Message'))
    ->setDescription(t('The SMS message.'))
    ->setDefaultValue('')
    ->setRequired(TRUE);
  return $fields;
}