Drupal modules for browsing and managing Fedora-based digital repositories.
 
 
 
 

460 lines
14 KiB

<?php
namespace Drupal\islandora\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\islandora\FedoraResourceInterface;
use Drupal\user\UserInterface;
/**
* Defines the Fedora resource entity.
*
* @ingroup islandora
*
* @ContentEntityType(
* id = "fedora_resource",
* label = @Translation("Fedora resource"),
* bundle_label = @Translation("Fedora resource type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\islandora\FedoraResourceListBuilder",
* "views_data" = "Drupal\islandora\Entity\FedoraResourceViewsData",
* "inline_form" = "Drupal\islandora\Form\FedoraResourceEntityInlineForm",
* "form" = {
* "default" = "Drupal\islandora\Form\FedoraResourceForm",
* "add" = "Drupal\islandora\Form\FedoraResourceForm",
* "edit" = "Drupal\islandora\Form\FedoraResourceForm",
* "delete" = "Drupal\islandora\Form\FedoraResourceDeleteForm",
* },
* "access" = "Drupal\islandora\FedoraResourceAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\islandora\FedoraResourceHtmlRouteProvider",
* },
* },
* base_table = "fedora_resource",
* data_table = "fedora_resource_field_data",
* revision_table = "fedora_resource_revision",
* revision_data_table = "fedora_resource_field_data_revision",
* translatable = TRUE,
* admin_permission = "administer fedora resource entities",
* entity_keys = {
* "id" = "id",
* "revision" = "vid",
* "bundle" = "type",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* },
* common_reference_target = TRUE,
* permission_granularity = "bundle",
* links = {
* "canonical" = "/fedora_resource/{fedora_resource}",
* "uuid" = "/islandora_uuid/{fedora_resource}",
* "add-form" = "/fedora_resource/add/{fedora_resource_type}",
* "edit-form" = "/fedora_resource/{fedora_resource}/edit",
* "delete-form" = "/fedora_resource/{fedora_resource}/delete",
* "collection" = "/admin/content/fedora_resource",
* },
* bundle_entity_type = "fedora_resource_type",
* field_ui_base_route = "entity.fedora_resource_type.edit_form"
* )
*/
class FedoraResource extends ContentEntityBase implements FedoraResourceInterface{
use EntityChangedTrait;
/**
* @param string $rel
* @param array $options
* @return $this|\Drupal\Core\Url
*/
public function toUrl($rel = 'canonical', array $options = []) {
// TODO: I Will override this to have a rel ='uuid' for islandora entities
return parent::toUrl($rel, $options); // TODO: Change the autogenerated stub
}
/**
* Gets an array of placeholders for Fedora Resource Entity.
*
* We are overriding this method to add additional uuid
* placeholders.
*
* @param string $rel
* The link relationship type, for example: canonical or edit-form.
*
* @return array
* An array of URI placeholders.
*/
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel); // TODO: Change the autogenerated stub
if ($rel === 'uuid') {
$uri_route_parameters[$this->getEntityTypeId()] = $this->uuid();
}
return $uri_route_parameters;
}
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += array(
'user_id' => \Drupal::currentUser()->id(),
);
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
// Reindex the entity when it is updated. The entity is automatically
// indexed when it is added, simply by being added to the {fedora_resource} table.
// Only required if using the core search index.
if ($update) {
if (\Drupal::moduleHandler()->moduleExists('search')) {
search_mark_for_reindex('fedora_resource_search', $this->id());
}
}
}
/**
* {@inheritdoc}
*/
public function getType() {
return $this->bundle();
}
/**
* {@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 function isPublished() {
return (bool) $this->getEntityKey('status');
}
/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED);
return $this;
}
/**
* {@inheritdoc}
*/
public function hasParent()
{
return ($this->get('fedora_has_parent') !== null);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return $this->get('fedora_has_parent')->getEntity();
}
/**
* {@inheritdoc}
*/
public function getParentId()
{
return $this->get('fedora_has_parent')->getEntity()->id();
}
/**
* {@inheritdoc}
*/
public function setParent(EntityTypeInterface $entity)
{
$this->set('fedora_has_parent', $entity);
return $this;
}
/**
* Default value callback for 'fedora_has_parent' base field definition.
*
* @see ::baseFieldDefinitions()
*
* @return array
* An array of default values.
*/
public static function getFedoraRoot() {
// Just stub code, we need to figure out what "root is" in this context
return array('root');
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Fedora resource entity.'))
->setReadOnly(TRUE);
$fields['vid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The Fedora resource revision ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The Fedora resource type/bundle.'))
->setSetting('target_type', 'fedora_resource_type')
->setRequired(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Fedora resource entity.'))
->setReadOnly(TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Fedora resource entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
))
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => array(
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
),
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['fedora_has_parent'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Fedora has Parent'))
->setDescription(t('Parent Fedora Resource.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'fedora_resource')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\islandora\Entity\FedoraResource::getFedoraRoot')
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
))
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => array(
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
),
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['ldp_contains'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('LDP Contains'))
->setDescription(t('Contains Fedora Resource.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'fedora_resource_type')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
))
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => array(
'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 Fedora resource entity.'))
->setSettings(array(
'max_length' => 50,
'text_processing' => 0,
))
->setDefaultValue('')
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -4,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -4,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Fedora resource is published.'))
->setDefaultValue(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code for the Fedora resource entity.'))
->setDisplayOptions('form', array(
'type' => 'language_select',
'weight' => 10,
))
->setDisplayConfigurable('form', TRUE);
$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.'));
$fields['promote'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Promoted to front page'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)
->setDisplayOptions('form', array(
'type' => 'boolean_checkbox',
'settings' => array(
'display_label' => TRUE,
),
'weight' => 15,
))
->setDisplayConfigurable('form', TRUE);
$fields['sticky'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Sticky at top of lists'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(FALSE)
->setDisplayOptions('form', array(
'type' => 'boolean_checkbox',
'settings' => array(
'display_label' => TRUE,
),
'weight' => 16,
))
->setDisplayConfigurable('form', TRUE);
$fields['revision_timestamp'] = BaseFieldDefinition::create('created')
->setLabel(t('Revision timestamp'))
->setDescription(t('The time that the current revision was created.'))
->setQueryable(FALSE)
->setRevisionable(TRUE);
$fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Revision user ID'))
->setDescription(t('The user ID of the author of the current revision.'))
->setSetting('target_type', 'user')
->setQueryable(FALSE)
->setRevisionable(TRUE);
$fields['revision_log'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Revision log message'))
->setDescription(t('Briefly describe the changes you have made.'))
->setRevisionable(TRUE)
->setDefaultValue('')
->setDisplayOptions('form', array(
'type' => 'string_textarea',
'weight' => 25,
'settings' => array(
'rows' => 4,
),
));
$fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Revision translation affected'))
->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}
}