You are here

Invoice.php in Drupal PM (Project Management) 4.x

File

modules/pm_invoice/src/Entity/Invoice.php
View source
<?php

namespace Drupal\pm_invoice\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\pm_project\Entity\ProjectTrait;
use Drupal\user\UserInterface;

/**
 * Defines the Invoice entity.
 *
 * @ingroup pm_invoice
 *
 * @ContentEntityType(
 *   id = "pm_invoice",
 *   label = @Translation("Invoice"),
 *   bundle_label = @Translation("Invoice type"),
 *   handlers = {
 *     "storage" = "Drupal\pm_invoice\InvoiceStorage",
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\pm_invoice\InvoiceListBuilder",
 *     "views_data" = "Drupal\pm_invoice\Entity\InvoiceViewsData",
 *
 *     "form" = {
 *       "default" = "Drupal\pm_invoice\Form\InvoiceForm",
 *       "add" = "Drupal\pm_invoice\Form\InvoiceForm",
 *       "edit" = "Drupal\pm_invoice\Form\InvoiceForm",
 *       "delete" = "Drupal\pm_invoice\Form\InvoiceDeleteForm",
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\pm_invoice\InvoiceHtmlRouteProvider",
 *     },
 *     "access" = "Drupal\pm_invoice\InvoiceAccessControlHandler",
 *   },
 *   base_table = "pm_invoice",
 *   revision_table = "pm_invoice_revision",
 *   revision_data_table = "pm_invoice_field_revision",
 *   translatable = FALSE,
 *   permission_granularity = "bundle",
 *   admin_permission = "administer invoice entities",
 *   entity_keys = {
 *     "id" = "id",
 *     "revision" = "vid",
 *     "bundle" = "type",
 *     "label" = "name",
 *     "uuid" = "uuid",
 *     "uid" = "user_id",
 *     "langcode" = "langcode",
 *     "published" = "status",
 *     "pm_project" = "pm_project",
 *   },
 *   links = {
 *     "canonical" = "/pm/invoice/{pm_invoice}",
 *     "add-page" = "/pm/invoice/add",
 *     "add-form" = "/pm/invoice/add/{pm_invoice_type}",
 *     "edit-form" = "/pm/invoice/{pm_invoice}/edit",
 *     "delete-form" = "/pm/invoice/{pm_invoice}/delete",
 *     "version-history" = "/pm/invoice/{pm_invoice}/revisions",
 *     "revision" = "/pm/invoice/{pm_invoice}/revisions/{pm_invoice_revision}/view",
 *     "revision_revert" = "/pm/invoice/{pm_invoice}/revisions/{pm_invoice_revision}/revert",
 *     "revision_delete" = "/pm/invoice/{pm_invoice}/revisions/{pm_invoice_revision}/delete",
 *     "collection" = "/pm/invoice",
 *   },
 *   bundle_entity_type = "pm_invoice_type",
 *   field_ui_base_route = "entity.pm_invoice_type.edit_form"
 * )
 */
class Invoice extends EditorialContentEntityBase implements InvoiceInterface {
  use EntityChangedTrait;
  use EntityPublishedTrait;
  use ProjectTrait;

  /**
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $values);
    $values += [
      'user_id' => \Drupal::currentUser()
        ->id(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function urlRouteParameters($rel) {
    $uri_route_parameters = parent::urlRouteParameters($rel);
    if ($rel === 'revision_revert' && $this instanceof RevisionableInterface) {
      $uri_route_parameters[$this
        ->getEntityTypeId() . '_revision'] = $this
        ->getRevisionId();
    }
    elseif ($rel === 'revision_delete' && $this instanceof RevisionableInterface) {
      $uri_route_parameters[$this
        ->getEntityTypeId() . '_revision'] = $this
        ->getRevisionId();
    }
    return $uri_route_parameters;
  }

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    // If no revision author has been set explicitly,
    // make the pm_invoice owner the revision author.
    if (!$this
      ->getRevisionUser()) {
      $this
        ->setRevisionUserId($this
        ->getOwnerId());
    }
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $this
      ->set('created', $timestamp);
    return $this;
  }

  /**
   * {@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}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    // Add project field.
    $fields += static::projectFieldDefinitions($entity_type);

    // Add the published field.
    $fields += static::publishedBaseFieldDefinitions($entity_type);
    $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Authored by'))
      ->setDescription(t('The user ID of author of the Invoice entity.'))
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->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['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('The name of the Invoice entity.'))
      ->setRevisionable(TRUE)
      ->setSettings([
      'max_length' => 50,
      'text_processing' => 0,
    ])
      ->setDefaultValue('')
      ->setDisplayOptions('view', [
      'label' => 'above',
      'type' => 'string',
      'weight' => -4,
    ])
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -4,
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setRequired(TRUE);
    $fields['status']
      ->setDescription(t('A boolean indicating whether the Invoice is published.'))
      ->setDisplayOptions('form', [
      'type' => 'boolean_checkbox',
      'weight' => -3,
    ]);
    $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
Invoice Defines the Invoice entity.