Moved everything relating to cases into opencase_cases module

This commit is contained in:
2021-02-08 17:27:18 +00:00
parent 2895dcdab8
commit 5afbeb956b
38 changed files with 107 additions and 103 deletions

View File

@ -0,0 +1,15 @@
opencase_entities.oc_case_type.*:
type: config_entity
label: 'Case type config'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
allowedActivityTypes:
type: sequence
label: 'Allowed Activity Types'
uuid:
type: string

View File

@ -0,0 +1,40 @@
<?php
/**
* @file
* Contains oc_case.page.inc.
*
* Page callback for Case entities.
*/
use Drupal\Core\Render\Element;
/**
* Prepares variables for Case templates.
*
* Default template: oc_case.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_case(array &$variables) {
// Separate the fields into two sections to be displayed in two columns.
// Remove the name (title) field as this is displayed anyway.
$variables['id'] = $variables['elements']['#oc_case']->get('id')[0]->get('value')->getValue();
$variables['eva_fields'] = array(); // if the installation has any "EVA" (embedded view) fields this should catch them.
$variables['base_fields'] = array();
$variables['other_fields'] = array();
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
if (in_array($key, ['created', 'changed', 'files', 'actors_involved', 'status', 'user_id'])) {
$variables['base_fields'][$key] = $variables['elements'][$key];
} else if (strpos($key, "entity_view") !== false) {
$variables['eva_fields'][$key] = $variables['elements'][$key];
} else {
$variables['other_fields'][$key] = $variables['elements'][$key];
unset($variables['other_fields']['name']);
}
}
}

View File

@ -0,0 +1,12 @@
entity.oc_case.add_form:
route_name: entity.oc_case.add_page
title: 'Add Case'
appears_on:
- entity.oc_case.collection
entity.oc_case_type.add_form:
route_name: entity.oc_case_type.add_form
title: 'Add Case type'
appears_on:
- entity.oc_case_type.collection

View File

@ -5,3 +5,21 @@ opencase_cases.manage_case_types:
parent: opencase.opencase_admin_menu
url: internal:/admin/opencase/oc_case_type
weight: 2
# Case menu items definition
entity.oc_case.collection:
title: 'Case list'
route_name: entity.oc_case.collection
description: 'List Case entities'
parent: system.admin_structure
weight: 100
# Case type menu items definition
entity.oc_case_type.collection:
title: 'Case type'
route_name: entity.oc_case_type.collection
description: 'List Case type (bundles)'
parent: system.admin_structure
weight: 99

View File

@ -0,0 +1,23 @@
# Case routing definition
entity.oc_case.canonical:
route_name: entity.oc_case.canonical
base_route: entity.oc_case.canonical
title: 'View'
entity.oc_case.edit_form:
route_name: entity.oc_case.edit_form
base_route: entity.oc_case.canonical
title: 'Edit'
entity.oc_case.version_history:
route_name: entity.oc_case.version_history
base_route: entity.oc_case.canonical
title: 'Revisions'
entity.oc_case.delete_form:
route_name: entity.oc_case.delete_form
base_route: entity.oc_case.canonical
title: Delete
weight: 10

View File

@ -31,6 +31,17 @@ function opencase_cases_theme() {
'opencase_cases' => [
'render element' => 'children',
],
'oc_case' => [
'render element' => 'elements',
'file' => 'oc_case.page.inc',
'template' => 'oc_case',
]
'oc_case_content_add_list' => [
'render element' => 'content',
'variables' => ['content' => NULL],
'file' => 'oc_case.page.inc',
]
];
];
}
@ -85,3 +96,45 @@ function _opencase_cases_delete_activity_redirect($form, &$form_state) {
$case_id = $form_state->getFormObject()->getEntity()->oc_case->target_id;
$form_state->setRedirect('entity.oc_case.canonical', ['oc_case' => $case_id]);
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function opencase_cases_theme_suggestions_oc_case(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#oc_case'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'oc_case__' . $sanitized_view_mode;
$suggestions[] = 'oc_case__' . $entity->bundle();
$suggestions[] = 'oc_case__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'oc_case__' . $entity->id();
$suggestions[] = 'oc_case__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
function opencase_views_query_alter(Drupal\views\ViewExecutable $view, $query) {
if ($view->getBaseEntityType() && $view->getBaseEntityType()->id() == 'oc_case') {
$query->addTag('oc_case_access');
}
if ($view->getBaseEntityType() && $view->getBaseEntityType()->id() == 'oc_activity') {
$query->addTag('oc_activity_access');
}
}
function opencase_query_oc_case_access_alter($query) {
if (\Drupal::currentUser()->hasPermission('view published case entities')) {
return;
}
$linked_actor_id = CaseInvolvement::getLinkedActorId(\Drupal::currentUser());
$query->addJoin('INNER', 'oc_case__actors_involved', 'access_filter', 'access_filter.entity_id = oc_case_field_data.id');
$query->condition('access_filter.actors_involved_target_id', $linked_actor_id);
}
function opencase_query_oc_activity_access_alter($query) {
if (\Drupal::currentUser()->hasPermission('view published case entities')) {
return;
}
$linked_actor_id = CaseInvolvement::getLinkedActorId(\Drupal::currentUser());
$query->addJoin('INNER', 'oc_case__actors_involved', 'access_filter', 'access_filter.entity_id = oc_activity_field_data.oc_case');
$query->condition('access_filter.actors_involved_target_id', $linked_actor_id);
}

View File

@ -0,0 +1,22 @@
<?php
namespace Drupal\opencase_entities;
class CaseInvolvement {
public static function getLinkedActorId($account) {
return \Drupal\user\Entity\User::load($account->id())->get('field_linked_opencase_actor')->target_id;
}
public static function userIsInvolved($account, $case) {
$actorId = self::getLinkedActorId($account);
$involvedIds = array_column($case->actors_involved->getValue(), 'target_id');
return in_array($actorId, $involvedIds);
}
public static function userIsInvolved_activity($account, $activity) {
$case_id = $activity->oc_case->target_id;
$case = \Drupal::entityTypeManager()->getStorage('oc_case')->load($case_id);
return self::userIsInvolved($account, $case);
}
}

View File

@ -0,0 +1,163 @@
<?php
namespace Drupal\opencase_entities\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Url;
use Drupal\opencase_entities\Entity\OCCaseInterface;
/**
* Class OCCaseController.
*
* Returns responses for Case routes.
*/
class OCCaseController extends ControllerBase implements ContainerInjectionInterface {
/**
* Displays a Case revision.
*
* @param int $oc_case_revision
* The Case revision ID.
*
* @return array
* An array suitable for drupal_render().
*/
public function revisionShow($oc_case_revision) {
$oc_case = $this->entityManager()->getStorage('oc_case')->loadRevision($oc_case_revision);
$view_builder = $this->entityManager()->getViewBuilder('oc_case');
return $view_builder->view($oc_case);
}
/**
* Page title callback for a Case revision.
*
* @param int $oc_case_revision
* The Case revision ID.
*
* @return string
* The page title.
*/
public function revisionPageTitle($oc_case_revision) {
$oc_case = $this->entityManager()->getStorage('oc_case')->loadRevision($oc_case_revision);
return $this->t('Revision of %title from %date', ['%title' => $oc_case->label(), '%date' => format_date($oc_case->getRevisionCreationTime())]);
}
/**
* Generates an overview table of older revisions of a Case .
*
* @param \Drupal\opencase_entities\Entity\OCCaseInterface $oc_case
* A Case object.
*
* @return array
* An array as expected by drupal_render().
*/
public function revisionOverview(OCCaseInterface $oc_case) {
$account = $this->currentUser();
$langcode = $oc_case->language()->getId();
$langname = $oc_case->language()->getName();
$languages = $oc_case->getTranslationLanguages();
$has_translations = (count($languages) > 1);
$oc_case_storage = $this->entityManager()->getStorage('oc_case');
$build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $oc_case->label()]) : $this->t('Revisions for %title', ['%title' => $oc_case->label()]);
$header = [$this->t('Revision'), $this->t('Operations')];
$revert_permission = (($account->hasPermission("revert all case revisions") || $account->hasPermission('administer case entities')));
$delete_permission = (($account->hasPermission("delete all case revisions") || $account->hasPermission('administer case entities')));
$rows = [];
$vids = $oc_case_storage->revisionIds($oc_case);
$latest_revision = TRUE;
foreach (array_reverse($vids) as $vid) {
/** @var \Drupal\opencase_entities\OCCaseInterface $revision */
$revision = $oc_case_storage->loadRevision($vid);
// Only show revisions that are affected by the language that is being
// displayed.
if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
$username = [
'#theme' => 'username',
'#account' => $revision->getRevisionUser(),
];
// Use revision link to link to revisions that are not active.
$date = \Drupal::service('date.formatter')->format($revision->getRevisionCreationTime(), 'short');
if ($vid != $oc_case->getRevisionId()) {
$link = $this->l($date, new Url('entity.oc_case.revision', ['oc_case' => $oc_case->id(), 'oc_case_revision' => $vid]));
}
else {
$link = $oc_case->link($date);
}
$row = [];
$column = [
'data' => [
'#type' => 'inline_template',
'#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
'#context' => [
'date' => $link,
'username' => \Drupal::service('renderer')->renderPlain($username),
'message' => ['#markup' => $revision->getRevisionLogMessage(), '#allowed_tags' => Xss::getHtmlTagList()],
],
],
];
$row[] = $column;
if ($latest_revision) {
$row[] = [
'data' => [
'#prefix' => '<em>',
'#markup' => $this->t('Current revision'),
'#suffix' => '</em>',
],
];
foreach ($row as &$current) {
$current['class'] = ['revision-current'];
}
$latest_revision = FALSE;
}
else {
$links = [];
if ($revert_permission) {
$links['revert'] = [
'title' => $this->t('Revert'),
'url' => $has_translations ?
Url::fromRoute('entity.oc_case.translation_revert', ['oc_case' => $oc_case->id(), 'oc_case_revision' => $vid, 'langcode' => $langcode]) :
Url::fromRoute('entity.oc_case.revision_revert', ['oc_case' => $oc_case->id(), 'oc_case_revision' => $vid]),
];
}
if ($delete_permission) {
$links['delete'] = [
'title' => $this->t('Delete'),
'url' => Url::fromRoute('entity.oc_case.revision_delete', ['oc_case' => $oc_case->id(), 'oc_case_revision' => $vid]),
];
}
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$rows[] = $row;
}
}
$build['oc_case_revisions_table'] = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
];
return $build;
}
}

View File

@ -0,0 +1,290 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
/**
* Defines the Case entity.
*
* @ingroup opencase_entities
*
* @ContentEntityType(
* id = "oc_case",
* label = @Translation("Case"),
* bundle_label = @Translation("Case type"),
* handlers = {
* "storage" = "Drupal\opencase_entities\OCCaseStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\opencase_entities\OCCaseListBuilder",
* "views_data" = "Drupal\opencase_entities\Entity\OCCaseViewsData",
* "translation" = "Drupal\opencase_entities\OCCaseTranslationHandler",
*
* "form" = {
* "default" = "Drupal\opencase_entities\Form\OCCaseForm",
* "add" = "Drupal\opencase_entities\Form\OCCaseForm",
* "edit" = "Drupal\opencase_entities\Form\OCCaseForm",
* "delete" = "Drupal\opencase_entities\Form\OCCaseDeleteForm",
* },
* "access" = "Drupal\opencase_entities\OCCaseAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\opencase_entities\OCCaseHtmlRouteProvider",
* },
* },
* base_table = "oc_case",
* data_table = "oc_case_field_data",
* revision_table = "oc_case_revision",
* revision_data_table = "oc_case_field_revision",
* translatable = TRUE,
* admin_permission = "administer case entities",
* entity_keys = {
* "id" = "id",
* "revision" = "vid",
* "bundle" = "type",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "status" = "status",
* },
* links = {
* "canonical" = "/opencase/oc_case/{oc_case}",
* "add-page" = "/opencase/oc_case/add",
* "add-form" = "/opencase/oc_case/add/{oc_case_type}",
* "edit-form" = "/opencase/oc_case/{oc_case}/edit",
* "delete-form" = "/opencase/oc_case/{oc_case}/delete",
* "version-history" = "/opencase/oc_case/{oc_case}/revisions",
* "revision" = "/opencase/oc_case/{oc_case}/revisions/{oc_case_revision}/view",
* "revision_revert" = "/opencase/oc_case/{oc_case}/revisions/{oc_case_revision}/revert",
* "revision_delete" = "/opencase/oc_case/{oc_case}/revisions/{oc_case_revision}/delete",
* "translation_revert" = "/opencase/oc_case/{oc_case}/revisions/{oc_case_revision}/revert/{langcode}",
* "collection" = "/opencase/oc_case",
* },
* bundle_entity_type = "oc_case_type",
* field_ui_base_route = "entity.oc_case_type.edit_form"
* )
*/
class OCCase extends RevisionableContentEntityBase implements OCCaseInterface {
use EntityChangedTrait;
/**
* {@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);
foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
$translation = $this->getTranslation($langcode);
// If no owner has been set explicitly, make the anonymous user the owner.
if (!$translation->getOwner()) {
$translation->setOwnerId(0);
}
}
// If no revision author has been set explicitly, make the oc_case 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 function isPublished() {
return (bool) $this->getEntityKey('status');
}
/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->set('status', $published ? TRUE : FALSE);
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// not currently used. Will add form and view settings when ready
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Case is published.'))
->setRevisionable(TRUE)
->setDefaultValue(TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Created by'))
->setDescription(t('The user ID of author of the Case entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 80,
]);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setRevisionable(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -100,
])
->setRequired(TRUE);
$fields['files'] = BaseFieldDefinition::create('file')
->setLabel(t('Files'))
->setDescription(t('Files attached to this case'))
->setSetting('file_directory', '[date:custom:Y]-[date:custom:m]')
->setSetting('handler', 'default:file')
->setSetting('file_extensions', 'txt jpg jpeg gif rtf xls xlsx doc swf png pdf docx csv')
->setSetting('description_field', 'true')
->setSetting('uri_scheme', 'private')
->setCardinality(-1)
->setDisplayOptions('form', [
'type' => 'file_generic',
'weight' => -1,
'settings' => [
'progress_indicator' => 'throbber',
],
])
->setDisplayOptions('view', [
'label' => 'above',
'settings' => ['use_description_as_link_text' => 'true']
]);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created on'))
->setDescription(t('When the case was created.'))
->setDisplayOptions('view', [
'label' => 'above',
'weight' => 80,
]);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Last updated'))
->setDescription(t('When the case was last edited.'))
->setDisplayOptions('view', [
'label' => 'above',
'weight' => 80,
]);
$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;
}
}

View File

@ -0,0 +1,116 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface for defining Case entities.
*
* @ingroup opencase_entities
*/
interface OCCaseInterface extends ContentEntityInterface, RevisionLogInterface, EntityChangedInterface, EntityOwnerInterface {
// Add get/set methods for your configuration properties here.
/**
* Gets the Case name.
*
* @return string
* Name of the Case.
*/
public function getName();
/**
* Sets the Case name.
*
* @param string $name
* The Case name.
*
* @return \Drupal\opencase_entities\Entity\OCCaseInterface
* The called Case entity.
*/
public function setName($name);
/**
* Gets the Case creation timestamp.
*
* @return int
* Creation timestamp of the Case.
*/
public function getCreatedTime();
/**
* Sets the Case creation timestamp.
*
* @param int $timestamp
* The Case creation timestamp.
*
* @return \Drupal\opencase_entities\Entity\OCCaseInterface
* The called Case entity.
*/
public function setCreatedTime($timestamp);
/**
* Returns the Case published status indicator.
*
* Unpublished Case are only visible to restricted users.
*
* @return bool
* TRUE if the Case is published.
*/
public function isPublished();
/**
* Sets the published status of a Case.
*
* @param bool $published
* TRUE to set this Case to published, FALSE to set it to unpublished.
*
* @return \Drupal\opencase_entities\Entity\OCCaseInterface
* The called Case entity.
*/
public function setPublished($published);
/**
* Gets the Case revision creation timestamp.
*
* @return int
* The UNIX timestamp of when this revision was created.
*/
public function getRevisionCreationTime();
/**
* Sets the Case revision creation timestamp.
*
* @param int $timestamp
* The UNIX timestamp of when this revision was created.
*
* @return \Drupal\opencase_entities\Entity\OCCaseInterface
* The called Case entity.
*/
public function setRevisionCreationTime($timestamp);
/**
* Gets the Case revision author.
*
* @return \Drupal\user\UserInterface
* The user entity for the revision author.
*/
public function getRevisionUser();
/**
* Sets the Case revision author.
*
* @param int $uid
* The user ID of the revision author.
*
* @return \Drupal\opencase_entities\Entity\OCCaseInterface
* The called Case entity.
*/
public function setRevisionUserId($uid);
}

View File

@ -0,0 +1,65 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
/**
* Defines the Case type entity.
*
* @ConfigEntityType(
* id = "oc_case_type",
* label = @Translation("Case type"),
* handlers = {
* "access" = "Drupal\opencase_entities\OCCaseTypeAccessControlHandler",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\opencase_entities\OCCaseTypeListBuilder",
* "form" = {
* "add" = "Drupal\opencase_entities\Form\OCCaseTypeForm",
* "edit" = "Drupal\opencase_entities\Form\OCCaseTypeForm",
* "delete" = "Drupal\opencase_entities\Form\OCCaseTypeDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\opencase_entities\OCCaseTypeHtmlRouteProvider",
* },
* },
* config_prefix = "oc_case_type",
* admin_permission = "administer case bundles",
* bundle_of = "oc_case",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "canonical" = "/admin/opencase/oc_case_type/{oc_case_type}",
* "add-form" = "/admin/opencase/oc_case_type/add",
* "edit-form" = "/admin/opencase/oc_case_type/{oc_case_type}/edit",
* "delete-form" = "/admin/opencase/oc_case_type/{oc_case_type}/delete",
* "collection" = "/admin/opencase/oc_case_type"
* }
* )
*/
class OCCaseType extends ConfigEntityBundleBase implements OCCaseTypeInterface {
/**
* The Case type ID.
*
* @var string
*/
protected $id;
/**
* The Case type label.
*
* @var string
*/
protected $label;
/**
* Activity types that can be attached to this type of case.
*
* @var array
*/
protected $allowedActivityTypes;
}

View File

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

View File

@ -0,0 +1,24 @@
<?php
namespace Drupal\opencase_entities\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides Views data for Case entities.
*/
class OCCaseViewsData 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 Case entities.
*
* @ingroup opencase_entities
*/
class OCCaseDeleteForm extends ContentEntityDeleteForm {
}

View File

@ -0,0 +1,71 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Form controller for Case edit forms.
*
* @ingroup opencase_entities
*/
class OCCaseForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var $entity \Drupal\opencase_entities\Entity\OCCase */
$form = parent::buildForm($form, $form_state);
if (!$this->entity->isNew()) {
$form['new_revision'] = [
'#type' => 'checkbox',
'#title' => $this->t('Create new revision'),
'#default_value' => FALSE,
'#weight' => 10,
];
}
$entity = $this->entity;
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
// Save as a new revision if requested to do so.
if (!$form_state->isValueEmpty('new_revision') && $form_state->getValue('new_revision') != FALSE) {
$entity->setNewRevision();
// If a new revision is created, save the current user as revision author.
$entity->setRevisionCreationTime(REQUEST_TIME);
$entity->setRevisionUserId(\Drupal::currentUser()->id());
}
else {
$entity->setNewRevision(FALSE);
}
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this->t('Created the %label Case.', [
'%label' => $entity->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label Case.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.oc_case.canonical', ['oc_case' => $entity->id()]);
}
}

View File

@ -0,0 +1,123 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for deleting a Case revision.
*
* @ingroup opencase_entities
*/
class OCCaseRevisionDeleteForm extends ConfirmFormBase {
/**
* The Case revision.
*
* @var \Drupal\opencase_entities\Entity\OCCaseInterface
*/
protected $revision;
/**
* The Case storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $OCCaseStorage;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Constructs a new OCCaseRevisionDeleteForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The entity storage.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
*/
public function __construct(EntityStorageInterface $entity_storage, Connection $connection) {
$this->OCCaseStorage = $entity_storage;
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_manager = $container->get('entity.manager');
return new static(
$entity_manager->getStorage('oc_case'),
$container->get('database')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'oc_case_revision_delete_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to delete the revision from %revision-date?', ['%revision-date' => format_date($this->revision->getRevisionCreationTime())]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.oc_case.version_history', ['oc_case' => $this->revision->id()]);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Delete');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $oc_case_revision = NULL) {
$this->revision = $this->OCCaseStorage->loadRevision($oc_case_revision);
$form = parent::buildForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->OCCaseStorage->deleteRevision($this->revision->getRevisionId());
$this->logger('content')->notice('Case: deleted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
drupal_set_message(t('Revision from %revision-date of Case %title has been deleted.', ['%revision-date' => format_date($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()]));
$form_state->setRedirect(
'entity.oc_case.canonical',
['oc_case' => $this->revision->id()]
);
if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {oc_case_field_revision} WHERE id = :id', [':id' => $this->revision->id()])->fetchField() > 1) {
$form_state->setRedirect(
'entity.oc_case.version_history',
['oc_case' => $this->revision->id()]
);
}
}
}

View File

@ -0,0 +1,149 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\opencase_entities\Entity\OCCaseInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for reverting a Case revision.
*
* @ingroup opencase_entities
*/
class OCCaseRevisionRevertForm extends ConfirmFormBase {
/**
* The Case revision.
*
* @var \Drupal\opencase_entities\Entity\OCCaseInterface
*/
protected $revision;
/**
* The Case storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $OCCaseStorage;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a new OCCaseRevisionRevertForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The Case storage.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter) {
$this->OCCaseStorage = $entity_storage;
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')->getStorage('oc_case'),
$container->get('date.formatter')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'oc_case_revision_revert_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.oc_case.version_history', ['oc_case' => $this->revision->id()]);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Revert');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return '';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $oc_case_revision = NULL) {
$this->revision = $this->OCCaseStorage->loadRevision($oc_case_revision);
$form = parent::buildForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// The revision timestamp will be updated when the revision is saved. Keep
// the original one for the confirmation message.
$original_revision_timestamp = $this->revision->getRevisionCreationTime();
$this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
$this->revision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
$this->revision->save();
$this->logger('content')->notice('Case: reverted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
drupal_set_message(t('Case %title has been reverted to the revision from %revision-date.', ['%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)]));
$form_state->setRedirect(
'entity.oc_case.version_history',
['oc_case' => $this->revision->id()]
);
}
/**
* Prepares a revision to be reverted.
*
* @param \Drupal\opencase_entities\Entity\OCCaseInterface $revision
* The revision to be reverted.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return \Drupal\opencase_entities\Entity\OCCaseInterface
* The prepared revision ready to be stored.
*/
protected function prepareRevertedRevision(OCCaseInterface $revision, FormStateInterface $form_state) {
$revision->setNewRevision();
$revision->isDefaultRevision(TRUE);
$revision->setRevisionCreationTime(REQUEST_TIME);
return $revision;
}
}

View File

@ -0,0 +1,115 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\opencase_entities\Entity\OCCaseInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for reverting a Case revision for a single translation.
*
* @ingroup opencase_entities
*/
class OCCaseRevisionRevertTranslationForm extends OCCaseRevisionRevertForm {
/**
* The language to be reverted.
*
* @var string
*/
protected $langcode;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a new OCCaseRevisionRevertTranslationForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The Case storage.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter, LanguageManagerInterface $language_manager) {
parent::__construct($entity_storage, $date_formatter);
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')->getStorage('oc_case'),
$container->get('date.formatter'),
$container->get('language_manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'oc_case_revision_revert_translation_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to revert @language translation to the revision from %revision-date?', ['@language' => $this->languageManager->getLanguageName($this->langcode), '%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime())]);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $oc_case_revision = NULL, $langcode = NULL) {
$this->langcode = $langcode;
$form = parent::buildForm($form, $form_state, $oc_case_revision);
$form['revert_untranslated_fields'] = [
'#type' => 'checkbox',
'#title' => $this->t('Revert content shared among translations'),
'#default_value' => FALSE,
];
return $form;
}
/**
* {@inheritdoc}
*/
protected function prepareRevertedRevision(OCCaseInterface $revision, FormStateInterface $form_state) {
$revert_untranslated_fields = $form_state->getValue('revert_untranslated_fields');
/** @var \Drupal\opencase_entities\Entity\OCCaseInterface $default_revision */
$latest_revision = $this->OCCaseStorage->load($revision->id());
$latest_revision_translation = $latest_revision->getTranslation($this->langcode);
$revision_translation = $revision->getTranslation($this->langcode);
foreach ($latest_revision_translation->getFieldDefinitions() as $field_name => $definition) {
if ($definition->isTranslatable() || $revert_untranslated_fields) {
$latest_revision_translation->set($field_name, $revision_translation->get($field_name)->getValue());
}
}
$latest_revision_translation->setNewRevision();
$latest_revision_translation->isDefaultRevision(TRUE);
$revision->setRevisionCreationTime(REQUEST_TIME);
return $latest_revision_translation;
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace Drupal\opencase_entities\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class OCCaseSettingsForm.
*
* @ingroup opencase_entities
*/
class OCCaseSettingsForm extends FormBase {
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'occase_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 Case 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['occase_settings']['#markup'] = 'Settings form for Case entities. Manage field settings here.';
return $form;
}
}

View File

@ -0,0 +1,53 @@
<?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 Case type entities.
*/
class OCCaseTypeDeleteForm 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_case_type.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message(
$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 OCCaseTypeForm.
*/
class OCCaseTypeForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$oc_case_type = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $oc_case_type->label(),
'#description' => $this->t("Label for the Case type."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $oc_case_type->id(),
'#machine_name' => [
'exists' => '\Drupal\opencase_entities\Entity\OCCaseType::load',
],
'#disabled' => !$oc_case_type->isNew(),
];
/* You will need additional form elements for your custom properties. */
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$oc_case_type = $this->entity;
$status = $oc_case_type->save();
switch ($status) {
case SAVED_NEW:
drupal_set_message($this->t('Created the %label Case type.', [
'%label' => $oc_case_type->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label Case type.', [
'%label' => $oc_case_type->label(),
]));
}
$form_state->setRedirectUrl($oc_case_type->toUrl('collection'));
}
}

View File

@ -0,0 +1,55 @@
<?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;
use Drupal\opencase_entities\CaseInvolvement;
/**
* Access controller for the Case entity.
*
* @see \Drupal\opencase_entities\Entity\OCCase.
*/
class OCCaseAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\opencase_entities\Entity\OCCaseInterface $entity */
switch ($operation) {
case 'view':
if (!$entity->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished case entities');
}
return AccessResult::allowedIf(
$account->hasPermission('view published case entities')
|| CaseInvolvement::userIsInvolved($account, $entity)
);
case 'update': // you can edit the case only if a) you can see it and b) you have the permission to edit cases.
return AccessResult::allowedIf(
$account->hasPermission('edit case entities')
&& ($account->hasPermission('view published case entities') || CaseInvolvement::userIsInvolved($account, $entity))
);
case 'delete': // you can delete the case only if a) you can see it and b) you have the permission to delete cases.
return AccessResult::allowedIf(
$account->hasPermission('delete case entities')
&& ($account->hasPermission('view published case entities') || CaseInvolvement::userIsInvolved($account, $entity))
);
}
// Unknown operation, no opinion.
return AccessResult::neutral();
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add case entities');
}
}

View File

@ -0,0 +1,196 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides routes for Case entities.
*
* @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class OCCaseHtmlRouteProvider extends AdminHtmlRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
$entity_type_id = $entity_type->id();
if ($history_route = $this->getHistoryRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.version_history", $history_route);
}
if ($revision_route = $this->getRevisionRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.revision", $revision_route);
}
if ($revert_route = $this->getRevisionRevertRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.revision_revert", $revert_route);
}
if ($delete_route = $this->getRevisionDeleteRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.revision_delete", $delete_route);
}
if ($translation_route = $this->getRevisionTranslationRevertRoute($entity_type)) {
$collection->add("{$entity_type_id}.revision_revert_translation_confirm", $translation_route);
}
if ($settings_form_route = $this->getSettingsFormRoute($entity_type)) {
$collection->add("$entity_type_id.settings", $settings_form_route);
}
return $collection;
}
/**
* Gets the version history route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getHistoryRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('version-history')) {
$route = new Route($entity_type->getLinkTemplate('version-history'));
$route
->setDefaults([
'_title' => "{$entity_type->getLabel()} revisions",
'_controller' => '\Drupal\opencase_entities\Controller\OCCaseController::revisionOverview',
])
->setRequirement('_permission', 'access case revisions')
->setOption('_admin_route', TRUE);
return $route;
}
}
/**
* Gets the revision route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getRevisionRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('revision')) {
$route = new Route($entity_type->getLinkTemplate('revision'));
$route
->setDefaults([
'_controller' => '\Drupal\opencase_entities\Controller\OCCaseController::revisionShow',
'_title_callback' => '\Drupal\opencase_entities\Controller\OCCaseController::revisionPageTitle',
])
->setRequirement('_permission', 'access case revisions')
->setOption('_admin_route', TRUE);
return $route;
}
}
/**
* Gets the revision revert route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getRevisionRevertRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('revision_revert')) {
$route = new Route($entity_type->getLinkTemplate('revision_revert'));
$route
->setDefaults([
'_form' => '\Drupal\opencase_entities\Form\OCCaseRevisionRevertForm',
'_title' => 'Revert to earlier revision',
])
->setRequirement('_permission', 'revert all case revisions')
->setOption('_admin_route', TRUE);
return $route;
}
}
/**
* Gets the revision delete route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getRevisionDeleteRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('revision_delete')) {
$route = new Route($entity_type->getLinkTemplate('revision_delete'));
$route
->setDefaults([
'_form' => '\Drupal\opencase_entities\Form\OCCaseRevisionDeleteForm',
'_title' => 'Delete earlier revision',
])
->setRequirement('_permission', 'delete all case revisions')
->setOption('_admin_route', TRUE);
return $route;
}
}
/**
* Gets the revision translation revert route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getRevisionTranslationRevertRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('translation_revert')) {
$route = new Route($entity_type->getLinkTemplate('translation_revert'));
$route
->setDefaults([
'_form' => '\Drupal\opencase_entities\Form\OCCaseRevisionRevertTranslationForm',
'_title' => 'Revert to earlier revision of a translation',
])
->setRequirement('_permission', 'revert all case revisions')
->setOption('_admin_route', TRUE);
return $route;
}
}
/**
* 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\OCCaseSettingsForm',
'_title' => "{$entity_type->getLabel()} settings",
])
->setRequirement('_permission', $entity_type->getAdminPermission())
->setOption('_admin_route', TRUE);
return $route;
}
}
}

View File

@ -0,0 +1,40 @@
<?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 Case entities.
*
* @ingroup opencase_entities
*/
class OCCaseListBuilder extends EntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['id'] = $this->t('Case ID');
$header['name'] = $this->t('Name');
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
/* @var $entity \Drupal\opencase_entities\Entity\OCCase */
$row['id'] = $entity->id();
$row['name'] = Link::createFromRoute(
$entity->label(),
'entity.oc_case.edit_form',
['oc_case' => $entity->id()]
);
return $row + parent::buildRow($entity);
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\opencase_entities\Entity\OCCaseInterface;
/**
* Defines the storage handler class for Case entities.
*
* This extends the base storage class, adding required special handling for
* Case entities.
*
* @ingroup opencase_entities
*/
class OCCaseStorage extends SqlContentEntityStorage implements OCCaseStorageInterface {
/**
* {@inheritdoc}
*/
public function revisionIds(OCCaseInterface $entity) {
return $this->database->query(
'SELECT vid FROM {oc_case_revision} WHERE id=:id ORDER BY vid',
[':id' => $entity->id()]
)->fetchCol();
}
/**
* {@inheritdoc}
*/
public function userRevisionIds(AccountInterface $account) {
return $this->database->query(
'SELECT vid FROM {oc_case_field_revision} WHERE uid = :uid ORDER BY vid',
[':uid' => $account->id()]
)->fetchCol();
}
/**
* {@inheritdoc}
*/
public function countDefaultLanguageRevisions(OCCaseInterface $entity) {
return $this->database->query('SELECT COUNT(*) FROM {oc_case_field_revision} WHERE id = :id AND default_langcode = 1', [':id' => $entity->id()])
->fetchField();
}
/**
* {@inheritdoc}
*/
public function clearRevisionsLanguage(LanguageInterface $language) {
return $this->database->update('oc_case_revision')
->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED])
->condition('langcode', $language->getId())
->execute();
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\opencase_entities\Entity\OCCaseInterface;
/**
* Defines the storage handler class for Case entities.
*
* This extends the base storage class, adding required special handling for
* Case entities.
*
* @ingroup opencase_entities
*/
interface OCCaseStorageInterface extends ContentEntityStorageInterface {
/**
* Gets a list of Case revision IDs for a specific Case.
*
* @param \Drupal\opencase_entities\Entity\OCCaseInterface $entity
* The Case entity.
*
* @return int[]
* Case revision IDs (in ascending order).
*/
public function revisionIds(OCCaseInterface $entity);
/**
* Gets a list of revision IDs having a given user as Case author.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The user entity.
*
* @return int[]
* Case revision IDs (in ascending order).
*/
public function userRevisionIds(AccountInterface $account);
/**
* Counts the number of revisions in the default language.
*
* @param \Drupal\opencase_entities\Entity\OCCaseInterface $entity
* The Case entity.
*
* @return int
* The number of revisions in the default language.
*/
public function countDefaultLanguageRevisions(OCCaseInterface $entity);
/**
* Unsets the language for all Case with the given language.
*
* @param \Drupal\Core\Language\LanguageInterface $language
* The language object.
*/
public function clearRevisionsLanguage(LanguageInterface $language);
}

View File

@ -0,0 +1,14 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\content_translation\ContentTranslationHandler;
/**
* Defines the translation handler for oc_case.
*/
class OCCaseTranslationHandler extends ContentTranslationHandler {
// Override here the needed methods from ContentTranslationHandler.
}

View File

@ -0,0 +1,26 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Access\AccessResult;
/**
* Defines the access control handler for the OCCaseType Config Entity.
* Always allows viewing the label of the bundle.
*
* @see Drupal\opencase_entities\Entity\OCCaseType
*/
class OCCaseTypeAccessControlHandler extends EntityAccessControlHandler {
protected $viewLabelOperation = TRUE;
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation == 'view label') {
return AccessResult::allowed();
}
return parent::checkAccess($entity, $operation, $account);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides routes for Case type entities.
*
* @see Drupal\Core\Entity\Routing\AdminHtmlRouteProvider
* @see Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider
*/
class OCCaseTypeHtmlRouteProvider 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 Case type entities.
*/
class OCCaseTypeListBuilder extends ConfigEntityListBuilder {
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header['label'] = $this->t('Case 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,93 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OpenCaseEntityPermissions implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructor for MyModulePermissions.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'));
}
/**
* Get permissions for MyModule.
*
* @return array
* Permissions array.
*/
public function permissions() {
$permissions = [];
foreach ($this->entityTypeManager->getStorage('oc_actor_type')->loadMultiple() as $id => $type) {
$permissions += [
"add $id entities" => [
'title' => $this->t('Create new %type entities', array('%type' => $type->label())),
]
];
$permissions += [
"edit $id entities" => [
'title' => $this->t('Edit %type entities', array('%type' => $type->label())),
]
];
$permissions += [
"delete $id entities" => [
'title' => $this->t('Delete %type entities', array('%type' => $type->label())),
]
];
$permissions += [
"view published $id entities" => [
'title' => $this->t('View published %type entities', array('%type' => $type->label())),
]
];
$permissions += [
"view all $id revisions" => [
'title' => $this->t('View %type revisions', array('%type' => $type->label())),
]
];
$permissions += [
"revert all $id revisions" => [
'title' => $this->t('Revert %type revisions', array('%type' => $type->label())),
]
];
$permissions += [
"delete all $id revisions" => [
'title' => $this->t('Delete %type revisions', array('%type' => $type->label())),
]
];
}
return $permissions;
}
}

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_case_content_add_list()
*
* @ingroup themeable
*/
#}
{% spaceless %}
<dl>
{% for type in types %}
<dt>{{ type.link }}</dt>
{% endfor %}
</dl>
{% endspaceless %}

View File

@ -0,0 +1,38 @@
{#
/**
* @file oc_case.html.twig
* Default theme implementation to present Case data.
*
* This template is used when viewing Case 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_case()
*
* @ingroup themeable
*/
#}
<div{{ attributes.addClass('oc_case') }}>
<h2>{{ title }}</h2>
<div class="oc_entity">
<div class="left">
{{ base_fields.actors_involved }}
{{ base_fields.changed }}
{{ base_fields.created }}
{{ base_fields.user_id }}
</div>
<div class="right">
{% for field in other_fields %}
{{ field }}
{% endfor %}
</div>
<div class="eva_fields">
{% for field in eva_fields %}
{{ field }}
{% endfor %}
</div>
</div>
</div>