View source
<?php
namespace Drupal\image_slider\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\image_slider\SliderInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
class Slider extends ContentEntityBase implements SliderInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Slider entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Slider entity.'))
->setReadOnly(TRUE);
$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);
$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);
$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);
$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);
$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);
$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;
}
}