You are here

public static function PushNotificationsToken::baseFieldDefinitions in Push Notifications 8

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/PushNotificationsToken.php, line 141
Contains \Drupal\push_notifications\Entity\PushNotificationsToken.

Class

PushNotificationsToken
Defines the token entity.

Namespace

Drupal\push_notifications\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  // Standard field, used as unique if primary key.
  $fields['id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('ID'))
    ->setDescription(t('Push notifications ID.'))
    ->setReadOnly(TRUE);

  // UUID field.
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The UUID of the push notifications token.'))
    ->setReadOnly(TRUE);

  // User ID.
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('User Name'))
    ->setDescription(t('The token owner.'))
    ->setSetting('target_type', 'user')
    ->setDisplayOptions('view', array(
    'label' => 'inline',
    'type' => 'string',
    'weight' => 0,
  ))
    ->setDisplayConfigurable('view', TRUE);

  // Token.
  $fields['token'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Token'))
    ->setDescription(t('Device Token'))
    ->setRequired(TRUE)
    ->setSettings(array(
    'max_length' => 255,
  ))
    ->setDisplayOptions('view', array(
    'label' => 'inline',
    'type' => 'string',
    'weight' => 1,
  ))
    ->setDisplayConfigurable('view', TRUE)
    ->addConstraint('PushNotificationsTokenUnique');

  // Network.
  $fields['network'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Network'))
    ->setDescription(t('Network Type'))
    ->setRequired(TRUE)
    ->setSettings(array(
    'max_length' => 255,
  ))
    ->setDisplayOptions('view', array(
    'label' => 'inline',
    'type' => 'string',
    'weight' => 2,
  ))
    ->setDisplayConfigurable('view', TRUE);

  // Timestamp.
  $fields['created'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Created'))
    ->setDescription(t('Timestamp the token was added.'))
    ->setRequired(TRUE)
    ->setDefaultValue(REQUEST_TIME)
    ->setDisplayOptions('view', array(
    'label' => 'inline',
    'type' => 'string',
    'weight' => 3,
  ))
    ->setDisplayConfigurable('view', TRUE);

  // Language code.
  // If no language code is provided when entity is created,
  // the language code is set to the default language of the site.
  $fields['langcode'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Language'))
    ->setDescription(t('The language associated with this token.'))
    ->setSettings(array(
    'max_length' => 255,
  ))
    ->setRequired(TRUE)
    ->addConstraint('PushNotificationsTokenLanguage')
    ->setDisplayOptions('view', array(
    'label' => 'inline',
    'type' => 'string',
    'weight' => 4,
  ))
    ->setDisplayConfigurable('view', TRUE);
  return $fields;
}