You are here

Booking.php in Booking and Availability Management Tools for Drupal 8

File

modules/bat_booking/src/Entity/Booking.php
View source
<?php

/**
 * @file
 * Contains \Drupal\bat_booking\Entity\Booking.
 */
namespace Drupal\bat_booking\Entity;

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
use Drupal\bat_booking\BookingInterface;
use Drupal\user\EntityOwnerTrait;

/**
 * Defines the Booking entity.
 *
 * @ingroup bat
 *
 * @ContentEntityType(
 *   id = "bat_booking",
 *   label = @Translation("Booking"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\bat_booking\BookingListBuilder",
 *     "views_data" = "Drupal\bat_booking\Entity\BookingViewsData",
 *     "form" = {
 *       "default" = "Drupal\bat_booking\Entity\Form\BookingForm",
 *       "add" = "Drupal\bat_booking\Entity\Form\BookingForm",
 *       "edit" = "Drupal\bat_booking\Entity\Form\BookingForm",
 *       "delete" = "Drupal\bat_booking\Entity\Form\BookingDeleteForm",
 *     },
 *     "access" = "Drupal\bat_booking\BookingAccessControlHandler",
 *   },
 *   base_table = "booking",
 *   admin_permission = "administer booking entity",
 *   entity_keys = {
 *     "id" = "id",
 *     "bundle" = "type",
 *     "label" = "label",
 *     "uuid" = "uuid",
 *     "uid" = "uid",
 *     "owner" = "uid",
 *   },
 *   bundle_entity_type = "bat_booking_bundle",
 *   field_ui_base_route = "entity.bat_booking_bundle.edit_form",
 *   permission_granularity = "bundle",
 *   links = {
 *     "canonical" = "/admin/booking/{bat_booking}",
 *     "edit-form" = "/admin/booking/{bat_booking}/edit",
 *     "delete-form" = "/admin/booking/{bat_booking}/delete"
 *   }
 * )
 */
class Booking extends ContentEntityBase implements BookingInterface {
  use EntityChangedTrait, EntityOwnerTrait;

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

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

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

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

  /**
   * {@inheritdoc}
   */
  public function getStatus() {
    return $this
      ->get('status')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setStatus($status) {
    $this
      ->set('status', $status);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields += static::ownerBaseFieldDefinitions($entity_type);
    $fields['id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the Booking entity.'))
      ->setReadOnly(TRUE);
    $fields['uuid'] = BaseFieldDefinition::create('uuid')
      ->setLabel(t('UUID'))
      ->setDescription(t('The UUID of the Booking entity.'))
      ->setReadOnly(TRUE);
    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Authored by'))
      ->setDescription(t('The user ID of author of the Booking entity.'))
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->setDefaultValueCallback(static::class . '::getCurrentUserId')
      ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'author',
      'weight' => 0,
    ])
      ->setDisplayOptions('form', [
      'type' => 'entity_reference_autocomplete',
      'weight' => 5,
      'settings' => [
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'autocomplete_type' => 'tags',
        'placeholder' => '',
      ],
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
    $fields['label'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Label'))
      ->setDescription(t('The label of the Booking entity.'))
      ->setSettings([
      'max_length' => 50,
      'text_processing' => 0,
    ])
      ->setDefaultValue('');
    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Authored on'))
      ->setDescription(t('The time that the entity was created.'))
      ->setDisplayOptions('form', [
      'type' => 'datetime_timestamp',
      'weight' => 10,
    ])
      ->setDisplayConfigurable('form', TRUE);
    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the entity was last edited.'));
    $fields['type'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Type'))
      ->setDescription(t('The booking bundle.'))
      ->setSetting('target_type', 'bat_booking_bundle');
    $fields['status'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Published'))
      ->setDefaultValue(TRUE);
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function save() {
    if ($this
      ->isNew()) {
      parent::save();
    }

    // Set default value for label.
    if (empty($this
      ->label())) {
      $booking_type = bat_booking_type_load($this
        ->bundle());
      $this
        ->set('label', $booking_type
        ->label() . ' ' . $this
        ->id());
    }
    parent::save();
  }

  /**
   * Default value callback for 'uid' base field definition.
   *
   * @see ::baseFieldDefinitions()
   *
   * @return array
   *   An array of default values.
   */
  public static function getCurrentUserId() {
    return [
      \Drupal::currentUser()
        ->id(),
    ];
  }

}

Classes

Namesort descending Description
Booking Defines the Booking entity.