podcast_publisher-1.0.0-alpha3/src/Entity/Podcast.php
src/Entity/Podcast.php
<?php
namespace Drupal\podcast_publisher\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\podcast_publisher\PodcastInterface;
use Drupal\user\UserInterface;
/**
* Defines the podcast entity class.
*
* @ContentEntityType(
* id = "podcast",
* label = @Translation("Podcast"),
* label_collection = @Translation("Podcasts"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\podcast_publisher\PodcastListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "access" = "Drupal\podcast_publisher\PodcastAccessControlHandler",
* "form" = {
* "add" = "Drupal\podcast_publisher\Form\PodcastForm",
* "edit" = "Drupal\podcast_publisher\Form\PodcastForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* }
* },
* base_table = "podcast",
* admin_permission = "administer podcast",
* entity_keys = {
* "id" = "id",
* "label" = "title",
* "uuid" = "uuid"
* },
* links = {
* "add-form" = "/admin/content/podcast/add",
* "canonical" = "/podcast/{podcast}",
* "edit-form" = "/admin/content/podcast/{podcast}/edit",
* "delete-form" = "/admin/content/podcast/{podcast}/delete",
* "collection" = "/admin/content/podcast"
* },
* field_ui_base_route = "entity.podcast.settings"
* )
*/
class Podcast extends ContentEntityBase implements PodcastInterface {
use EntityChangedTrait;
/**
* {@inheritdoc}
*
* When a new podcast entity is created, set the uid entity reference to
* the current user as the creator of the entity.
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += ['uid' => \Drupal::currentUser()->id()];
}
/**
* {@inheritdoc}
*/
public function getTitle() {
return $this->get('title')->value;
}
/**
* {@inheritdoc}
*/
public function setTitle($title) {
$this->set('title', $title);
return $this;
}
/**
* {@inheritdoc}
*/
public function isEnabled() {
return (bool) $this->get('status')->value;
}
/**
* {@inheritdoc}
*/
public function setStatus($status) {
$this->set('status', $status);
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('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 static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The title of the podcast entity.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 1,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Status'))
->setDescription(t('A boolean indicating whether the podcast is enabled.'))
->setDefaultValue(TRUE)
->setSetting('on_label', 'Enabled')
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => FALSE,
],
'weight' => 15,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'type' => 'boolean',
'label' => 'above',
'weight' => 0,
'settings' => [
'format' => 'enabled-disabled',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setDescription(t('The user ID of the podcast author.'))
->setSetting('target_type', 'user')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 13,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 15,
])
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the podcast was created.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 20,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 20,
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the podcast was last edited.'));
$fields['category'] = BaseFieldDefinition::create('string')
->setLabel(t('Category'))
->setDescription(t("The podcast's categories."))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSettings([
'max_length' => 255,
'case_sensitive' => FALSE,
'is_ascii' => FALSE,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 11,
'settings' => [
'size' => 60,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['complete'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Complete'))
->setDescription(t('Whether the podcast is complete.'))
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'weight' => 9,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['copyright'] = BaseFieldDefinition::create('string')
->setLabel(t('Copyright'))
->setCardinality(1)
->setSettings([
'max_length' => 255,
'case_sensitive' => FALSE,
'is_ascii' => FALSE,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 8,
'settings' => [
'size' => 60,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['email'] = BaseFieldDefinition::create('email')
->setLabel(t('Email'))
->setDescription(t('Email of podcast owner.'))
->setCardinality(1)
->setSettings([
'max_length' => 255,
'case_sensitive' => FALSE,
'is_ascii' => FALSE,
])
->setDisplayOptions('form', [
'type' => 'email_default',
'weight' => 4,
'settings' => [
'size' => 60,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['explicit'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Explicit'))
->setDescription(t('Whether the podcast is explicit.'))
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['funding'] = BaseFieldDefinition::create('link')
->setLabel(t('Funding'))
->setDescription(t('Link to funding website.'))
->setSettings([
'link_type' => 16,
'title' => 1,
])
->setDisplayOptions('form', [
'type' => 'link_default',
'weight' => 6,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['image'] = BaseFieldDefinition::create('image')
->setLabel(t('Image'))
->setDescription(t("The podcast's image."))
->setSetting('alt_field', TRUE)
->setSetting('alt_field_required', TRUE)
->setSetting('title_field', TRUE)
->setSetting('title_field_required', FALSE)
->setDisplayOptions('form', [
'type' => 'image_image',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['itunes_category'] = BaseFieldDefinition::create('entity_reference')
->setLabel('Itunes Category')
->setDescription(t('Category in itunes are predefined.'))
->setCardinality(3)
->setSettings([
'target_type' => 'taxonomy_term',
'handler' => 'default:taxonomy_term',
'handler_settings' => [
'target_bundles' => [
'pp_itunes_categories' => 'pp_itunes_categories',
],
'sort' => [
'field' => 'name',
'direction' => 'asc',
],
'auto_create' => 'false',
],
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 12,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['subtitle'] = BaseFieldDefinition::create('string')
->setLabel(t('Subtitle'))
->setCardinality(1)
->setSettings([
'max_length' => 255,
'case_sensitive' => FALSE,
'is_ascii' => FALSE,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 2,
'settings' => [
'size' => 60,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['summary'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Summary'))
->setCardinality(1)
->setSettings([
'max_length' => 255,
'case_sensitive' => FALSE,
'is_ascii' => FALSE,
])
->setDisplayOptions('form', [
'type' => 'string_textarea',
'weight' => 3,
'settings' => [
'rows' => 5,
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['type'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Type'))
->setDescription(t('Type of podcast.'))
->setCardinality(1)
->setRequired(TRUE)
->setSetting('allowed_values', [
'episodical' => 'Episodical',
'serial' => 'Serial',
])
->setDisplayOptions('form', [
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['authors'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setDescription(t('The user ID of the podcast author.'))
->setSetting('target_type', 'user')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 15,
])
->setDisplayConfigurable('view', TRUE);
return $fields;
}
}
