diff --git a/modules/opencase_cases/config/schema/oc_actor_case_relation_type.schema.yml b/modules/opencase_cases/config/schema/oc_actor_case_relation_type.schema.yml new file mode 100644 index 0000000..b205969 --- /dev/null +++ b/modules/opencase_cases/config/schema/oc_actor_case_relation_type.schema.yml @@ -0,0 +1,12 @@ +opencase_cases.oc_actor_case_relation_type.*: + type: config_entity + label: 'Actor-Case Relation type config' + mapping: + id: + type: string + label: 'ID' + label: + type: label + label: 'Label' + uuid: + type: string diff --git a/modules/opencase_cases/oc_actor_case_relation.page.inc b/modules/opencase_cases/oc_actor_case_relation.page.inc new file mode 100644 index 0000000..2f5e8e2 --- /dev/null +++ b/modules/opencase_cases/oc_actor_case_relation.page.inc @@ -0,0 +1,30 @@ +addJoin('INNER', 'oc_case__actors_involved', 'access_filter', 'access_filter.entity_id = oc_activity_field_data.oc_case'); $query->condition('access_filter.actors_involved_target_id', $linked_actor_id); } + +/** + * Implements hook_theme(). + */ +function opencase_cases_theme() { + $theme = []; + $theme['oc_actor_case_relation'] = [ + 'render element' => 'elements', + 'file' => 'oc_actor_case_relation.page.inc', + 'template' => 'oc_actor_case_relation', + ]; + $theme['oc_actor_case_relation_content_add_list'] = [ + 'render element' => 'content', + 'variables' => ['content' => NULL], + 'file' => 'oc_actor_case_relation.page.inc', + ]; + return $theme; +} + +/** + * Implements hook_theme_suggestions_HOOK(). + */ +function opencase_cases_theme_suggestions_oc_actor_case_relation(array $variables) { + $suggestions = []; + $entity = $variables['elements']['#oc_actor_case_relation']; + $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); + + $suggestions[] = 'oc_actor_case_relation__' . $sanitized_view_mode; + $suggestions[] = 'oc_actor_case_relation__' . $entity->bundle(); + $suggestions[] = 'oc_actor_case_relation__' . $entity->bundle() . '__' . $sanitized_view_mode; + $suggestions[] = 'oc_actor_case_relation__' . $entity->id(); + $suggestions[] = 'oc_actor_case_relation__' . $entity->id() . '__' . $sanitized_view_mode; + return $suggestions; +} diff --git a/modules/opencase_cases/opencase_cases.permissions.yml b/modules/opencase_cases/opencase_cases.permissions.yml new file mode 100644 index 0000000..b73f8f4 --- /dev/null +++ b/modules/opencase_cases/opencase_cases.permissions.yml @@ -0,0 +1,33 @@ +add actor-case relation entities: + title: 'Create new Actor-Case Relation entities' + +administer actor-case relation entities: + title: 'Administer Actor-Case Relation entities' + description: 'Allow to access the administration form to configure Actor-Case Relation entities.' + restrict access: true + +delete actor-case relation entities: + title: 'Delete Actor-Case Relation entities' + +edit actor-case relation entities: + title: 'Edit Actor-Case Relation entities' + +view published actor-case relation entities: + title: 'View published Actor-Case Relation entities' + +view unpublished actor-case relation entities: + title: 'View unpublished Actor-Case Relation entities' + +view all actor-case relation revisions: + title: 'View all Actor-Case Relation revisions' + +revert all actor-case relation revisions: + title: 'Revert all Actor-Case Relation revisions' + description: 'Role requires permission view Actor-Case Relation revisions and edit rights for actor-case relation entities in question or administer actor-case relation entities.' + +delete all actor-case relation revisions: + title: 'Delete all revisions' + description: 'Role requires permission to view Actor-Case Relation revisions and delete rights for actor-case relation entities in question or administer actor-case relation entities.' + +permission_callbacks: + - \Drupal\opencase_cases\OCActorCaseRelationPermissions::generatePermissions diff --git a/modules/opencase_cases/src/Controller/OCActorCaseRelationController.php b/modules/opencase_cases/src/Controller/OCActorCaseRelationController.php new file mode 100644 index 0000000..79d75cd --- /dev/null +++ b/modules/opencase_cases/src/Controller/OCActorCaseRelationController.php @@ -0,0 +1,209 @@ +dateFormatter = $container->get('date.formatter'); + $instance->renderer = $container->get('renderer'); + return $instance; + } + + /** + * Displays a Actor-Case Relation revision. + * + * @param int $oc_actor_case_relation_revision + * The Actor-Case Relation revision ID. + * + * @return array + * An array suitable for drupal_render(). + */ + public function revisionShow($oc_actor_case_relation_revision) { + $oc_actor_case_relation = $this->entityTypeManager()->getStorage('oc_actor_case_relation') + ->loadRevision($oc_actor_case_relation_revision); + $view_builder = $this->entityTypeManager()->getViewBuilder('oc_actor_case_relation'); + + return $view_builder->view($oc_actor_case_relation); + } + + /** + * Page title callback for a Actor-Case Relation revision. + * + * @param int $oc_actor_case_relation_revision + * The Actor-Case Relation revision ID. + * + * @return string + * The page title. + */ + public function revisionPageTitle($oc_actor_case_relation_revision) { + $oc_actor_case_relation = $this->entityTypeManager()->getStorage('oc_actor_case_relation') + ->loadRevision($oc_actor_case_relation_revision); + return $this->t('Revision of %title from %date', [ + '%title' => $oc_actor_case_relation->label(), + '%date' => $this->dateFormatter->format($oc_actor_case_relation->getRevisionCreationTime()), + ]); + } + + /** + * Generates an overview table of older revisions of a Actor-Case Relation. + * + * @param \Drupal\opencase_cases\Entity\OCActorCaseRelationInterface $oc_actor_case_relation + * A Actor-Case Relation object. + * + * @return array + * An array as expected by drupal_render(). + */ + public function revisionOverview(OCActorCaseRelationInterface $oc_actor_case_relation) { + $account = $this->currentUser(); + $oc_actor_case_relation_storage = $this->entityTypeManager()->getStorage('oc_actor_case_relation'); + + $langcode = $oc_actor_case_relation->language()->getId(); + $langname = $oc_actor_case_relation->language()->getName(); + $languages = $oc_actor_case_relation->getTranslationLanguages(); + $has_translations = (count($languages) > 1); + $build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $oc_actor_case_relation->label()]) : $this->t('Revisions for %title', ['%title' => $oc_actor_case_relation->label()]); + + $header = [$this->t('Revision'), $this->t('Operations')]; + $revert_permission = (($account->hasPermission("revert all actor-case relation revisions") || $account->hasPermission('administer actor-case relation entities'))); + $delete_permission = (($account->hasPermission("delete all actor-case relation revisions") || $account->hasPermission('administer actor-case relation entities'))); + + $rows = []; + + $vids = $oc_actor_case_relation_storage->revisionIds($oc_actor_case_relation); + + $latest_revision = TRUE; + + foreach (array_reverse($vids) as $vid) { + /** @var \Drupal\opencase_cases\OCActorCaseRelationInterface $revision */ + $revision = $oc_actor_case_relation_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 = $this->dateFormatter->format($revision->getRevisionCreationTime(), 'short'); + if ($vid != $oc_actor_case_relation->getRevisionId()) { + $link = $this->l($date, new Url('entity.oc_actor_case_relation.revision', [ + 'oc_actor_case_relation' => $oc_actor_case_relation->id(), + 'oc_actor_case_relation_revision' => $vid, + ])); + } + else { + $link = $oc_actor_case_relation->link($date); + } + + $row = []; + $column = [ + 'data' => [ + '#type' => 'inline_template', + '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}

{{ message }}

{% endif %}', + '#context' => [ + 'date' => $link, + 'username' => $this->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_case_relation.translation_revert', [ + 'oc_actor_case_relation' => $oc_actor_case_relation->id(), + 'oc_actor_case_relation_revision' => $vid, + 'langcode' => $langcode, + ]) : + Url::fromRoute('entity.oc_actor_case_relation.revision_revert', [ + 'oc_actor_case_relation' => $oc_actor_case_relation->id(), + 'oc_actor_case_relation_revision' => $vid, + ]), + ]; + } + + if ($delete_permission) { + $links['delete'] = [ + 'title' => $this->t('Delete'), + 'url' => Url::fromRoute('entity.oc_actor_case_relation.revision_delete', [ + 'oc_actor_case_relation' => $oc_actor_case_relation->id(), + 'oc_actor_case_relation_revision' => $vid, + ]), + ]; + } + + $row[] = [ + 'data' => [ + '#type' => 'operations', + '#links' => $links, + ], + ]; + } + + $rows[] = $row; + } + } + + $build['oc_actor_case_relation_revisions_table'] = [ + '#theme' => 'table', + '#rows' => $rows, + '#header' => $header, + ]; + + return $build; + } + +} diff --git a/modules/opencase_cases/src/Entity/OCActorCaseRelation.php b/modules/opencase_cases/src/Entity/OCActorCaseRelation.php new file mode 100644 index 0000000..f6559f8 --- /dev/null +++ b/modules/opencase_cases/src/Entity/OCActorCaseRelation.php @@ -0,0 +1,268 @@ + \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_case_relation 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 static function baseFieldDefinitions(EntityTypeInterface $entity_type) { + $fields = parent::baseFieldDefinitions($entity_type); + + // Add the published field. + $fields += static::publishedBaseFieldDefinitions($entity_type); + + $fields['user_id'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Authored by')) + ->setDescription(t('The user ID of author of the Actor-Case Relation 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-Case Relation 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']->setDescription(t('A boolean indicating whether the Actor-Case Relation is published.')) + ->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_cases/src/Entity/OCActorCaseRelationInterface.php b/modules/opencase_cases/src/Entity/OCActorCaseRelationInterface.php new file mode 100644 index 0000000..37c4e01 --- /dev/null +++ b/modules/opencase_cases/src/Entity/OCActorCaseRelationInterface.php @@ -0,0 +1,98 @@ +account = $container->get('current_user'); + return $instance; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + /* @var \Drupal\opencase_cases\Entity\OCActorCaseRelation $entity */ + $form = parent::buildForm($form, $form_state); + + if (!$this->entity->isNew()) { + $form['new_revision'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Create new revision'), + '#default_value' => FALSE, + '#weight' => 10, + ]; + } + + 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($this->time->getRequestTime()); + $entity->setRevisionUserId($this->account->id()); + } + else { + $entity->setNewRevision(FALSE); + } + + $status = parent::save($form, $form_state); + + switch ($status) { + case SAVED_NEW: + $this->messenger()->addMessage($this->t('Created the %label Actor-Case Relation.', [ + '%label' => $entity->label(), + ])); + break; + + default: + $this->messenger()->addMessage($this->t('Saved the %label Actor-Case Relation.', [ + '%label' => $entity->label(), + ])); + } + $form_state->setRedirect('entity.oc_actor_case_relation.canonical', ['oc_actor_case_relation' => $entity->id()]); + } + +} diff --git a/modules/opencase_cases/src/Form/OCActorCaseRelationRevisionDeleteForm.php b/modules/opencase_cases/src/Form/OCActorCaseRelationRevisionDeleteForm.php new file mode 100644 index 0000000..23be1ca --- /dev/null +++ b/modules/opencase_cases/src/Form/OCActorCaseRelationRevisionDeleteForm.php @@ -0,0 +1,108 @@ +oCActorCaseRelationStorage = $container->get('entity_type.manager')->getStorage('oc_actor_case_relation'); + $instance->connection = $container->get('database'); + return $instance; + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oc_actor_case_relation_revision_delete_confirm'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return $this->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_case_relation.version_history', ['oc_actor_case_relation' => $this->revision->id()]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $oc_actor_case_relation_revision = NULL) { + $this->revision = $this->OCActorCaseRelationStorage->loadRevision($oc_actor_case_relation_revision); + $form = parent::buildForm($form, $form_state); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->OCActorCaseRelationStorage->deleteRevision($this->revision->getRevisionId()); + + $this->logger('content')->notice('Actor-Case Relation: deleted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + $this->messenger()->addMessage(t('Revision from %revision-date of Actor-Case Relation %title has been deleted.', ['%revision-date' => format_date($this->revision->getRevisionCreationTime()), '%title' => $this->revision->label()])); + $form_state->setRedirect( + 'entity.oc_actor_case_relation.canonical', + ['oc_actor_case_relation' => $this->revision->id()] + ); + if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {oc_actor_case_relation_field_revision} WHERE id = :id', [':id' => $this->revision->id()])->fetchField() > 1) { + $form_state->setRedirect( + 'entity.oc_actor_case_relation.version_history', + ['oc_actor_case_relation' => $this->revision->id()] + ); + } + } + +} diff --git a/modules/opencase_cases/src/Form/OCActorCaseRelationRevisionRevertForm.php b/modules/opencase_cases/src/Form/OCActorCaseRelationRevisionRevertForm.php new file mode 100644 index 0000000..19c6a89 --- /dev/null +++ b/modules/opencase_cases/src/Form/OCActorCaseRelationRevisionRevertForm.php @@ -0,0 +1,137 @@ +oCActorCaseRelationStorage = $container->get('entity_type.manager')->getStorage('oc_actor_case_relation'); + $instance->dateFormatter = $container->get('date.formatter'); + return $instance; + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oc_actor_case_relation_revision_revert_confirm'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return $this->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_case_relation.version_history', ['oc_actor_case_relation' => $this->revision->id()]); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Revert'); + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return ''; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, $oc_actor_case_relation_revision = NULL) { + $this->revision = $this->OCActorCaseRelationStorage->loadRevision($oc_actor_case_relation_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 = $this->t('Copy of the revision from %date.', [ + '%date' => $this->dateFormatter->format($original_revision_timestamp), + ]); + $this->revision->save(); + + $this->logger('content')->notice('Actor-Case Relation: reverted %title revision %revision.', ['%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); + $this->messenger()->addMessage(t('Actor-Case Relation %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_case_relation.version_history', + ['oc_actor_case_relation' => $this->revision->id()] + ); + } + + /** + * Prepares a revision to be reverted. + * + * @param \Drupal\opencase_cases\Entity\OCActorCaseRelationInterface $revision + * The revision to be reverted. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return \Drupal\opencase_cases\Entity\OCActorCaseRelationInterface + * The prepared revision ready to be stored. + */ + protected function prepareRevertedRevision(OCActorCaseRelationInterface $revision, FormStateInterface $form_state) { + $revision->setNewRevision(); + $revision->isDefaultRevision(TRUE); + $revision->setRevisionCreationTime(REQUEST_TIME); + + return $revision; + } + +} diff --git a/modules/opencase_cases/src/Form/OCActorCaseRelationRevisionRevertTranslationForm.php b/modules/opencase_cases/src/Form/OCActorCaseRelationRevisionRevertTranslationForm.php new file mode 100644 index 0000000..0ab3b06 --- /dev/null +++ b/modules/opencase_cases/src/Form/OCActorCaseRelationRevisionRevertTranslationForm.php @@ -0,0 +1,97 @@ +languageManager = $container->get('language_manager'); + return $instance; + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'oc_actor_case_relation_revision_revert_translation_confirm'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return $this->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_case_relation_revision = NULL, $langcode = NULL) { + $this->langcode = $langcode; + $form = parent::buildForm($form, $form_state, $oc_actor_case_relation_revision); + + $form['revert_untranslated_fields'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Revert content shared among translations'), + '#default_value' => FALSE, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + protected function prepareRevertedRevision(OCActorCaseRelationInterface $revision, FormStateInterface $form_state) { + $revert_untranslated_fields = $form_state->getValue('revert_untranslated_fields'); + + /** @var \Drupal\opencase_cases\Entity\OCActorCaseRelationInterface $default_revision */ + $latest_revision = $this->OCActorCaseRelationStorage->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_cases/src/Form/OCActorCaseRelationSettingsForm.php b/modules/opencase_cases/src/Form/OCActorCaseRelationSettingsForm.php new file mode 100644 index 0000000..d650775 --- /dev/null +++ b/modules/opencase_cases/src/Form/OCActorCaseRelationSettingsForm.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_case_relation_type.collection'); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->entity->delete(); + + $this->messenger()->addMessage( + $this->t('content @type: deleted @label.', [ + '@type' => $this->entity->bundle(), + '@label' => $this->entity->label(), + ]) + ); + + $form_state->setRedirectUrl($this->getCancelUrl()); + } + +} diff --git a/modules/opencase_cases/src/Form/OCActorCaseRelationTypeForm.php b/modules/opencase_cases/src/Form/OCActorCaseRelationTypeForm.php new file mode 100644 index 0000000..694b49f --- /dev/null +++ b/modules/opencase_cases/src/Form/OCActorCaseRelationTypeForm.php @@ -0,0 +1,65 @@ +entity; + $form['label'] = [ + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $oc_actor_case_relation_type->label(), + '#description' => $this->t("Label for the Actor-Case Relation type."), + '#required' => TRUE, + ]; + + $form['id'] = [ + '#type' => 'machine_name', + '#default_value' => $oc_actor_case_relation_type->id(), + '#machine_name' => [ + 'exists' => '\Drupal\opencase_cases\Entity\OCActorCaseRelationType::load', + ], + '#disabled' => !$oc_actor_case_relation_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_case_relation_type = $this->entity; + $status = $oc_actor_case_relation_type->save(); + + switch ($status) { + case SAVED_NEW: + $this->messenger()->addMessage($this->t('Created the %label Actor-Case Relation type.', [ + '%label' => $oc_actor_case_relation_type->label(), + ])); + break; + + default: + $this->messenger()->addMessage($this->t('Saved the %label Actor-Case Relation type.', [ + '%label' => $oc_actor_case_relation_type->label(), + ])); + } + $form_state->setRedirectUrl($oc_actor_case_relation_type->toUrl('collection')); + } + +} diff --git a/modules/opencase_cases/src/OCActorCaseRelationAccessControlHandler.php b/modules/opencase_cases/src/OCActorCaseRelationAccessControlHandler.php new file mode 100644 index 0000000..9b08e9b --- /dev/null +++ b/modules/opencase_cases/src/OCActorCaseRelationAccessControlHandler.php @@ -0,0 +1,116 @@ +isPublished()) { + $permission = $this->checkOwn($entity, 'view unpublished', $account); + if (!empty($permission)) { + return AccessResult::allowed(); + } + + return AccessResult::allowedIfHasPermission($account, 'view unpublished actor-case relation entities'); + } + + $permission = $this->checkOwn($entity, $operation, $account); + if (!empty($permission)) { + return AccessResult::allowed(); + } + + return AccessResult::allowedIfHasPermission($account, 'view published actor-case relation entities'); + + case 'update': + + $permission = $this->checkOwn($entity, $operation, $account); + if (!empty($permission)) { + return AccessResult::allowed(); + } + return AccessResult::allowedIfHasPermission($account, 'edit actor-case relation entities'); + + case 'delete': + + $permission = $this->checkOwn($entity, $operation, $account); + if (!empty($permission)) { + return AccessResult::allowed(); + } + return AccessResult::allowedIfHasPermission($account, 'delete actor-case relation 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-case relation entities'); + } + + /** + * Test for given 'own' permission. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * @param $operation + * @param \Drupal\Core\Session\AccountInterface $account + * + * @return string|null + * The permission string indicating it's allowed. + */ + protected function checkOwn(EntityInterface $entity, $operation, AccountInterface $account) { + $status = $entity->isPublished(); + $uid = $entity->getOwnerId(); + + $is_own = $account->isAuthenticated() && $account->id() == $uid; + if (!$is_own) { + return; + } + + $bundle = $entity->bundle(); + + $ops = [ + 'create' => '%bundle add own %bundle entities', + 'view unpublished' => '%bundle view own unpublished %bundle entities', + 'view' => '%bundle view own entities', + 'update' => '%bundle edit own entities', + 'delete' => '%bundle delete own entities', + ]; + $permission = strtr($ops[$operation], ['%bundle' => $bundle]); + + if ($operation === 'view unpublished') { + if (!$status && $account->hasPermission($permission)) { + return $permission; + } + else { + return NULL; + } + } + if ($account->hasPermission($permission)) { + return $permission; + } + + return NULL; + } + +} diff --git a/modules/opencase_cases/src/OCActorCaseRelationHtmlRouteProvider.php b/modules/opencase_cases/src/OCActorCaseRelationHtmlRouteProvider.php new file mode 100644 index 0000000..30c6d96 --- /dev/null +++ b/modules/opencase_cases/src/OCActorCaseRelationHtmlRouteProvider.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_cases\Controller\OCActorCaseRelationController::revisionOverview', + ]) + ->setRequirement('_permission', 'view all actor-case relation 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_cases\Controller\OCActorCaseRelationController::revisionShow', + '_title_callback' => '\Drupal\opencase_cases\Controller\OCActorCaseRelationController::revisionPageTitle', + ]) + ->setRequirement('_permission', 'view all actor-case relation 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_cases\Form\OCActorCaseRelationRevisionRevertForm', + '_title' => 'Revert to earlier revision', + ]) + ->setRequirement('_permission', 'revert all actor-case relation 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_cases\Form\OCActorCaseRelationRevisionDeleteForm', + '_title' => 'Delete earlier revision', + ]) + ->setRequirement('_permission', 'delete all actor-case relation 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_cases\Form\OCActorCaseRelationRevisionRevertTranslationForm', + '_title' => 'Revert to earlier revision of a translation', + ]) + ->setRequirement('_permission', 'revert all actor-case relation 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_cases\Form\OCActorCaseRelationSettingsForm', + '_title' => "{$entity_type->getLabel()} settings", + ]) + ->setRequirement('_permission', $entity_type->getAdminPermission()) + ->setOption('_admin_route', TRUE); + + return $route; + } + } + +} diff --git a/modules/opencase_cases/src/OCActorCaseRelationListBuilder.php b/modules/opencase_cases/src/OCActorCaseRelationListBuilder.php new file mode 100644 index 0000000..476539a --- /dev/null +++ b/modules/opencase_cases/src/OCActorCaseRelationListBuilder.php @@ -0,0 +1,39 @@ +t('Actor-Case Relation ID'); + $header['name'] = $this->t('Name'); + return $header + parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + /* @var \Drupal\opencase_cases\Entity\OCActorCaseRelation $entity */ + $row['id'] = $entity->id(); + $row['name'] = Link::createFromRoute( + $entity->label(), + 'entity.oc_actor_case_relation.edit_form', + ['oc_actor_case_relation' => $entity->id()] + ); + return $row + parent::buildRow($entity); + } + +} diff --git a/modules/opencase_cases/src/OCActorCaseRelationPermissions.php b/modules/opencase_cases/src/OCActorCaseRelationPermissions.php new file mode 100644 index 0000000..9d6a11b --- /dev/null +++ b/modules/opencase_cases/src/OCActorCaseRelationPermissions.php @@ -0,0 +1,80 @@ +buildPermissions($type); + } + + return $perms; + } + + /** + * Returns a list of node permissions for a given node type. + * + * @param \Drupal\opencase_cases\Entity\OCActorCaseRelation $type + * The OCActorCaseRelation type. + * + * @return array + * An associative array of permission names and descriptions. + */ + protected function buildPermissions(OCActorCaseRelation $type) { + $type_id = $type->id(); + $type_params = ['%type_name' => $type->label()]; + + return [ + "$type_id create entities" => [ + 'title' => $this->t('Create new %type_name entities', $type_params), + ], + "$type_id edit own entities" => [ + 'title' => $this->t('Edit own %type_name entities', $type_params), + ], + "$type_id edit any entities" => [ + 'title' => $this->t('Edit any %type_name entities', $type_params), + ], + "$type_id delete own entities" => [ + 'title' => $this->t('Delete own %type_name entities', $type_params), + ], + "$type_id delete any entities" => [ + 'title' => $this->t('Delete any %type_name entities', $type_params), + ], + "$type_id view revisions" => [ + 'title' => $this->t('View %type_name revisions', $type_params), + 'description' => t('To view a revision, you also need permission to view the entity item.'), + ], + "$type_id revert revisions" => [ + 'title' => $this->t('Revert %type_name revisions', $type_params), + 'description' => t('To revert a revision, you also need permission to edit the entity item.'), + ], + "$type_id delete revisions" => [ + 'title' => $this->t('Delete %type_name revisions', $type_params), + 'description' => $this->t('To delete a revision, you also need permission to delete the entity item.'), + ], + ]; + } + +} diff --git a/modules/opencase_cases/src/OCActorCaseRelationStorage.php b/modules/opencase_cases/src/OCActorCaseRelationStorage.php new file mode 100644 index 0000000..2e424dc --- /dev/null +++ b/modules/opencase_cases/src/OCActorCaseRelationStorage.php @@ -0,0 +1,58 @@ +database->query( + 'SELECT vid FROM {oc_actor_case_relation_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_case_relation_field_revision} WHERE uid = :uid ORDER BY vid', + [':uid' => $account->id()] + )->fetchCol(); + } + + /** + * {@inheritdoc} + */ + public function countDefaultLanguageRevisions(OCActorCaseRelationInterface $entity) { + return $this->database->query('SELECT COUNT(*) FROM {oc_actor_case_relation_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_case_relation_revision') + ->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]) + ->condition('langcode', $language->getId()) + ->execute(); + } + +} diff --git a/modules/opencase_cases/src/OCActorCaseRelationStorageInterface.php b/modules/opencase_cases/src/OCActorCaseRelationStorageInterface.php new file mode 100644 index 0000000..2338b63 --- /dev/null +++ b/modules/opencase_cases/src/OCActorCaseRelationStorageInterface.php @@ -0,0 +1,61 @@ +t('Actor-Case Relation 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_cases/templates/oc-actor-case-relation-content-add-list.html.twig b/modules/opencase_cases/templates/oc-actor-case-relation-content-add-list.html.twig new file mode 100644 index 0000000..6b04ccf --- /dev/null +++ b/modules/opencase_cases/templates/oc-actor-case-relation-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_case_relation_content_add_list() + * + * @ingroup themeable + */ +#} +{% spaceless %} +
+ {% for type in types %} +
{{ type.link }}
+ {% endfor %} +
+{% endspaceless %} diff --git a/modules/opencase_cases/templates/oc_actor_case_relation.html.twig b/modules/opencase_cases/templates/oc_actor_case_relation.html.twig new file mode 100644 index 0000000..ce5033a --- /dev/null +++ b/modules/opencase_cases/templates/oc_actor_case_relation.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @file oc_actor_case_relation.html.twig + * Default theme implementation to present Actor-Case Relation data. + * + * This template is used when viewing Actor-Case Relation 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_case_relation() + * + * @ingroup themeable + */ +#} + + {% if content %} + {{- content -}} + {% endif %} +