Added Event entity type.

This commit is contained in:
naomi 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,12 @@
opencase.oc_event.*:
type: config_entity
label: 'Event config'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
uuid:
type: string

View File

@ -0,0 +1,12 @@
opencase_entities.oc_event.*:
type: config_entity
label: 'Event config'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
uuid:
type: string

View File

@ -0,0 +1,12 @@
opencase_entities.oc_event_type.*:
type: config_entity
label: 'Event type config'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
uuid:
type: string

View File

@ -0,0 +1,30 @@
<?php
/**
* @file
* Contains oc_event.page.inc.
*
* Page callback for Event entities.
*/
use Drupal\Core\Render\Element;
/**
* Prepares variables for Event templates.
*
* Default template: oc_event.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_oc_event(array &$variables) {
// Fetch OCEvent Entity Object.
$oc_event = $variables['elements']['#oc_event'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}

View File

@ -41,3 +41,13 @@ entity.oc_organisation_type.add_form:
title: 'Add Organisation type'
appears_on:
- entity.oc_organisation_type.collection
entity.oc_event.add_form:
route_name: entity.oc_event.add_page
title: 'Add Event'
appears_on:
- entity.oc_event.collection
entity.oc_event_type.add_form:
route_name: entity.oc_event_type.add_form
title: 'Add Event type'
appears_on:
- entity.oc_event_type.collection

View File

@ -69,3 +69,20 @@ entity.oc_organisation_type.collection:
description: 'List Organisation type (bundles)'
parent: system.admin_structure
weight: 99
# Event menu items definition
entity.oc_event.collection:
title: 'Event list'
route_name: entity.oc_event.collection
description: 'List Event entities'
parent: system.admin_structure
weight: 100
# Event type menu items definition
entity.oc_event_type.collection:
title: 'Event type'
route_name: entity.oc_event_type.collection
description: 'List Event type (bundles)'
parent: system.admin_structure
weight: 99

View File

@ -89,3 +89,20 @@ entity.oc_organisation.delete_form:
base_route: entity.oc_organisation.canonical
title: Delete
weight: 10
# Event routing definition
entity.oc_event.canonical:
route_name: entity.oc_event.canonical
base_route: entity.oc_event.canonical
title: 'View'
entity.oc_event.edit_form:
route_name: entity.oc_event.edit_form
base_route: entity.oc_event.canonical
title: 'Edit'
entity.oc_event.delete_form:
route_name: entity.oc_event.delete_form
base_route: entity.oc_event.canonical
title: Delete
weight: 10

View File

@ -72,6 +72,16 @@ function opencase_entities_theme() {
'variables' => ['content' => NULL],
'file' => 'oc_organisation.page.inc',
];
$theme['oc_event'] = [
'render element' => 'elements',
'file' => 'oc_event.page.inc',
'template' => 'oc_event',
];
$theme['oc_event_content_add_list'] = [
'render element' => 'content',
'variables' => ['content' => NULL],
'file' => 'oc_event.page.inc',
];
return $theme;
}
@ -165,3 +175,19 @@ function opencase_entities_theme_suggestions_oc_organisation(array $variables) {
$suggestions[] = 'oc_organisation__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function opencase_entities_theme_suggestions_oc_event(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#oc_event'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'oc_event__' . $sanitized_view_mode;
$suggestions[] = 'oc_event__' . $entity->bundle();
$suggestions[] = 'oc_event__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'oc_event__' . $entity->id();
$suggestions[] = 'oc_event__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}

View File

@ -105,3 +105,25 @@ delete all organisation revisions:
permission_callbacks:
- \Drupal\opencase_entities\OCOrganisationPermissions::generatePermissions
add event entities:
title: 'Create new Event entities'
administer event entities:
title: 'Administer Event entities'
description: 'Allow to access the administration form to configure Event entities.'
restrict access: true
delete event entities:
title: 'Delete Event entities'
edit event entities:
title: 'Edit Event entities'
view published event entities:
title: 'View published Event entities'
view unpublished event entities:
title: 'View unpublished Event entities'
permission_callbacks:
- \Drupal\opencase_entities\OCEventPermissions::generatePermissions

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);
}
}

View File

@ -0,0 +1,23 @@
{#
/**
* @file
* Default theme implementation to present a list of custom content entity types/bundles.
*
* Available variables:
* - types: A collection of all the available custom entity types/bundles.
* Each type/bundle contains the following:
* - link: A link to add a content entity of this type.
* - description: A description of this content entity types/bundle.
*
* @see template_preprocess_oc_event_content_add_list()
*
* @ingroup themeable
*/
#}
{% spaceless %}
<dl>
{% for type in types %}
<dt>{{ type.link }}</dt>
{% endfor %}
</dl>
{% endspaceless %}

View File

@ -0,0 +1,22 @@
{#
/**
* @file oc_event.html.twig
* Default theme implementation to present Event data.
*
* This template is used when viewing Event pages.
*
*
* Available variables:
* - content: A list of content items. Use 'content' to print all content, or
* - attributes: HTML attributes for the container element.
*
* @see template_preprocess_oc_event()
*
* @ingroup themeable
*/
#}
<div{{ attributes.addClass('oc_event') }}>
{% if content %}
{{- content -}}
{% endif %}
</div>

View File

@ -0,0 +1,5 @@
entity.oc_event.add_form:
route_name: entity.oc_event.add_form
title: 'Add Event'
appears_on:
- entity.oc_event.collection

View File

@ -18,6 +18,11 @@ opencase.add_organisations_links:
deriver: Drupal\opencase\Plugin\Derivative\AddOrganisationsMenuLink
menu_name: opencase
parent: opencase.opencase_add_new_things_menu
opencase.add_events_links:
class: Drupal\opencase\Plugin\Menu\AddEventsMenuLink
deriver: Drupal\opencase\Plugin\Derivative\AddEventsMenuLink
menu_name: opencase
parent: opencase.opencase_add_new_things_menu
opencase.opencase_admin_menu:
title: 'Administration'
description: 'Management and Configuration'
@ -59,6 +64,13 @@ opencase.manage_activity_types:
parent: opencase.opencase_admin_menu
url: internal:/admin/opencase/oc_activity_type
weight: 3
opencase.manage_event_types:
title: 'Manage event types'
description: 'Configure types of people and their fields'
menu_name: opencase
parent: opencase.opencase_admin_menu
url: internal:/admin/opencase/oc_event_type
weight: 1
opencase.opencase_support_menu:
title: 'Support'

View File

@ -0,0 +1,51 @@
<?php
namespace Drupal\opencase\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Builds the form to delete Event entities.
*/
class OCEventDeleteForm 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.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());
}
}

65
src/Form/OCEventForm.php Normal file
View File

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

View File

@ -0,0 +1,26 @@
<?php
namespace Drupal\opencase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
/**
* 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);
// Provide your custom entity routes here.
return $collection;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace Drupal\opencase;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
/**
* Provides a listing of Event entities.
*/
class OCEventListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('Event');
$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);
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace Drupal\opencase\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
 * Derivative class that provides the menu links adding various types of events
 */
class AddEventsMenuLink extends DeriverBase implements ContainerDeriverInterface {
/**
   * @var EntityTypeManagerInterface $entityTypeManager.
   */
protected $entityTypeManager;
/**
   * Creates a AddEventsMenuLink instance.
   *
   * @param $base_plugin_id
   * @param EntityTypeManagerInterface $entity_type_manager
   */
public function __construct($base_plugin_id, EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
   * {@inheritdoc}
   */
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$base_plugin_id,
$container->get('entity_type.manager')
);
}
/**
   * {@inheritdoc}
   */
public function getDerivativeDefinitions($base_plugin_definition) {
$links = [];
$eventTypes = $this->entityTypeManager->getStorage('oc_event_type')->loadMultiple();
foreach ($eventTypes as $id => $eventType) {
$links[$id] = [
'title' => "Add ". $eventType->label() . " Event",
'route_name' => "entity.oc_event.add_form",
'route_parameters' => ['oc_event_type' => $eventType->id()]
] + $base_plugin_definition;
}
return $links;
}
}

View File

@ -0,0 +1,6 @@
<?php
namespace Drupal\opencase\Plugin\Menu;
use Drupal\Core\Menu\MenuLinkDefault;
class AddEventsMenuLink extends MenuLinkDefault {}