From 249003bf1601115711e4aecf10e6fd21264c4f9f Mon Sep 17 00:00:00 2001 From: naomi Date: Sun, 29 Apr 2018 13:58:46 +0200 Subject: [PATCH] Made Actor, Activity and Case entities --- .../config/schema/oc_activity_type.schema.yml | 12 + .../config/schema/oc_actor_type.schema.yml | 12 + .../config/schema/oc_case_type.schema.yml | 12 + .../opencase_entities/oc_activity.page.inc | 30 ++ modules/opencase_entities/oc_actor.page.inc | 30 ++ modules/opencase_entities/oc_case.page.inc | 30 ++ .../opencase_entities.links.action.yml | 33 ++ .../opencase_entities.links.menu.yml | 54 ++++ .../opencase_entities.links.task.yml | 69 +++++ .../opencase_entities.module | 86 +++++- .../opencase_entities.permissions.yml | 90 ++++++ .../src/Controller/OCActivityController.php | 163 ++++++++++ .../src/Controller/OCActorController.php | 163 ++++++++++ .../src/Controller/OCCaseController.php | 163 ++++++++++ .../src/Entity/OCActivity.php | 281 ++++++++++++++++++ .../src/Entity/OCActivityInterface.php | 116 ++++++++ .../src/Entity/OCActivityType.php | 58 ++++ .../src/Entity/OCActivityTypeInterface.php | 13 + .../src/Entity/OCActivityViewsData.php | 24 ++ .../opencase_entities/src/Entity/OCActor.php | 281 ++++++++++++++++++ .../src/Entity/OCActorInterface.php | 116 ++++++++ .../src/Entity/OCActorType.php | 58 ++++ .../src/Entity/OCActorTypeInterface.php | 13 + .../src/Entity/OCActorViewsData.php | 24 ++ .../opencase_entities/src/Entity/OCCase.php | 281 ++++++++++++++++++ .../src/Entity/OCCaseInterface.php | 116 ++++++++ .../src/Entity/OCCaseType.php | 58 ++++ .../src/Entity/OCCaseTypeInterface.php | 13 + .../src/Entity/OCCaseViewsData.php | 24 ++ .../src/Form/OCActivityDeleteForm.php | 15 + .../src/Form/OCActivityForm.php | 71 +++++ .../src/Form/OCActivityRevisionDeleteForm.php | 123 ++++++++ .../src/Form/OCActivityRevisionRevertForm.php | 149 ++++++++++ ...CActivityRevisionRevertTranslationForm.php | 115 +++++++ .../src/Form/OCActivitySettingsForm.php | 53 ++++ .../src/Form/OCActivityTypeDeleteForm.php | 53 ++++ .../src/Form/OCActivityTypeForm.php | 65 ++++ .../src/Form/OCActorDeleteForm.php | 15 + .../src/Form/OCActorForm.php | 71 +++++ .../src/Form/OCActorRevisionDeleteForm.php | 123 ++++++++ .../src/Form/OCActorRevisionRevertForm.php | 149 ++++++++++ .../OCActorRevisionRevertTranslationForm.php | 115 +++++++ .../src/Form/OCActorSettingsForm.php | 53 ++++ .../src/Form/OCActorTypeDeleteForm.php | 53 ++++ .../src/Form/OCActorTypeForm.php | 65 ++++ .../src/Form/OCCaseDeleteForm.php | 15 + .../opencase_entities/src/Form/OCCaseForm.php | 71 +++++ .../src/Form/OCCaseRevisionDeleteForm.php | 123 ++++++++ .../src/Form/OCCaseRevisionRevertForm.php | 149 ++++++++++ .../OCCaseRevisionRevertTranslationForm.php | 115 +++++++ .../src/Form/OCCaseSettingsForm.php | 53 ++++ .../src/Form/OCCaseTypeDeleteForm.php | 53 ++++ .../src/Form/OCCaseTypeForm.php | 65 ++++ .../src/OCActivityAccessControlHandler.php | 47 +++ .../src/OCActivityHtmlRouteProvider.php | 196 ++++++++++++ .../src/OCActivityListBuilder.php | 40 +++ .../src/OCActivityStorage.php | 58 ++++ .../src/OCActivityStorageInterface.php | 61 ++++ .../src/OCActivityTranslationHandler.php | 14 + .../src/OCActivityTypeHtmlRouteProvider.php | 28 ++ .../src/OCActivityTypeListBuilder.php | 32 ++ .../src/OCActorAccessControlHandler.php | 47 +++ .../src/OCActorHtmlRouteProvider.php | 196 ++++++++++++ .../src/OCActorListBuilder.php | 40 +++ .../opencase_entities/src/OCActorStorage.php | 58 ++++ .../src/OCActorStorageInterface.php | 61 ++++ .../src/OCActorTranslationHandler.php | 14 + .../src/OCActorTypeHtmlRouteProvider.php | 28 ++ .../src/OCActorTypeListBuilder.php | 32 ++ .../src/OCCaseAccessControlHandler.php | 47 +++ .../src/OCCaseHtmlRouteProvider.php | 196 ++++++++++++ .../src/OCCaseListBuilder.php | 40 +++ .../opencase_entities/src/OCCaseStorage.php | 58 ++++ .../src/OCCaseStorageInterface.php | 61 ++++ .../src/OCCaseTranslationHandler.php | 14 + .../src/OCCaseTypeHtmlRouteProvider.php | 28 ++ .../src/OCCaseTypeListBuilder.php | 32 ++ .../oc-activity-content-add-list.html.twig | 23 ++ .../oc-actor-content-add-list.html.twig | 23 ++ .../oc-case-content-add-list.html.twig | 23 ++ .../templates/oc_activity.html.twig | 22 ++ .../templates/oc_actor.html.twig | 22 ++ .../templates/oc_case.html.twig | 22 ++ 83 files changed, 5914 insertions(+), 4 deletions(-) create mode 100644 modules/opencase_entities/config/schema/oc_activity_type.schema.yml create mode 100644 modules/opencase_entities/config/schema/oc_actor_type.schema.yml create mode 100644 modules/opencase_entities/config/schema/oc_case_type.schema.yml create mode 100644 modules/opencase_entities/oc_activity.page.inc create mode 100644 modules/opencase_entities/oc_actor.page.inc create mode 100644 modules/opencase_entities/oc_case.page.inc create mode 100644 modules/opencase_entities/opencase_entities.links.action.yml create mode 100644 modules/opencase_entities/opencase_entities.links.menu.yml create mode 100644 modules/opencase_entities/opencase_entities.links.task.yml create mode 100644 modules/opencase_entities/opencase_entities.permissions.yml create mode 100644 modules/opencase_entities/src/Controller/OCActivityController.php create mode 100644 modules/opencase_entities/src/Controller/OCActorController.php create mode 100644 modules/opencase_entities/src/Controller/OCCaseController.php create mode 100644 modules/opencase_entities/src/Entity/OCActivity.php create mode 100644 modules/opencase_entities/src/Entity/OCActivityInterface.php create mode 100644 modules/opencase_entities/src/Entity/OCActivityType.php create mode 100644 modules/opencase_entities/src/Entity/OCActivityTypeInterface.php create mode 100644 modules/opencase_entities/src/Entity/OCActivityViewsData.php create mode 100644 modules/opencase_entities/src/Entity/OCActor.php create mode 100644 modules/opencase_entities/src/Entity/OCActorInterface.php create mode 100644 modules/opencase_entities/src/Entity/OCActorType.php create mode 100644 modules/opencase_entities/src/Entity/OCActorTypeInterface.php create mode 100644 modules/opencase_entities/src/Entity/OCActorViewsData.php create mode 100644 modules/opencase_entities/src/Entity/OCCase.php create mode 100644 modules/opencase_entities/src/Entity/OCCaseInterface.php create mode 100644 modules/opencase_entities/src/Entity/OCCaseType.php create mode 100644 modules/opencase_entities/src/Entity/OCCaseTypeInterface.php create mode 100644 modules/opencase_entities/src/Entity/OCCaseViewsData.php create mode 100644 modules/opencase_entities/src/Form/OCActivityDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCActivityForm.php create mode 100644 modules/opencase_entities/src/Form/OCActivityRevisionDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCActivityRevisionRevertForm.php create mode 100644 modules/opencase_entities/src/Form/OCActivityRevisionRevertTranslationForm.php create mode 100644 modules/opencase_entities/src/Form/OCActivitySettingsForm.php create mode 100644 modules/opencase_entities/src/Form/OCActivityTypeDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCActivityTypeForm.php create mode 100644 modules/opencase_entities/src/Form/OCActorDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCActorForm.php create mode 100644 modules/opencase_entities/src/Form/OCActorRevisionDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCActorRevisionRevertForm.php create mode 100644 modules/opencase_entities/src/Form/OCActorRevisionRevertTranslationForm.php create mode 100644 modules/opencase_entities/src/Form/OCActorSettingsForm.php create mode 100644 modules/opencase_entities/src/Form/OCActorTypeDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCActorTypeForm.php create mode 100644 modules/opencase_entities/src/Form/OCCaseDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCCaseForm.php create mode 100644 modules/opencase_entities/src/Form/OCCaseRevisionDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCCaseRevisionRevertForm.php create mode 100644 modules/opencase_entities/src/Form/OCCaseRevisionRevertTranslationForm.php create mode 100644 modules/opencase_entities/src/Form/OCCaseSettingsForm.php create mode 100644 modules/opencase_entities/src/Form/OCCaseTypeDeleteForm.php create mode 100644 modules/opencase_entities/src/Form/OCCaseTypeForm.php create mode 100644 modules/opencase_entities/src/OCActivityAccessControlHandler.php create mode 100644 modules/opencase_entities/src/OCActivityHtmlRouteProvider.php create mode 100644 modules/opencase_entities/src/OCActivityListBuilder.php create mode 100644 modules/opencase_entities/src/OCActivityStorage.php create mode 100644 modules/opencase_entities/src/OCActivityStorageInterface.php create mode 100644 modules/opencase_entities/src/OCActivityTranslationHandler.php create mode 100644 modules/opencase_entities/src/OCActivityTypeHtmlRouteProvider.php create mode 100644 modules/opencase_entities/src/OCActivityTypeListBuilder.php create mode 100644 modules/opencase_entities/src/OCActorAccessControlHandler.php create mode 100644 modules/opencase_entities/src/OCActorHtmlRouteProvider.php create mode 100644 modules/opencase_entities/src/OCActorListBuilder.php create mode 100644 modules/opencase_entities/src/OCActorStorage.php create mode 100644 modules/opencase_entities/src/OCActorStorageInterface.php create mode 100644 modules/opencase_entities/src/OCActorTranslationHandler.php create mode 100644 modules/opencase_entities/src/OCActorTypeHtmlRouteProvider.php create mode 100644 modules/opencase_entities/src/OCActorTypeListBuilder.php create mode 100644 modules/opencase_entities/src/OCCaseAccessControlHandler.php create mode 100644 modules/opencase_entities/src/OCCaseHtmlRouteProvider.php create mode 100644 modules/opencase_entities/src/OCCaseListBuilder.php create mode 100644 modules/opencase_entities/src/OCCaseStorage.php create mode 100644 modules/opencase_entities/src/OCCaseStorageInterface.php create mode 100644 modules/opencase_entities/src/OCCaseTranslationHandler.php create mode 100644 modules/opencase_entities/src/OCCaseTypeHtmlRouteProvider.php create mode 100644 modules/opencase_entities/src/OCCaseTypeListBuilder.php create mode 100644 modules/opencase_entities/templates/oc-activity-content-add-list.html.twig create mode 100644 modules/opencase_entities/templates/oc-actor-content-add-list.html.twig create mode 100644 modules/opencase_entities/templates/oc-case-content-add-list.html.twig create mode 100644 modules/opencase_entities/templates/oc_activity.html.twig create mode 100644 modules/opencase_entities/templates/oc_actor.html.twig create mode 100644 modules/opencase_entities/templates/oc_case.html.twig diff --git a/modules/opencase_entities/config/schema/oc_activity_type.schema.yml b/modules/opencase_entities/config/schema/oc_activity_type.schema.yml new file mode 100644 index 0000000..99acc5a --- /dev/null +++ b/modules/opencase_entities/config/schema/oc_activity_type.schema.yml @@ -0,0 +1,12 @@ +opencase_entities.oc_activity_type.*: + type: config_entity + label: 'Activity type config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/modules/opencase_entities/config/schema/oc_actor_type.schema.yml b/modules/opencase_entities/config/schema/oc_actor_type.schema.yml new file mode 100644 index 0000000..87b7207 --- /dev/null +++ b/modules/opencase_entities/config/schema/oc_actor_type.schema.yml @@ -0,0 +1,12 @@ +opencase_entities.oc_actor_type.*: + type: config_entity + label: 'Actor type config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/modules/opencase_entities/config/schema/oc_case_type.schema.yml b/modules/opencase_entities/config/schema/oc_case_type.schema.yml new file mode 100644 index 0000000..7a22f60 --- /dev/null +++ b/modules/opencase_entities/config/schema/oc_case_type.schema.yml @@ -0,0 +1,12 @@ +opencase_entities.oc_case_type.*: + type: config_entity + label: 'Case type config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/modules/opencase_entities/oc_activity.page.inc b/modules/opencase_entities/oc_activity.page.inc new file mode 100644 index 0000000..fbd7d00 --- /dev/null +++ b/modules/opencase_entities/oc_activity.page.inc @@ -0,0 +1,30 @@ + [ - 'render element' => 'children', - ], + $theme = []; + $theme['opencase_entities'] = [ + 'render element' => 'children', ]; + $theme['oc_actor'] = [ + 'render element' => 'elements', + 'file' => 'oc_actor.page.inc', + 'template' => 'oc_actor', + ]; + $theme['oc_actor_content_add_list'] = [ + 'render element' => 'content', + 'variables' => ['content' => NULL], + 'file' => 'oc_actor.page.inc', + ]; + $theme['oc_case'] = [ + 'render element' => 'elements', + 'file' => 'oc_case.page.inc', + 'template' => 'oc_case', + ]; + $theme['oc_case_content_add_list'] = [ + 'render element' => 'content', + 'variables' => ['content' => NULL], + 'file' => 'oc_case.page.inc', + ]; + $theme['oc_activity'] = [ + 'render element' => 'elements', + 'file' => 'oc_activity.page.inc', + 'template' => 'oc_activity', + ]; + $theme['oc_activity_content_add_list'] = [ + 'render element' => 'content', + 'variables' => ['content' => NULL], + 'file' => 'oc_activity.page.inc', + ]; + return $theme; +} + +/** +* Implements hook_theme_suggestions_HOOK(). +*/ +function opencase_entities_theme_suggestions_oc_actor(array $variables) { + $suggestions = []; + $entity = $variables['elements']['#oc_actor']; + $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); + + $suggestions[] = 'oc_actor__' . $sanitized_view_mode; + $suggestions[] = 'oc_actor__' . $entity->bundle(); + $suggestions[] = 'oc_actor__' . $entity->bundle() . '__' . $sanitized_view_mode; + $suggestions[] = 'oc_actor__' . $entity->id(); + $suggestions[] = 'oc_actor__' . $entity->id() . '__' . $sanitized_view_mode; + return $suggestions; +} + +/** +* Implements hook_theme_suggestions_HOOK(). +*/ +function opencase_entities_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; +} + +/** +* Implements hook_theme_suggestions_HOOK(). +*/ +function opencase_entities_theme_suggestions_oc_activity(array $variables) { + $suggestions = []; + $entity = $variables['elements']['#oc_activity']; + $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); + + $suggestions[] = 'oc_activity__' . $sanitized_view_mode; + $suggestions[] = 'oc_activity__' . $entity->bundle(); + $suggestions[] = 'oc_activity__' . $entity->bundle() . '__' . $sanitized_view_mode; + $suggestions[] = 'oc_activity__' . $entity->id(); + $suggestions[] = 'oc_activity__' . $entity->id() . '__' . $sanitized_view_mode; + return $suggestions; } diff --git a/modules/opencase_entities/opencase_entities.permissions.yml b/modules/opencase_entities/opencase_entities.permissions.yml new file mode 100644 index 0000000..564d348 --- /dev/null +++ b/modules/opencase_entities/opencase_entities.permissions.yml @@ -0,0 +1,90 @@ +add actor entities: + title: 'Create new Actor entities' + +administer actor entities: + title: 'Administer Actor entities' + description: 'Allow to access the administration form to configure Actor entities.' + restrict access: true + +delete actor entities: + title: 'Delete Actor entities' + +edit actor entities: + title: 'Edit Actor entities' + +view published actor entities: + title: 'View published Actor entities' + +view unpublished actor entities: + title: 'View unpublished Actor entities' + +view all actor revisions: + title: 'View all Actor revisions' + +revert all actor revisions: + title: 'Revert all Actor revisions' + description: 'Role requires permission view Actor revisions and edit rights for actor entities in question or administer actor entities.' + +delete all actor revisions: + title: 'Delete all revisions' + description: 'Role requires permission to view Actor revisions and delete rights for actor entities in question or administer actor entities.' +add case entities: + title: 'Create new Case entities' + +administer case entities: + title: 'Administer Case entities' + description: 'Allow to access the administration form to configure Case entities.' + restrict access: true + +delete case entities: + title: 'Delete Case entities' + +edit case entities: + title: 'Edit Case entities' + +view published case entities: + title: 'View published Case entities' + +view unpublished case entities: + title: 'View unpublished Case entities' + +view all case revisions: + title: 'View all Case revisions' + +revert all case revisions: + title: 'Revert all Case revisions' + description: 'Role requires permission view Case revisions and edit rights for case entities in question or administer case entities.' + +delete all case revisions: + title: 'Delete all revisions' + description: 'Role requires permission to view Case revisions and delete rights for case entities in question or administer case entities.' +add activity entities: + title: 'Create new Activity entities' + +administer activity entities: + title: 'Administer Activity entities' + description: 'Allow to access the administration form to configure Activity entities.' + restrict access: true + +delete activity entities: + title: 'Delete Activity entities' + +edit activity entities: + title: 'Edit Activity entities' + +view published activity entities: + title: 'View published Activity entities' + +view unpublished activity entities: + title: 'View unpublished Activity entities' + +view all activity revisions: + title: 'View all Activity revisions' + +revert all activity revisions: + title: 'Revert all Activity revisions' + description: 'Role requires permission view Activity revisions and edit rights for activity entities in question or administer activity entities.' + +delete all activity revisions: + title: 'Delete all revisions' + description: 'Role requires permission to view Activity revisions and delete rights for activity entities in question or administer activity entities.' diff --git a/modules/opencase_entities/src/Controller/OCActivityController.php b/modules/opencase_entities/src/Controller/OCActivityController.php new file mode 100644 index 0000000..b848796 --- /dev/null +++ b/modules/opencase_entities/src/Controller/OCActivityController.php @@ -0,0 +1,163 @@ +entityManager()->getStorage('oc_activity')->loadRevision($oc_activity_revision); + $view_builder = $this->entityManager()->getViewBuilder('oc_activity'); + + return $view_builder->view($oc_activity); + } + + /** + * Page title callback for a Activity revision. + * + * @param int $oc_activity_revision + * The Activity revision ID. + * + * @return string + * The page title. + */ + public function revisionPageTitle($oc_activity_revision) { + $oc_activity = $this->entityManager()->getStorage('oc_activity')->loadRevision($oc_activity_revision); + return $this->t('Revision of %title from %date', ['%title' => $oc_activity->label(), '%date' => format_date($oc_activity->getRevisionCreationTime())]); + } + + /** + * Generates an overview table of older revisions of a Activity . + * + * @param \Drupal\opencase_entities\Entity\OCActivityInterface $oc_activity + * A Activity object. + * + * @return array + * An array as expected by drupal_render(). + */ + public function revisionOverview(OCActivityInterface $oc_activity) { + $account = $this->currentUser(); + $langcode = $oc_activity->language()->getId(); + $langname = $oc_activity->language()->getName(); + $languages = $oc_activity->getTranslationLanguages(); + $has_translations = (count($languages) > 1); + $oc_activity_storage = $this->entityManager()->getStorage('oc_activity'); + + $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $oc_activity->label()]) : $this->t('Revisions for %title', ['%title' => $oc_activity->label()]); + $header = [$this->t('Revision'), $this->t('Operations')]; + + $revert_permission = (($account->hasPermission("revert all activity revisions") || $account->hasPermission('administer activity entities'))); + $delete_permission = (($account->hasPermission("delete all activity revisions") || $account->hasPermission('administer activity entities'))); + + $rows = []; + + $vids = $oc_activity_storage->revisionIds($oc_activity); + + $latest_revision = TRUE; + + foreach (array_reverse($vids) as $vid) { + /** @var \Drupal\opencase_entities\OCActivityInterface $revision */ + $revision = $oc_activity_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_activity->getRevisionId()) { + $link = $this->l($date, new Url('entity.oc_activity.revision', ['oc_activity' => $oc_activity->id(), 'oc_activity_revision' => $vid])); + } + else { + $link = $oc_activity->link($date); + } + + $row = []; + $column = [ + 'data' => [ + '#type' => 'inline_template', + '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}

{{ message }}

{% 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' => '', + '#markup' => $this->t('Current revision'), + '#suffix' => '', + ], + ]; + 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_activity.translation_revert', ['oc_activity' => $oc_activity->id(), 'oc_activity_revision' => $vid, 'langcode' => $langcode]) : + Url::fromRoute('entity.oc_activity.revision_revert', ['oc_activity' => $oc_activity->id(), 'oc_activity_revision' => $vid]), + ]; + } + + if ($delete_permission) { + $links['delete'] = [ + 'title' => $this->t('Delete'), + 'url' => Url::fromRoute('entity.oc_activity.revision_delete', ['oc_activity' => $oc_activity->id(), 'oc_activity_revision' => $vid]), + ]; + } + + $row[] = [ + 'data' => [ + '#type' => 'operations', + '#links' => $links, + ], + ]; + } + + $rows[] = $row; + } + } + + $build['oc_activity_revisions_table'] = [ + '#theme' => 'table', + '#rows' => $rows, + '#header' => $header, + ]; + + return $build; + } + +} diff --git a/modules/opencase_entities/src/Controller/OCActorController.php b/modules/opencase_entities/src/Controller/OCActorController.php new file mode 100644 index 0000000..345fe78 --- /dev/null +++ b/modules/opencase_entities/src/Controller/OCActorController.php @@ -0,0 +1,163 @@ +entityManager()->getStorage('oc_actor')->loadRevision($oc_actor_revision); + $view_builder = $this->entityManager()->getViewBuilder('oc_actor'); + + return $view_builder->view($oc_actor); + } + + /** + * Page title callback for a Actor revision. + * + * @param int $oc_actor_revision + * The Actor revision ID. + * + * @return string + * The page title. + */ + public function revisionPageTitle($oc_actor_revision) { + $oc_actor = $this->entityManager()->getStorage('oc_actor')->loadRevision($oc_actor_revision); + return $this->t('Revision of %title from %date', ['%title' => $oc_actor->label(), '%date' => format_date($oc_actor->getRevisionCreationTime())]); + } + + /** + * Generates an overview table of older revisions of a Actor . + * + * @param \Drupal\opencase_entities\Entity\OCActorInterface $oc_actor + * A Actor object. + * + * @return array + * An array as expected by drupal_render(). + */ + public function revisionOverview(OCActorInterface $oc_actor) { + $account = $this->currentUser(); + $langcode = $oc_actor->language()->getId(); + $langname = $oc_actor->language()->getName(); + $languages = $oc_actor->getTranslationLanguages(); + $has_translations = (count($languages) > 1); + $oc_actor_storage = $this->entityManager()->getStorage('oc_actor'); + + $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $oc_actor->label()]) : $this->t('Revisions for %title', ['%title' => $oc_actor->label()]); + $header = [$this->t('Revision'), $this->t('Operations')]; + + $revert_permission = (($account->hasPermission("revert all actor revisions") || $account->hasPermission('administer actor entities'))); + $delete_permission = (($account->hasPermission("delete all actor revisions") || $account->hasPermission('administer actor entities'))); + + $rows = []; + + $vids = $oc_actor_storage->revisionIds($oc_actor); + + $latest_revision = TRUE; + + foreach (array_reverse($vids) as $vid) { + /** @var \Drupal\opencase_entities\OCActorInterface $revision */ + $revision = $oc_actor_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_actor->getRevisionId()) { + $link = $this->l($date, new Url('entity.oc_actor.revision', ['oc_actor' => $oc_actor->id(), 'oc_actor_revision' => $vid])); + } + else { + $link = $oc_actor->link($date); + } + + $row = []; + $column = [ + 'data' => [ + '#type' => 'inline_template', + '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}

{{ message }}

{% 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' => '', + '#markup' => $this->t('Current revision'), + '#suffix' => '', + ], + ]; + 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_actor.translation_revert', ['oc_actor' => $oc_actor->id(), 'oc_actor_revision' => $vid, 'langcode' => $langcode]) : + Url::fromRoute('entity.oc_actor.revision_revert', ['oc_actor' => $oc_actor->id(), 'oc_actor_revision' => $vid]), + ]; + } + + if ($delete_permission) { + $links['delete'] = [ + 'title' => $this->t('Delete'), + 'url' => Url::fromRoute('entity.oc_actor.revision_delete', ['oc_actor' => $oc_actor->id(), 'oc_actor_revision' => $vid]), + ]; + } + + $row[] = [ + 'data' => [ + '#type' => 'operations', + '#links' => $links, + ], + ]; + } + + $rows[] = $row; + } + } + + $build['oc_actor_revisions_table'] = [ + '#theme' => 'table', + '#rows' => $rows, + '#header' => $header, + ]; + + return $build; + } + +} diff --git a/modules/opencase_entities/src/Controller/OCCaseController.php b/modules/opencase_entities/src/Controller/OCCaseController.php new file mode 100644 index 0000000..fbb0782 --- /dev/null +++ b/modules/opencase_entities/src/Controller/OCCaseController.php @@ -0,0 +1,163 @@ +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 %}

{{ message }}

{% 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' => '', + '#markup' => $this->t('Current revision'), + '#suffix' => '', + ], + ]; + 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; + } + +} diff --git a/modules/opencase_entities/src/Entity/OCActivity.php b/modules/opencase_entities/src/Entity/OCActivity.php new file mode 100644 index 0000000..f10075e --- /dev/null +++ b/modules/opencase_entities/src/Entity/OCActivity.php @@ -0,0 +1,281 @@ + \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_activity 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); + + $fields['user_id'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Authored by')) + ->setDescription(t('The user ID of author of the Activity entity.')) + ->setRevisionable(TRUE) + ->setSetting('target_type', 'user') + ->setSetting('handler', 'default') + ->setTranslatable(TRUE) + ->setDisplayOptions('view', [ + 'label' => 'hidden', + 'type' => 'author', + 'weight' => 0, + ]) + ->setDisplayOptions('form', [ + 'type' => 'entity_reference_autocomplete', + 'weight' => 5, + 'settings' => [ + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ], + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); + + $fields['name'] = BaseFieldDefinition::create('string') + ->setLabel(t('Name')) + ->setDescription(t('The name of the Activity entity.')) + ->setRevisionable(TRUE) + ->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'] = BaseFieldDefinition::create('boolean') + ->setLabel(t('Publishing status')) + ->setDescription(t('A boolean indicating whether the Activity is published.')) + ->setRevisionable(TRUE) + ->setDefaultValue(TRUE) + ->setDisplayOptions('form', [ + 'type' => 'boolean_checkbox', + 'weight' => -3, + ]); + + $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['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; + } + +} diff --git a/modules/opencase_entities/src/Entity/OCActivityInterface.php b/modules/opencase_entities/src/Entity/OCActivityInterface.php new file mode 100644 index 0000000..f68f220 --- /dev/null +++ b/modules/opencase_entities/src/Entity/OCActivityInterface.php @@ -0,0 +1,116 @@ + \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_actor 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); + + $fields['user_id'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Authored by')) + ->setDescription(t('The user ID of author of the Actor entity.')) + ->setRevisionable(TRUE) + ->setSetting('target_type', 'user') + ->setSetting('handler', 'default') + ->setTranslatable(TRUE) + ->setDisplayOptions('view', [ + 'label' => 'hidden', + 'type' => 'author', + 'weight' => 0, + ]) + ->setDisplayOptions('form', [ + 'type' => 'entity_reference_autocomplete', + 'weight' => 5, + 'settings' => [ + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ], + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); + + $fields['name'] = BaseFieldDefinition::create('string') + ->setLabel(t('Name')) + ->setDescription(t('The name of the Actor entity.')) + ->setRevisionable(TRUE) + ->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'] = BaseFieldDefinition::create('boolean') + ->setLabel(t('Publishing status')) + ->setDescription(t('A boolean indicating whether the Actor is published.')) + ->setRevisionable(TRUE) + ->setDefaultValue(TRUE) + ->setDisplayOptions('form', [ + 'type' => 'boolean_checkbox', + 'weight' => -3, + ]); + + $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['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; + } + +} diff --git a/modules/opencase_entities/src/Entity/OCActorInterface.php b/modules/opencase_entities/src/Entity/OCActorInterface.php new file mode 100644 index 0000000..dda9548 --- /dev/null +++ b/modules/opencase_entities/src/Entity/OCActorInterface.php @@ -0,0 +1,116 @@ + \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); + + $fields['user_id'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Authored 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' => 'hidden', + 'type' => 'author', + 'weight' => 0, + ]) + ->setDisplayOptions('form', [ + 'type' => 'entity_reference_autocomplete', + 'weight' => 5, + 'settings' => [ + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ], + ]) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayConfigurable('view', TRUE); + + $fields['name'] = BaseFieldDefinition::create('string') + ->setLabel(t('Name')) + ->setDescription(t('The name of the Case entity.')) + ->setRevisionable(TRUE) + ->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'] = BaseFieldDefinition::create('boolean') + ->setLabel(t('Publishing status')) + ->setDescription(t('A boolean indicating whether the Case is published.')) + ->setRevisionable(TRUE) + ->setDefaultValue(TRUE) + ->setDisplayOptions('form', [ + 'type' => 'boolean_checkbox', + 'weight' => -3, + ]); + + $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['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; + } + +} diff --git a/modules/opencase_entities/src/Entity/OCCaseInterface.php b/modules/opencase_entities/src/Entity/OCCaseInterface.php new file mode 100644 index 0000000..ca2d20b --- /dev/null +++ b/modules/opencase_entities/src/Entity/OCCaseInterface.php @@ -0,0 +1,116 @@ +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 Activity.', [ + '%label' => $entity->label(), + ])); + break; + + default: + drupal_set_message($this->t('Saved the %label Activity.', [ + '%label' => $entity->label(), + ])); + } + $form_state->setRedirect('entity.oc_activity.canonical', ['oc_activity' => $entity->id()]); + } + +} diff --git a/modules/opencase_entities/src/Form/OCActivityRevisionDeleteForm.php b/modules/opencase_entities/src/Form/OCActivityRevisionDeleteForm.php new file mode 100644 index 0000000..adc276f --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActivityRevisionDeleteForm.php @@ -0,0 +1,123 @@ +OCActivityStorage = $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_activity'), + $container->get('database') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oc_activity_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_activity.version_history', ['oc_activity' => $this->revision->id()]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $oc_activity_revision = NULL) { + $this->revision = $this->OCActivityStorage->loadRevision($oc_activity_revision); + $form = parent::buildForm($form, $form_state); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->OCActivityStorage->deleteRevision($this->revision->getRevisionId()); + + $this->logger('content')->notice('Activity: deleted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + drupal_set_message(t('Revision from %revision-date of Activity %title has been deleted.', ['%revision-date' => format_date($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()])); + $form_state->setRedirect( + 'entity.oc_activity.canonical', + ['oc_activity' => $this->revision->id()] + ); + if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {oc_activity_field_revision} WHERE id = :id', [':id' => $this->revision->id()])->fetchField() > 1) { + $form_state->setRedirect( + 'entity.oc_activity.version_history', + ['oc_activity' => $this->revision->id()] + ); + } + } + +} diff --git a/modules/opencase_entities/src/Form/OCActivityRevisionRevertForm.php b/modules/opencase_entities/src/Form/OCActivityRevisionRevertForm.php new file mode 100644 index 0000000..5fc3c8b --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActivityRevisionRevertForm.php @@ -0,0 +1,149 @@ +OCActivityStorage = $entity_storage; + $this->dateFormatter = $date_formatter; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorage('oc_activity'), + $container->get('date.formatter') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oc_activity_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_activity.version_history', ['oc_activity' => $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_activity_revision = NULL) { + $this->revision = $this->OCActivityStorage->loadRevision($oc_activity_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('Activity: reverted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + drupal_set_message(t('Activity %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_activity.version_history', + ['oc_activity' => $this->revision->id()] + ); + } + + /** + * Prepares a revision to be reverted. + * + * @param \Drupal\opencase_entities\Entity\OCActivityInterface $revision + * The revision to be reverted. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return \Drupal\opencase_entities\Entity\OCActivityInterface + * The prepared revision ready to be stored. + */ + protected function prepareRevertedRevision(OCActivityInterface $revision, FormStateInterface $form_state) { + $revision->setNewRevision(); + $revision->isDefaultRevision(TRUE); + $revision->setRevisionCreationTime(REQUEST_TIME); + + return $revision; + } + +} diff --git a/modules/opencase_entities/src/Form/OCActivityRevisionRevertTranslationForm.php b/modules/opencase_entities/src/Form/OCActivityRevisionRevertTranslationForm.php new file mode 100644 index 0000000..e000cd3 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActivityRevisionRevertTranslationForm.php @@ -0,0 +1,115 @@ +languageManager = $language_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorage('oc_activity'), + $container->get('date.formatter'), + $container->get('language_manager') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oc_activity_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_activity_revision = NULL, $langcode = NULL) { + $this->langcode = $langcode; + $form = parent::buildForm($form, $form_state, $oc_activity_revision); + + $form['revert_untranslated_fields'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Revert content shared among translations'), + '#default_value' => FALSE, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + protected function prepareRevertedRevision(OCActivityInterface $revision, FormStateInterface $form_state) { + $revert_untranslated_fields = $form_state->getValue('revert_untranslated_fields'); + + /** @var \Drupal\opencase_entities\Entity\OCActivityInterface $default_revision */ + $latest_revision = $this->OCActivityStorage->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; + } + +} diff --git a/modules/opencase_entities/src/Form/OCActivitySettingsForm.php b/modules/opencase_entities/src/Form/OCActivitySettingsForm.php new file mode 100644 index 0000000..9225b27 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActivitySettingsForm.php @@ -0,0 +1,53 @@ +t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return new Url('entity.oc_activity_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()); + } + +} diff --git a/modules/opencase_entities/src/Form/OCActivityTypeForm.php b/modules/opencase_entities/src/Form/OCActivityTypeForm.php new file mode 100644 index 0000000..845106b --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActivityTypeForm.php @@ -0,0 +1,65 @@ +entity; + $form['label'] = [ + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $oc_activity_type->label(), + '#description' => $this->t("Label for the Activity type."), + '#required' => TRUE, + ]; + + $form['id'] = [ + '#type' => 'machine_name', + '#default_value' => $oc_activity_type->id(), + '#machine_name' => [ + 'exists' => '\Drupal\opencase_entities\Entity\OCActivityType::load', + ], + '#disabled' => !$oc_activity_type->isNew(), + ]; + + /* You will need additional form elements for your custom properties. */ + + return $form; + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $oc_activity_type = $this->entity; + $status = $oc_activity_type->save(); + + switch ($status) { + case SAVED_NEW: + drupal_set_message($this->t('Created the %label Activity type.', [ + '%label' => $oc_activity_type->label(), + ])); + break; + + default: + drupal_set_message($this->t('Saved the %label Activity type.', [ + '%label' => $oc_activity_type->label(), + ])); + } + $form_state->setRedirectUrl($oc_activity_type->toUrl('collection')); + } + +} diff --git a/modules/opencase_entities/src/Form/OCActorDeleteForm.php b/modules/opencase_entities/src/Form/OCActorDeleteForm.php new file mode 100644 index 0000000..96dae73 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActorDeleteForm.php @@ -0,0 +1,15 @@ +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 Actor.', [ + '%label' => $entity->label(), + ])); + break; + + default: + drupal_set_message($this->t('Saved the %label Actor.', [ + '%label' => $entity->label(), + ])); + } + $form_state->setRedirect('entity.oc_actor.canonical', ['oc_actor' => $entity->id()]); + } + +} diff --git a/modules/opencase_entities/src/Form/OCActorRevisionDeleteForm.php b/modules/opencase_entities/src/Form/OCActorRevisionDeleteForm.php new file mode 100644 index 0000000..a4c686b --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActorRevisionDeleteForm.php @@ -0,0 +1,123 @@ +OCActorStorage = $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_actor'), + $container->get('database') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oc_actor_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_actor.version_history', ['oc_actor' => $this->revision->id()]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $oc_actor_revision = NULL) { + $this->revision = $this->OCActorStorage->loadRevision($oc_actor_revision); + $form = parent::buildForm($form, $form_state); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->OCActorStorage->deleteRevision($this->revision->getRevisionId()); + + $this->logger('content')->notice('Actor: deleted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + drupal_set_message(t('Revision from %revision-date of Actor %title has been deleted.', ['%revision-date' => format_date($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()])); + $form_state->setRedirect( + 'entity.oc_actor.canonical', + ['oc_actor' => $this->revision->id()] + ); + if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {oc_actor_field_revision} WHERE id = :id', [':id' => $this->revision->id()])->fetchField() > 1) { + $form_state->setRedirect( + 'entity.oc_actor.version_history', + ['oc_actor' => $this->revision->id()] + ); + } + } + +} diff --git a/modules/opencase_entities/src/Form/OCActorRevisionRevertForm.php b/modules/opencase_entities/src/Form/OCActorRevisionRevertForm.php new file mode 100644 index 0000000..0842dcd --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActorRevisionRevertForm.php @@ -0,0 +1,149 @@ +OCActorStorage = $entity_storage; + $this->dateFormatter = $date_formatter; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorage('oc_actor'), + $container->get('date.formatter') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oc_actor_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_actor.version_history', ['oc_actor' => $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_actor_revision = NULL) { + $this->revision = $this->OCActorStorage->loadRevision($oc_actor_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('Actor: reverted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + drupal_set_message(t('Actor %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_actor.version_history', + ['oc_actor' => $this->revision->id()] + ); + } + + /** + * Prepares a revision to be reverted. + * + * @param \Drupal\opencase_entities\Entity\OCActorInterface $revision + * The revision to be reverted. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return \Drupal\opencase_entities\Entity\OCActorInterface + * The prepared revision ready to be stored. + */ + protected function prepareRevertedRevision(OCActorInterface $revision, FormStateInterface $form_state) { + $revision->setNewRevision(); + $revision->isDefaultRevision(TRUE); + $revision->setRevisionCreationTime(REQUEST_TIME); + + return $revision; + } + +} diff --git a/modules/opencase_entities/src/Form/OCActorRevisionRevertTranslationForm.php b/modules/opencase_entities/src/Form/OCActorRevisionRevertTranslationForm.php new file mode 100644 index 0000000..5eb0a12 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActorRevisionRevertTranslationForm.php @@ -0,0 +1,115 @@ +languageManager = $language_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager')->getStorage('oc_actor'), + $container->get('date.formatter'), + $container->get('language_manager') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oc_actor_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_actor_revision = NULL, $langcode = NULL) { + $this->langcode = $langcode; + $form = parent::buildForm($form, $form_state, $oc_actor_revision); + + $form['revert_untranslated_fields'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Revert content shared among translations'), + '#default_value' => FALSE, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + protected function prepareRevertedRevision(OCActorInterface $revision, FormStateInterface $form_state) { + $revert_untranslated_fields = $form_state->getValue('revert_untranslated_fields'); + + /** @var \Drupal\opencase_entities\Entity\OCActorInterface $default_revision */ + $latest_revision = $this->OCActorStorage->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; + } + +} diff --git a/modules/opencase_entities/src/Form/OCActorSettingsForm.php b/modules/opencase_entities/src/Form/OCActorSettingsForm.php new file mode 100644 index 0000000..827e81b --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActorSettingsForm.php @@ -0,0 +1,53 @@ +t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return new Url('entity.oc_actor_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()); + } + +} diff --git a/modules/opencase_entities/src/Form/OCActorTypeForm.php b/modules/opencase_entities/src/Form/OCActorTypeForm.php new file mode 100644 index 0000000..b138366 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCActorTypeForm.php @@ -0,0 +1,65 @@ +entity; + $form['label'] = [ + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $oc_actor_type->label(), + '#description' => $this->t("Label for the Actor type."), + '#required' => TRUE, + ]; + + $form['id'] = [ + '#type' => 'machine_name', + '#default_value' => $oc_actor_type->id(), + '#machine_name' => [ + 'exists' => '\Drupal\opencase_entities\Entity\OCActorType::load', + ], + '#disabled' => !$oc_actor_type->isNew(), + ]; + + /* You will need additional form elements for your custom properties. */ + + return $form; + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + $oc_actor_type = $this->entity; + $status = $oc_actor_type->save(); + + switch ($status) { + case SAVED_NEW: + drupal_set_message($this->t('Created the %label Actor type.', [ + '%label' => $oc_actor_type->label(), + ])); + break; + + default: + drupal_set_message($this->t('Saved the %label Actor type.', [ + '%label' => $oc_actor_type->label(), + ])); + } + $form_state->setRedirectUrl($oc_actor_type->toUrl('collection')); + } + +} diff --git a/modules/opencase_entities/src/Form/OCCaseDeleteForm.php b/modules/opencase_entities/src/Form/OCCaseDeleteForm.php new file mode 100644 index 0000000..c2c317d --- /dev/null +++ b/modules/opencase_entities/src/Form/OCCaseDeleteForm.php @@ -0,0 +1,15 @@ +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()]); + } + +} diff --git a/modules/opencase_entities/src/Form/OCCaseRevisionDeleteForm.php b/modules/opencase_entities/src/Form/OCCaseRevisionDeleteForm.php new file mode 100644 index 0000000..f3de820 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCCaseRevisionDeleteForm.php @@ -0,0 +1,123 @@ +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()] + ); + } + } + +} diff --git a/modules/opencase_entities/src/Form/OCCaseRevisionRevertForm.php b/modules/opencase_entities/src/Form/OCCaseRevisionRevertForm.php new file mode 100644 index 0000000..e86c5b8 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCCaseRevisionRevertForm.php @@ -0,0 +1,149 @@ +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; + } + +} diff --git a/modules/opencase_entities/src/Form/OCCaseRevisionRevertTranslationForm.php b/modules/opencase_entities/src/Form/OCCaseRevisionRevertTranslationForm.php new file mode 100644 index 0000000..8edf6c5 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCCaseRevisionRevertTranslationForm.php @@ -0,0 +1,115 @@ +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; + } + +} diff --git a/modules/opencase_entities/src/Form/OCCaseSettingsForm.php b/modules/opencase_entities/src/Form/OCCaseSettingsForm.php new file mode 100644 index 0000000..217ed23 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCCaseSettingsForm.php @@ -0,0 +1,53 @@ +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()); + } + +} diff --git a/modules/opencase_entities/src/Form/OCCaseTypeForm.php b/modules/opencase_entities/src/Form/OCCaseTypeForm.php new file mode 100644 index 0000000..bace3a0 --- /dev/null +++ b/modules/opencase_entities/src/Form/OCCaseTypeForm.php @@ -0,0 +1,65 @@ +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')); + } + +} diff --git a/modules/opencase_entities/src/OCActivityAccessControlHandler.php b/modules/opencase_entities/src/OCActivityAccessControlHandler.php new file mode 100644 index 0000000..7d48a41 --- /dev/null +++ b/modules/opencase_entities/src/OCActivityAccessControlHandler.php @@ -0,0 +1,47 @@ +isPublished()) { + return AccessResult::allowedIfHasPermission($account, 'view unpublished activity entities'); + } + return AccessResult::allowedIfHasPermission($account, 'view published activity entities'); + + case 'update': + return AccessResult::allowedIfHasPermission($account, 'edit activity entities'); + + case 'delete': + return AccessResult::allowedIfHasPermission($account, 'delete activity entities'); + } + + // Unknown operation, no opinion. + return AccessResult::neutral(); + } + + /** + * {@inheritdoc} + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { + return AccessResult::allowedIfHasPermission($account, 'add activity entities'); + } + +} diff --git a/modules/opencase_entities/src/OCActivityHtmlRouteProvider.php b/modules/opencase_entities/src/OCActivityHtmlRouteProvider.php new file mode 100644 index 0000000..07fd009 --- /dev/null +++ b/modules/opencase_entities/src/OCActivityHtmlRouteProvider.php @@ -0,0 +1,196 @@ +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\OCActivityController::revisionOverview', + ]) + ->setRequirement('_permission', 'access activity 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\OCActivityController::revisionShow', + '_title_callback' => '\Drupal\opencase_entities\Controller\OCActivityController::revisionPageTitle', + ]) + ->setRequirement('_permission', 'access activity 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\OCActivityRevisionRevertForm', + '_title' => 'Revert to earlier revision', + ]) + ->setRequirement('_permission', 'revert all activity 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\OCActivityRevisionDeleteForm', + '_title' => 'Delete earlier revision', + ]) + ->setRequirement('_permission', 'delete all activity 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\OCActivityRevisionRevertTranslationForm', + '_title' => 'Revert to earlier revision of a translation', + ]) + ->setRequirement('_permission', 'revert all activity 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\OCActivitySettingsForm', + '_title' => "{$entity_type->getLabel()} settings", + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/modules/opencase_entities/src/OCActivityListBuilder.php b/modules/opencase_entities/src/OCActivityListBuilder.php new file mode 100644 index 0000000..1c750ee --- /dev/null +++ b/modules/opencase_entities/src/OCActivityListBuilder.php @@ -0,0 +1,40 @@ +t('Activity ID'); + $header['name'] = $this->t('Name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + /* @var $entity \Drupal\opencase_entities\Entity\OCActivity */ + $row['id'] = $entity->id(); + $row['name'] = Link::createFromRoute( + $entity->label(), + 'entity.oc_activity.edit_form', + ['oc_activity' => $entity->id()] + ); + return $row + parent::buildRow($entity); + } + +} diff --git a/modules/opencase_entities/src/OCActivityStorage.php b/modules/opencase_entities/src/OCActivityStorage.php new file mode 100644 index 0000000..4b3189b --- /dev/null +++ b/modules/opencase_entities/src/OCActivityStorage.php @@ -0,0 +1,58 @@ +database->query( + 'SELECT vid FROM {oc_activity_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_activity_field_revision} WHERE uid = :uid ORDER BY vid', + [':uid' => $account->id()] + )->fetchCol(); + } + + /** + * {@inheritdoc} + */ + public function countDefaultLanguageRevisions(OCActivityInterface $entity) { + return $this->database->query('SELECT COUNT(*) FROM {oc_activity_field_revision} WHERE id = :id AND default_langcode = 1', [':id' => $entity->id()]) + ->fetchField(); + } + + /** + * {@inheritdoc} + */ + public function clearRevisionsLanguage(LanguageInterface $language) { + return $this->database->update('oc_activity_revision') + ->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]) + ->condition('langcode', $language->getId()) + ->execute(); + } + +} diff --git a/modules/opencase_entities/src/OCActivityStorageInterface.php b/modules/opencase_entities/src/OCActivityStorageInterface.php new file mode 100644 index 0000000..f483e70 --- /dev/null +++ b/modules/opencase_entities/src/OCActivityStorageInterface.php @@ -0,0 +1,61 @@ +t('Activity 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); + } + +} diff --git a/modules/opencase_entities/src/OCActorAccessControlHandler.php b/modules/opencase_entities/src/OCActorAccessControlHandler.php new file mode 100644 index 0000000..8eeda18 --- /dev/null +++ b/modules/opencase_entities/src/OCActorAccessControlHandler.php @@ -0,0 +1,47 @@ +isPublished()) { + return AccessResult::allowedIfHasPermission($account, 'view unpublished actor entities'); + } + return AccessResult::allowedIfHasPermission($account, 'view published actor entities'); + + case 'update': + return AccessResult::allowedIfHasPermission($account, 'edit actor entities'); + + case 'delete': + return AccessResult::allowedIfHasPermission($account, 'delete actor entities'); + } + + // Unknown operation, no opinion. + return AccessResult::neutral(); + } + + /** + * {@inheritdoc} + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { + return AccessResult::allowedIfHasPermission($account, 'add actor entities'); + } + +} diff --git a/modules/opencase_entities/src/OCActorHtmlRouteProvider.php b/modules/opencase_entities/src/OCActorHtmlRouteProvider.php new file mode 100644 index 0000000..4601625 --- /dev/null +++ b/modules/opencase_entities/src/OCActorHtmlRouteProvider.php @@ -0,0 +1,196 @@ +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\OCActorController::revisionOverview', + ]) + ->setRequirement('_permission', 'access actor 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\OCActorController::revisionShow', + '_title_callback' => '\Drupal\opencase_entities\Controller\OCActorController::revisionPageTitle', + ]) + ->setRequirement('_permission', 'access actor 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\OCActorRevisionRevertForm', + '_title' => 'Revert to earlier revision', + ]) + ->setRequirement('_permission', 'revert all actor 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\OCActorRevisionDeleteForm', + '_title' => 'Delete earlier revision', + ]) + ->setRequirement('_permission', 'delete all actor 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\OCActorRevisionRevertTranslationForm', + '_title' => 'Revert to earlier revision of a translation', + ]) + ->setRequirement('_permission', 'revert all actor 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\OCActorSettingsForm', + '_title' => "{$entity_type->getLabel()} settings", + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/modules/opencase_entities/src/OCActorListBuilder.php b/modules/opencase_entities/src/OCActorListBuilder.php new file mode 100644 index 0000000..17c9220 --- /dev/null +++ b/modules/opencase_entities/src/OCActorListBuilder.php @@ -0,0 +1,40 @@ +t('Actor ID'); + $header['name'] = $this->t('Name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + /* @var $entity \Drupal\opencase_entities\Entity\OCActor */ + $row['id'] = $entity->id(); + $row['name'] = Link::createFromRoute( + $entity->label(), + 'entity.oc_actor.edit_form', + ['oc_actor' => $entity->id()] + ); + return $row + parent::buildRow($entity); + } + +} diff --git a/modules/opencase_entities/src/OCActorStorage.php b/modules/opencase_entities/src/OCActorStorage.php new file mode 100644 index 0000000..6c2b2dc --- /dev/null +++ b/modules/opencase_entities/src/OCActorStorage.php @@ -0,0 +1,58 @@ +database->query( + 'SELECT vid FROM {oc_actor_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_actor_field_revision} WHERE uid = :uid ORDER BY vid', + [':uid' => $account->id()] + )->fetchCol(); + } + + /** + * {@inheritdoc} + */ + public function countDefaultLanguageRevisions(OCActorInterface $entity) { + return $this->database->query('SELECT COUNT(*) FROM {oc_actor_field_revision} WHERE id = :id AND default_langcode = 1', [':id' => $entity->id()]) + ->fetchField(); + } + + /** + * {@inheritdoc} + */ + public function clearRevisionsLanguage(LanguageInterface $language) { + return $this->database->update('oc_actor_revision') + ->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]) + ->condition('langcode', $language->getId()) + ->execute(); + } + +} diff --git a/modules/opencase_entities/src/OCActorStorageInterface.php b/modules/opencase_entities/src/OCActorStorageInterface.php new file mode 100644 index 0000000..b46865a --- /dev/null +++ b/modules/opencase_entities/src/OCActorStorageInterface.php @@ -0,0 +1,61 @@ +t('Actor 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); + } + +} diff --git a/modules/opencase_entities/src/OCCaseAccessControlHandler.php b/modules/opencase_entities/src/OCCaseAccessControlHandler.php new file mode 100644 index 0000000..2d98c2f --- /dev/null +++ b/modules/opencase_entities/src/OCCaseAccessControlHandler.php @@ -0,0 +1,47 @@ +isPublished()) { + return AccessResult::allowedIfHasPermission($account, 'view unpublished case entities'); + } + return AccessResult::allowedIfHasPermission($account, 'view published case entities'); + + case 'update': + return AccessResult::allowedIfHasPermission($account, 'edit case entities'); + + case 'delete': + return AccessResult::allowedIfHasPermission($account, 'delete case entities'); + } + + // 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'); + } + +} diff --git a/modules/opencase_entities/src/OCCaseHtmlRouteProvider.php b/modules/opencase_entities/src/OCCaseHtmlRouteProvider.php new file mode 100644 index 0000000..a039952 --- /dev/null +++ b/modules/opencase_entities/src/OCCaseHtmlRouteProvider.php @@ -0,0 +1,196 @@ +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; + } + } + +} diff --git a/modules/opencase_entities/src/OCCaseListBuilder.php b/modules/opencase_entities/src/OCCaseListBuilder.php new file mode 100644 index 0000000..0ab0404 --- /dev/null +++ b/modules/opencase_entities/src/OCCaseListBuilder.php @@ -0,0 +1,40 @@ +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); + } + +} diff --git a/modules/opencase_entities/src/OCCaseStorage.php b/modules/opencase_entities/src/OCCaseStorage.php new file mode 100644 index 0000000..8f7e2eb --- /dev/null +++ b/modules/opencase_entities/src/OCCaseStorage.php @@ -0,0 +1,58 @@ +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(); + } + +} diff --git a/modules/opencase_entities/src/OCCaseStorageInterface.php b/modules/opencase_entities/src/OCCaseStorageInterface.php new file mode 100644 index 0000000..585a363 --- /dev/null +++ b/modules/opencase_entities/src/OCCaseStorageInterface.php @@ -0,0 +1,61 @@ +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); + } + +} diff --git a/modules/opencase_entities/templates/oc-activity-content-add-list.html.twig b/modules/opencase_entities/templates/oc-activity-content-add-list.html.twig new file mode 100644 index 0000000..7d4e5cc --- /dev/null +++ b/modules/opencase_entities/templates/oc-activity-content-add-list.html.twig @@ -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_activity_content_add_list() + * + * @ingroup themeable + */ +#} +{% spaceless %} +
+ {% for type in types %} +
{{ type.link }}
+ {% endfor %} +
+{% endspaceless %} diff --git a/modules/opencase_entities/templates/oc-actor-content-add-list.html.twig b/modules/opencase_entities/templates/oc-actor-content-add-list.html.twig new file mode 100644 index 0000000..5d7add2 --- /dev/null +++ b/modules/opencase_entities/templates/oc-actor-content-add-list.html.twig @@ -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_actor_content_add_list() + * + * @ingroup themeable + */ +#} +{% spaceless %} +
+ {% for type in types %} +
{{ type.link }}
+ {% endfor %} +
+{% endspaceless %} diff --git a/modules/opencase_entities/templates/oc-case-content-add-list.html.twig b/modules/opencase_entities/templates/oc-case-content-add-list.html.twig new file mode 100644 index 0000000..9a3fa9b --- /dev/null +++ b/modules/opencase_entities/templates/oc-case-content-add-list.html.twig @@ -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 %} +
+ {% for type in types %} +
{{ type.link }}
+ {% endfor %} +
+{% endspaceless %} diff --git a/modules/opencase_entities/templates/oc_activity.html.twig b/modules/opencase_entities/templates/oc_activity.html.twig new file mode 100644 index 0000000..20fa995 --- /dev/null +++ b/modules/opencase_entities/templates/oc_activity.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @file oc_activity.html.twig + * Default theme implementation to present Activity data. + * + * This template is used when viewing Activity 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_activity() + * + * @ingroup themeable + */ +#} + + {% if content %} + {{- content -}} + {% endif %} + diff --git a/modules/opencase_entities/templates/oc_actor.html.twig b/modules/opencase_entities/templates/oc_actor.html.twig new file mode 100644 index 0000000..e92f6da --- /dev/null +++ b/modules/opencase_entities/templates/oc_actor.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @file oc_actor.html.twig + * Default theme implementation to present Actor data. + * + * This template is used when viewing Actor 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_actor() + * + * @ingroup themeable + */ +#} + + {% if content %} + {{- content -}} + {% endif %} + diff --git a/modules/opencase_entities/templates/oc_case.html.twig b/modules/opencase_entities/templates/oc_case.html.twig new file mode 100644 index 0000000..299f447 --- /dev/null +++ b/modules/opencase_entities/templates/oc_case.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @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 + */ +#} + + {% if content %} + {{- content -}} + {% endif %} +