You are here

public static function Slider::baseFieldDefinitions in Image sliders 8

Define the field properties here.

Field name, type and size determine the table structure.

In addition, we can define how the field and its content can be manipulated in the GUI. The behaviour of the widgets used can be determined here.

Overrides ContentEntityBase::baseFieldDefinitions

File

src/Entity/Slider.php, line 119

Class

Slider
Defines the SliderEntityExample entity.

Namespace

Drupal\image_slider\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  // Standard field, used as unique if primary index.
  $fields['id'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('ID'))
    ->setDescription(t('The ID of the Slider entity.'))
    ->setReadOnly(TRUE);

  // Standard field, unique outside of the scope of the current project.
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The UUID of the Slider entity.'))
    ->setReadOnly(TRUE);

  // Name field for the slider.
  // We set display options for the view as well as the form.
  // Users with correct privileges can change the view and edit
  // configuration.
  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Name'))
    ->setDescription(t('The name of the Slider entity.'))
    ->setSettings([
    'max_length' => 255,
    'text_processing' => 0,
  ])
    ->setDefaultValue(NULL)
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'string',
    'weight' => -6,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -6,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setRequired(TRUE);

  // Description field for the slider.
  // We set display options for the view as well as the form.
  // Users with correct privileges can change the view and edit
  // configuration.
  $fields['description'] = BaseFieldDefinition::create('text_long')
    ->setLabel(t('Description'))
    ->setDescription(t('The description.'))
    ->setRevisionable(TRUE)
    ->setSettings([
    'max_length' => 1000,
    'text_processing' => 0,
  ])
    ->setDefaultValue('')
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'string',
    'weight' => -4,
  ])
    ->setDisplayOptions('form', [
    'type' => 'text_textarea ',
    'rows' => 0,
    'weight' => -4,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setRequired(TRUE);

  // ImaGE field for the slider.
  // We set display options for the view as well as the form.
  // Users with correct privileges can change the view and edit
  // configuration.
  $fields['image'] = BaseFieldDefinition::create('image')
    ->setLabel(t('Image'))
    ->setDescription(t('Image field'))
    ->setSettings([
    'file_directory' => 'slider_list',
    'alt_field_required' => TRUE,
    'title_field_required' => 1,
    'file_extensions' => 'png jpg jpeg',
  ])
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'default',
    'weight' => -3,
  ])
    ->setDisplayOptions('form', [
    'label' => 'above',
    'type' => 'image_image',
    'weight' => -3,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setRequired(TRUE)
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

  // Slider type field for the list of slider category.
  // ListTextType with a drop down menu widget.
  // The values shown in the menu are 'male' and 'female'.
  // In the view the field content is shown as string.
  // In the form the choices are presented as options list.
  $fields['slide_type'] = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Slider type'))
    ->setDescription(t('The slider type entity.'))
    ->setSettings([
    'allowed_values' => [
      'full-width-slider' => 'Full Width Slider',
      'image-slider' => 'Image Slider',
      'image-gallery' => 'Image Gallery',
      'image-gallery-with-vertical-thumbnail' => 'Image Gallery With Vertical Thumbnail',
      'scrolling-logo-thumbnail-slider' => 'Scrolling Logo Thumbnail Slider',
      'full-window-for-pc' => 'Full window for pc',
      'different-size-photo-slider' => 'Different Size Photo Slider',
      'nearby-image-partial-visible-slider' => 'Nearby Image Partial Visible Slider',
      'carousel-slider' => 'Carousel Slider',
      'banner-slider' => 'Banner Slider',
      'banner-rotator' => 'Banner Rotator',
    ],
  ])
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'list_default',
    'weight' => -4,
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => -4,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setRequired(TRUE);

  // Owner field of the slider.
  // Entity reference field, holds the reference to the user object.
  // The view shows the user name field of the user.
  // The form presents a auto complete field for the user name.
  $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('User Name'))
    ->setDescription(t('The Name of the associated user.'))
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'author',
    'weight' => -3,
  ])
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => 60,
      'placeholder' => '',
    ],
    'weight' => -3,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);

  // Role field for the slider.
  // The values shown in options are 'administrator' and 'user'.
  $fields['role'] = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Role'))
    ->setDescription(t('The role of the Slider entity.'))
    ->setSettings([
    'allowed_values' => [
      'administrator' => 'administrator',
      'user' => 'user',
    ],
  ])
    ->setDefaultValue('user')
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'string',
    'weight' => -2,
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => -2,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language code'))
    ->setDescription(t('The language code of SliderEntityExample entity.'));
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time that the entity was created.'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the entity was last edited.'));
  return $fields;
}