Added Event entity type.

This commit is contained in:
2021-01-28 10:22:20 +00:00
parent 53648faff7
commit b48ba012cb
36 changed files with 1353 additions and 0 deletions

View File

@ -0,0 +1,159 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\Core\Datetime\DrupalDateTime;
/**
* Defines the Event entity.
*
* @ingroup opencase_entities
*
* @ContentEntityType(
* id = "oc_event",
* label = @Translation("Event"),
* bundle_label = @Translation("Event type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\opencase_entities\OCEventListBuilder",
* "views_data" = "Drupal\opencase_entities\Entity\OCEventViewsData",
*
* "form" = {
* "default" = "Drupal\opencase_entities\Form\OCEventForm",
* "add" = "Drupal\opencase_entities\Form\OCEventForm",
* "edit" = "Drupal\opencase_entities\Form\OCEventForm",
* "delete" = "Drupal\opencase_entities\Form\OCEventDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\opencase_entities\OCEventHtmlRouteProvider",
* },
* "access" = "Drupal\opencase_entities\OCEventAccessControlHandler",
* },
* base_table = "oc_event",
* translatable = FALSE,
* permission_granularity = "bundle",
* admin_permission = "administer event entities",
* entity_keys = {
* "id" = "id",
* "bundle" = "type",
* "label" = "name",
* "uuid" = "uuid",
* "langcode" = "langcode",
* "published" = "status",
* },
* links = {
* "canonical" = "/opencase/oc_event/{oc_event}",
* "add-page" = "/opencase/oc_event/add",
* "add-form" = "/opencase/oc_event/add/{oc_event_type}",
* "edit-form" = "/opencase/oc_event/{oc_event}/edit",
* "delete-form" = "/opencase/oc_event/{oc_event}/delete",
* "collection" = "/opencase/oc_event",
* },
* bundle_entity_type = "oc_event_type",
* field_ui_base_route = "entity.oc_event_type.edit_form"
* )
*/
class OCEvent extends ContentEntityBase implements OCEventInterface {
use EntityChangedTrait;
use EntityPublishedTrait;
/**
* {@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 static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// Add the published field.
$fields += static::publishedBaseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Event entity.'))
->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 Event is published.'));
$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['activity_date_time'] = BaseFieldDefinition::create('datetime')
->setLabel(t('Date and time'))
->setRevisionable(TRUE)
->setRequired(TRUE)
// Uses the currentDateTime function from the Activity entity
->setDefaultValueCallback('\Drupal\opencase_entities\Entity\OCActivity::currentDateTime')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'datetime_default',
'settings' => [
'format_type' => 'medium',
],
'weight' => -3,
])
->setDisplayOptions('form', [
'type' => 'datetime_default',
'weight' => -3,
]);
return $fields;
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
/**
* Provides an interface for defining Event entities.
*
* @ingroup opencase_entities
*/
interface OCEventInterface extends ContentEntityInterface, EntityChangedInterface, EntityPublishedInterface {
/**
* Add get/set methods for your configuration properties here.
*/
/**
* Gets the Event name.
*
* @return string
* Name of the Event.
*/
public function getName();
/**
* Sets the Event name.
*
* @param string $name
* The Event name.
*
* @return \Drupal\opencase_entities\Entity\OCEventInterface
* The called Event entity.
*/
public function setName($name);
/**
* Gets the Event creation timestamp.
*
* @return int
* Creation timestamp of the Event.
*/
public function getCreatedTime();
/**
* Sets the Event creation timestamp.
*
* @param int $timestamp
* The Event creation timestamp.
*
* @return \Drupal\opencase_entities\Entity\OCEventInterface
* The called Event entity.
*/
public function setCreatedTime($timestamp);
}

View File

@ -0,0 +1,58 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
/**
* Defines the Event type entity.
*
* @ConfigEntityType(
* id = "oc_event_type",
* label = @Translation("Event type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\opencase_entities\OCEventTypeListBuilder",
* "form" = {
* "add" = "Drupal\opencase_entities\Form\OCEventTypeForm",
* "edit" = "Drupal\opencase_entities\Form\OCEventTypeForm",
* "delete" = "Drupal\opencase_entities\Form\OCEventTypeDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\opencase_entities\OCEventTypeHtmlRouteProvider",
* },
* },
* config_prefix = "oc_event_type",
* admin_permission = "administer site configuration",
* bundle_of = "oc_event",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "canonical" = "/admin/opencase/oc_event_type/{oc_event_type}",
* "add-form" = "/admin/opencase/oc_event_type/add",
* "edit-form" = "/admin/opencase/oc_event_type/{oc_event_type}/edit",
* "delete-form" = "/admin/opencase/oc_event_type/{oc_event_type}/delete",
* "collection" = "/admin/opencase/oc_event_type"
* }
* )
*/
class OCEventType extends ConfigEntityBundleBase implements OCEventTypeInterface {
/**
* The Event type ID.
*
* @var string
*/
protected $id;
/**
* The Event type label.
*
* @var string
*/
protected $label;
}

View File

@ -0,0 +1,13 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface for defining Event type entities.
*/
interface OCEventTypeInterface extends ConfigEntityInterface {
// Add get/set methods for your configuration properties here.
}

View File

@ -0,0 +1,23 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides Views data for Event entities.
*/
class OCEventViewsData extends EntityViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
// Additional information for Views integration, such as table joins, can be
// put here.
return $data;
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
/**
* Provides a form for deleting Event entities.
*
* @ingroup opencase_entities
*/
class OCEventDeleteForm extends ContentEntityDeleteForm {
}

View File

@ -0,0 +1,66 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for Event edit forms.
*
* @ingroup opencase_entities
*/
class OCEventForm extends ContentEntityForm {
/**
* The current user account.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $account;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
$instance = parent::create($container);
$instance->account = $container->get('current_user');
return $instance;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var \Drupal\opencase_entities\Entity\OCEvent $entity */
$form = parent::buildForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
$this->messenger()->addMessage($this->t('Created the %label Event.', [
'%label' => $entity->label(),
]));
break;
default:
$this->messenger()->addMessage($this->t('Saved the %label Event.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.oc_event.canonical', ['oc_event' => $entity->id()]);
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class OCEventSettingsForm.
*
* @ingroup opencase_entities
*/
class OCEventSettingsForm extends FormBase {
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'ocevent_settings';
}
/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Empty implementation of the abstract submit class.
}
/**
* Defines the settings form for Event entities.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* Form definition array.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['ocevent_settings']['#markup'] = 'Settings form for Event entities. Manage field settings here.';
return $form;
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Builds the form to delete Event type entities.
*/
class OCEventTypeDeleteForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.oc_event_type.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$this->messenger()->addMessage(
$this->t('content @type: deleted @label.', [
'@type' => $this->entity->bundle(),
'@label' => $this->entity->label(),
])
);
$form_state->setRedirectUrl($this->getCancelUrl());
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Class OCEventTypeForm.
*/
class OCEventTypeForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$oc_event_type = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $oc_event_type->label(),
'#description' => $this->t("Label for the Event type."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $oc_event_type->id(),
'#machine_name' => [
'exists' => '\Drupal\opencase_entities\Entity\OCEventType::load',
],
'#disabled' => !$oc_event_type->isNew(),
];
/* You will need additional form elements for your custom properties. */
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$oc_event_type = $this->entity;
$status = $oc_event_type->save();
switch ($status) {
case SAVED_NEW:
$this->messenger()->addMessage($this->t('Created the %label Event type.', [
'%label' => $oc_event_type->label(),
]));
break;
default:
$this->messenger()->addMessage($this->t('Saved the %label Event type.', [
'%label' => $oc_event_type->label(),
]));
}
$form_state->setRedirectUrl($oc_event_type->toUrl('collection'));
}
}

View File

View File

@ -0,0 +1,116 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
/**
* Access controller for the Event entity.
*
* @see \Drupal\opencase_entities\Entity\OCEvent.
*/
class OCEventAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\opencase_entities\Entity\OCEventInterface $entity */
switch ($operation) {
case 'view':
if (!$entity->isPublished()) {
$permission = $this->checkOwn($entity, 'view unpublished', $account);
if (!empty($permission)) {
return AccessResult::allowed();
}
return AccessResult::allowedIfHasPermission($account, 'view unpublished event entities');
}
$permission = $this->checkOwn($entity, $operation, $account);
if (!empty($permission)) {
return AccessResult::allowed();
}
return AccessResult::allowedIfHasPermission($account, 'view published event entities');
case 'update':
$permission = $this->checkOwn($entity, $operation, $account);
if (!empty($permission)) {
return AccessResult::allowed();
}
return AccessResult::allowedIfHasPermission($account, 'edit event entities');
case 'delete':
$permission = $this->checkOwn($entity, $operation, $account);
if (!empty($permission)) {
return AccessResult::allowed();
}
return AccessResult::allowedIfHasPermission($account, 'delete event entities');
}
// Unknown operation, no opinion.
return AccessResult::neutral();
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add event entities');
}
/**
* Test for given 'own' permission.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param $operation
* @param \Drupal\Core\Session\AccountInterface $account
*
* @return string|null
* The permission string indicating it's allowed.
*/
protected function checkOwn(EntityInterface $entity, $operation, AccountInterface $account) {
$status = $entity->isPublished();
$uid = $entity->getOwnerId();
$is_own = $account->isAuthenticated() && $account->id() == $uid;
if (!$is_own) {
return;
}
$bundle = $entity->bundle();
$ops = [
'create' => '%bundle add own %bundle entities',
'view unpublished' => '%bundle view own unpublished %bundle entities',
'view' => '%bundle view own entities',
'update' => '%bundle edit own entities',
'delete' => '%bundle delete own entities',
];
$permission = strtr($ops[$operation], ['%bundle' => $bundle]);
if ($operation === 'view unpublished') {
if (!$status && $account->hasPermission($permission)) {
return $permission;
}
else {
return NULL;
}
}
if ($account->hasPermission($permission)) {
return $permission;
}
return NULL;
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides routes for Event entities.
*
* @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class OCEventHtmlRouteProvider extends AdminHtmlRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
$entity_type_id = $entity_type->id();
if ($settings_form_route = $this->getSettingsFormRoute($entity_type)) {
$collection->add("$entity_type_id.settings", $settings_form_route);
}
return $collection;
}
/**
* Gets the settings form route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getSettingsFormRoute(EntityTypeInterface $entity_type) {
if (!$entity_type->getBundleEntityType()) {
$route = new Route("/admin/structure/{$entity_type->id()}/settings");
$route
->setDefaults([
'_form' => 'Drupal\opencase_entities\Form\OCEventSettingsForm',
'_title' => "{$entity_type->getLabel()} settings",
])
->setRequirement('_permission', $entity_type->getAdminPermission())
->setOption('_admin_route', TRUE);
return $route;
}
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Link;
/**
* Defines a class to build a listing of Event entities.
*
* @ingroup opencase_entities
*/
class OCEventListBuilder extends EntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('Event ID');
$header['name'] = $this->t('Name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var \Drupal\opencase_entities\Entity\OCEvent $entity */
$row['id'] = $entity->id();
$row['name'] = Link::createFromRoute(
$entity->label(),
'entity.oc_event.edit_form',
['oc_event' => $entity->id()]
);
return $row + parent::buildRow($entity);
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\opencase_entities\Entity\OCEvent;
/**
* Provides dynamic permissions for Event of different types.
*
* @ingroup opencase_entities
*
*/
class OCEventPermissions{
use StringTranslationTrait;
/**
* Returns an array of node type permissions.
*
* @return array
* The OCEvent by bundle permissions.
* @see \Drupal\user\PermissionHandlerInterface::getPermissions()
*/
public function generatePermissions() {
$perms = [];
foreach (OCEvent::loadMultiple() as $type) {
$perms += $this->buildPermissions($type);
}
return $perms;
}
/**
* Returns a list of node permissions for a given node type.
*
* @param \Drupal\opencase_entities\Entity\OCEvent $type
* The OCEvent type.
*
* @return array
* An associative array of permission names and descriptions.
*/
protected function buildPermissions(OCEvent $type) {
$type_id = $type->id();
$type_params = ['%type_name' => $type->label()];
return [
"$type_id create entities" => [
'title' => $this->t('Create new %type_name entities', $type_params),
],
"$type_id edit own entities" => [
'title' => $this->t('Edit own %type_name entities', $type_params),
],
"$type_id edit any entities" => [
'title' => $this->t('Edit any %type_name entities', $type_params),
],
"$type_id delete own entities" => [
'title' => $this->t('Delete own %type_name entities', $type_params),
],
"$type_id delete any entities" => [
'title' => $this->t('Delete any %type_name entities', $type_params),
],
];
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
/**
* Provides routes for Event type entities.
*
* @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class OCEventTypeHtmlRouteProvider extends AdminHtmlRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
// Provide your custom entity routes here.
return $collection;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of Event type entities.
*/
class OCEventTypeListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('Event type');
$header['id'] = $this->t('Machine name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row['label'] = $entity->label();
$row['id'] = $entity->id();
// You probably want a few more properties here...
return $row + parent::buildRow($entity);
}
}