You are here

public static function MediaGallery::baseFieldDefinitions in Media Gallery 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/MediaGallery.php, line 149

Class

MediaGallery
Defines the media gallery entity class.

Namespace

Drupal\media_gallery\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setDescription(t('The title of the media gallery entity.'))
    ->setRequired(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['description'] = BaseFieldDefinition::create('text_long')
    ->setLabel(t('Description'))
    ->setDescription(t('A description of the media gallery.'))
    ->setDisplayOptions('form', [
    'type' => 'text_textarea',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayOptions('view', [
    'type' => 'text_default',
    'label' => 'hidden',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['images'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Gallery images'))
    ->setSetting('target_type', 'media')
    ->SetCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->setDisplayConfigurable('form', FALSE)
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'media_library_widget',
    'weight' => 2,
  ])
    ->setDisplayOptions('view', [
    'type' => 'entity_reference_entity_view',
    'weight' => 2,
    'label' => 'hidden',
    'settings' => [
      'view_mode' => 'media_colorbox',
    ],
  ]);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Author'))
    ->setDescription(t('The user ID of the media gallery author.'))
    ->setSetting('target_type', 'user')
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => 60,
      'placeholder' => '',
    ],
    'weight' => 10,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'hidden',
    'weight' => 10,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('The time that the media gallery was created.'))
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'hidden',
    'weight' => 11,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'datetime_timestamp',
    'weight' => 11,
  ])
    ->setDisplayConfigurable('view', TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the media gallery was last edited.'));
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Published'))
    ->setDefaultValue(TRUE)
    ->setSetting('on_label', 'Published')
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => FALSE,
    ],
    'weight' => 12,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayOptions('view', [
    'type' => 'hidden',
    'label' => 'hidden',
    'weight' => 12,
    'settings' => [
      'format' => 'enabled-disabled',
    ],
  ])
    ->setDisplayConfigurable('view', TRUE);
  return $fields;
}