You are here

Slider.php in Image sliders 8

File

src/Entity/Slider.php
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;

/**
 * Defines the SliderEntityExample entity.
 *
 * @ingroup image_slider
 *
 * The following construct is the actual definition of the entity type which
 * is read and cached. Don't forget to clear cache after changes.
 *
 * @ContentEntityType(
 *   id = "image_slider",
 *   label = @Translation("Slider entity"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\image_slider\Entity\Controller\SliderListBuilder",
 *     "form" = {
 *       "default" = "Drupal\image_slider\Form\SliderForm",
 *       "delete" = "Drupal\image_slider\Form\SliderDeleteForm",
 *     },
 *     "access" = "Drupal\image_slider\SliderAccessControlHandler",
 *   },
 *   list_cache_contexts = { "user" },
 *   base_table = "slider",
 *   admin_permission = "administer slider entity",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "name",
 *     "uuid" = "uuid"
 *   },
 *   links = {
 *     "canonical" = "/image_slider/{image_slider}",
 *     "edit-form" = "/image_slider/{image_slider}/edit",
 *     "delete-form" = "/slider/{image_slider}/delete",
 *     "collection" = "/image_slider/list"
 *   },
 *   field_ui_base_route = "image_slider.slider_settings",
 * )
 *
 * The 'links' above are defined by their path. For core to find the
 * corresponding route, the route name must follow the correct pattern:
 *
 * entity.<entity_type>.<link_name>
 *
 * Example: 'entity.image_slider.canonical'.
 *
 * See the routing file at image_slider.routing.yml for the
 * corresponding implementation.
 */
class Slider extends ContentEntityBase implements SliderInterface {
  use EntityChangedTrait;

  /**
   * {@inheritdoc}
   *
   * When a new entity instance is added, set the user_id entity reference to
   * the current user as the creator of the instance.
   */
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $values);
    $values += [
      'user_id' => \Drupal::currentUser()
        ->id(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOwner() {
    return $this
      ->get('user_id')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getOwnerId() {
    return $this
      ->get('user_id')->target_id;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwnerId($uid) {
    $this
      ->set('user_id', $uid);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwner(UserInterface $account) {
    $this
      ->set('user_id', $account
      ->id());
    return $this;
  }

  /**
   * {@inheritdoc}
   *
   * 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.
   */
  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;
  }

}

Classes

Namesort descending Description
Slider Defines the SliderEntityExample entity.