You are here

public static function BackgroundImage::baseFieldDefinitions in Background Image 8

Same name and namespace in other branches
  1. 2.x src/Entity/BackgroundImage.php \Drupal\background_image\Entity\BackgroundImage::baseFieldDefinitions()
  2. 2.0.x src/Entity/BackgroundImage.php \Drupal\background_image\Entity\BackgroundImage::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/BackgroundImage.php, line 147

Class

BackgroundImage
Defines the Background Image entity.

Namespace

Drupal\background_image\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('The time that the background image was created.'))
    ->setDisplayOptions('view', [
    'region' => 'hidden',
  ]);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the background image was last edited.'))
    ->setDisplayOptions('view', [
    'region' => 'hidden',
  ]);
  $fields['image'] = BaseFieldDefinition::create('image')
    ->setLabel(t('Image'))
    ->setDescription(t('The background image to display.'))
    ->setSettings([
    'file_directory' => 'background_image',
    'alt_field' => 0,
    'alt_field_required' => 0,
    'title_field' => 0,
    'title_field_required' => 0,
    'max_resolution' => '',
    'min_resolution' => '',
    'default_image' => [
      'uuid' => NULL,
      'alt' => '',
      'title' => '',
      'width' => NULL,
      'height' => NULL,
    ],
  ])
    ->setDisplayOptions('form', [
    'type' => 'image_image',
    'weight' => 1,
  ])
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'weight' => 0,
  ]);
  $fields['label'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Label'))
    ->setDescription(t('An administrative description to help identify this specific background image.'))
    ->setRequired(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue('');
  $fields['settings'] = BaseFieldDefinition::create('map')
    ->setLabel(t('Settings'))
    ->setDescription(t('Specific settings for this background image.'))
    ->setTranslatable(TRUE);
  $fields['type'] = BaseFieldDefinition::create('list_integer')
    ->setLabel(t('Type'))
    ->setDescription(t('Choose when this background image should be displayed.'))
    ->setRequired(TRUE)
    ->setDefaultValue(self::TYPE_GLOBAL)
    ->setSettings([
    'allowed_values' => self::getTypes(),
  ]);
  $fields['target'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Target'))
    ->setDescription(t('A target, if any.'))
    ->setDefaultValue('');
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('User'))
    ->setDescription(t('An entity reference to the user who created this background image.'))
    ->addConstraint('ReferenceAccess')
    ->addConstraint('ValidReference')
    ->setRequired(TRUE)
    ->setSettings([
    'target_type' => 'user',
  ]);
  return $fields;
}