Skip to content

Commit

Permalink
Merge pull request #177 from City-of-Helsinki/UHF-10352
Browse files Browse the repository at this point in the history
UHF-10352
  • Loading branch information
rpnykanen authored Jan 10, 2025
2 parents 004a73f + c1df336 commit f7c04cd
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Entity/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
* },
* links = {
* "edit-form" = "/admin/content/integrations/tpr-service-channel/{tpr_service_channel}/edit",
* "collection" = "/admin/content/integrations/tpr-service-channel"
* "collection" = "/admin/content/integrations/tpr-service-channel",
* },
* field_ui_base_route = "tpr_service_channel.settings"
* )
Expand Down
5 changes: 4 additions & 1 deletion src/Entity/ErrandService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* "translation" = "Drupal\helfi_tpr\Entity\TranslationHandler",
* "form" = {
* "default" = "Drupal\Core\Entity\ContentEntityForm",
* "delete" = "Drupal\helfi_tpr\Entity\Form\TprDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\helfi_api_base\Entity\Routing\EntityRouteProvider",
Expand Down Expand Up @@ -51,8 +52,10 @@
* "revision_log_message" = "revision_log"
* },
* links = {
* "canonical" = "/tpr-errand-service/{tpr_errand_service}",
* "edit-form" = "/admin/content/integrations/tpr-errand-service/{tpr_errand_service}/edit",
* "collection" = "/admin/content/integrations/tpr-errand-service"
* "collection" = "/admin/content/integrations/tpr-errand-service",
* "delete-form" = "/admin/content/integrations/tpr-errand-service/{tpr_errand_service}/delete",
* },
* field_ui_base_route = "tpr_errand_service.settings"
* )
Expand Down
133 changes: 133 additions & 0 deletions src/Entity/Form/TprDeleteForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_tpr\Entity\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Utility\Error;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;

/**
* Tpr-entity delete form.
*/
final class TprDeleteForm extends ContentEntityDeleteForm {

use AutowireTrait;

/**
* The constructor.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info interface.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time interface.
* @param \GuzzleHttp\ClientInterface $http_client
* The time interface.
* @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_manager
* The migration plugin manager.
*/
public function __construct(
EntityRepositoryInterface $entity_repository,
EntityTypeBundleInfoInterface $entity_type_bundle_info,
TimeInterface $time,
private readonly ClientInterface $http_client,
private readonly MigrationPluginManagerInterface $migration_manager,
) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->entityExists()) {
$this->messenger()->addWarning(
$this->t('Cannot delete TPR-entity which still exists in the API')
);
return;
}

/** @var \Drupal\helfi_tpr\Entity\TprEntityBase $entity */
$entity = $this->entity;
$message = $this->getDeletionMessage();

// Make sure that deleting a translation does not delete the whole entity.
if (!$entity->isDefaultTranslation()) {
$untranslated_entity = $entity->getUntranslated();
$untranslated_entity->removeTranslation($entity->language()->getId());
$untranslated_entity->save();
$form_state->setRedirectUrl($untranslated_entity->toUrl('canonical'));
}
else {
$entity->delete(TRUE);
$form_state->setRedirectUrl($this->getRedirectUrl());
}

$this->messenger()->addStatus($message);
$this->logDeletionMessage();
}

/**
* Check if the entity being deleted exists in the API.
*
* @return bool
* The entity exists in api.
*/
private function entityExists(): bool {
$entityTypeId = $this->entity->getEntityTypeId();

$urlMap = [
'tpr_errand_service' => 'canonical_url',
'tpr_ontology_word_details' => 'details_url',
'tpr_service' => 'canonical_url',
'tpr_unit' => 'url',
];
$key = $urlMap[$entityTypeId];
$url = $this->migration_manager->getDefinition($entityTypeId)['source'][$key];

$request_url = sprintf(
'%s/%s%s',
rtrim(strtok($url, '?'), '/'),
$this->entity->id(),
"?language={$this->entity->language()->getId()}"
);

try {
$response = $this->http_client
->request('GET', $request_url);
$data = $response->getBody()
->getContents();
}
catch (ClientException $e) {
if ($e->getResponse()->getStatusCode() === 404) {
return FALSE;
}

Error::logException($this->getLogger('helfi_tpr'), $e);
// Prevent from deleting.
return TRUE;
}

if (!json_validate($data) || $data === 'null') {
return FALSE;
}

$api_data = json_decode($data, TRUE);
if (isset($api_data['id']) && $api_data['id'] == $this->entity->id()) {
return TRUE;
}

return FALSE;
}

}
2 changes: 2 additions & 0 deletions src/Entity/OntologyWordDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* "translation" = "Drupal\helfi_tpr\Entity\TranslationHandler",
* "form" = {
* "default" = "Drupal\helfi_tpr\Entity\Form\ContentEntityForm",
* "delete" = "Drupal\helfi_tpr\Entity\Form\TprDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\helfi_api_base\Entity\Routing\EntityRouteProvider",
Expand Down Expand Up @@ -63,6 +64,7 @@
* "edit-form" = "/admin/content/integrations/tpr-ontology-word-details/{tpr_ontology_word_details}/edit",
* "collection" = "/admin/content/integrations/tpr-ontology-word-details",
* "version-history" = "/admin/content/integrations/tpr-ontology-word-details/{tpr_ontology_word_details}/revisions",
* "delete-form" = "/admin/content/integrations/tpr-ontology-word-details/{tpr_ontology_word_details}/delete",
* },
* field_ui_base_route = "tpr_ontology_word_details.settings"
* )
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* "translation" = "Drupal\helfi_tpr\Entity\TranslationHandler",
* "form" = {
* "default" = "Drupal\helfi_tpr\Entity\Form\ContentEntityForm",
* "delete" = "Drupal\helfi_tpr\Entity\Form\TprDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\helfi_api_base\Entity\Routing\EntityRouteProvider",
Expand Down Expand Up @@ -66,6 +67,7 @@
* "version-history" = "/admin/content/integrations/tpr-service/{tpr_service}/revisions",
* "revision" = "/tpr-service/{tpr_service}/revisions/{tpr_service_revision}/view",
* "revision-revert-language-form" = "/admin/content/integrations/tpr-service/{tpr_service}/revisions/{tpr_service_revision}/revert/{langcode}",
* "delete-form" = "/admin/content/integrations/tpr-service/{tpr_service}/delete",
* },
* field_ui_base_route = "tpr_service.settings"
* )
Expand Down
20 changes: 20 additions & 0 deletions src/Entity/TprEntityBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,19 @@ abstract class TprEntityBase extends RemoteEntityBase implements RevisionableInt
* {@inheritdoc}
*/
public function label() {
// @todo Fix after core issue has been resolved.
// https://www.drupal.org/project/drupal/issues/3423205
// Getting name_override after deletion causes exception
// because field does not exist when drupal is trying to log the deletion.
if (!isset($this->translations[$this->activeLangcode]['status'])) {
return parent::label();
}

// Use overridden name field as default label when possible.
if (!$this->get('name_override')->isEmpty()) {
return $this->get('name_override')->value;
}

return parent::label();
}

Expand Down Expand Up @@ -180,4 +189,15 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return $fields;
}

/**
* {@inheritdoc}
*/
public function delete(bool $forceDelete = FALSE) : void {
// You should only be able to delete via delete form.
if (!$forceDelete) {
return;
}
parent::delete($forceDelete);
}

}
2 changes: 2 additions & 0 deletions src/Entity/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* "translation" = "Drupal\helfi_tpr\Entity\TranslationHandler",
* "form" = {
* "default" = "Drupal\helfi_tpr\Entity\Form\ContentEntityForm",
* "delete" = "Drupal\helfi_tpr\Entity\Form\TprDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\helfi_api_base\Entity\Routing\EntityRouteProvider",
Expand Down Expand Up @@ -69,6 +70,7 @@
* "version-history" = "/admin/content/integrations/tpr-unit/{tpr_unit}/revisions",
* "revision" = "/tpr-unit/{tpr_unit}/revisions/{tpr_unit_revision}/view",
* "revision-revert-language-form" = "/admin/content/integrations/tpr-unit/{tpr_unit}/revisions/{tpr_unit_revision}/revert/{langcode}",
* "delete-form" = "/admin/content/integrations/tpr-unit/{tpr_unit}/delete",
* },
* field_ui_base_route = "tpr_unit.settings"
* )
Expand Down
3 changes: 0 additions & 3 deletions tests/src/Kernel/ErrandServiceEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ protected function getEntity(int $id) : ErrandService {
public function testEntityDeletion() : void {
$entity = $this->getEntity(1);

// Test that the entity is not deleted.
// See Drupal\helfi_tpr\Entity\TprEntityBase::delete() for more
// information.
$entity->delete();
$this->assertNotEquals(NULL, ErrandService::load(1));
}
Expand Down
3 changes: 0 additions & 3 deletions tests/src/Kernel/OntologyWordDetailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ protected function getEntity(string $id) : OntologyWordDetails {
public function testEntityDeletion() : void {
$entity = $this->getEntity('1_1');

// Test that the entity is not deleted.
// See Drupal\helfi_tpr\Entity\TprEntityBase::delete() for more
// information.
$entity->delete();
$this->assertNotEquals(NULL, OntologyWordDetails::load('1_1'));
}
Expand Down
3 changes: 0 additions & 3 deletions tests/src/Kernel/ServiceEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ protected function getEntity(int $id) : Service {
public function testEntityDeletion() : void {
$entity = $this->getEntity(1);

// Test that the entity is not deleted.
// See Drupal\helfi_tpr\Entity\TprEntityBase::delete() for more
// information.
$entity->delete();
$this->assertNotEquals(NULL, Service::load(1));
}
Expand Down
3 changes: 0 additions & 3 deletions tests/src/Kernel/UnitEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ protected function getEntity(int $id) : Unit {
public function testEntityDeletion() : void {
$entity = $this->getEntity(1);

// Test that the entity is not deleted.
// See Drupal\helfi_tpr\Entity\TprEntityBase::delete() for more
// information.
$entity->delete();
$this->assertNotEquals(NULL, Unit::load(1));
}
Expand Down
4 changes: 4 additions & 0 deletions translations/fi.po
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,7 @@ msgstr "Nostot"
msgctxt "TPR Unit field label"
msgid "Contact details of daycare centre groups"
msgstr "Päiväkotiryhmien yhteystiedot"

msgctxt "TPR entity delete error "
msgid "Cannot delete TPR-entity which still exists in the API"
msgstr "Rajapinnasta löytyvää TPR-entiteettiä ei voida poistaa"

0 comments on commit f7c04cd

Please sign in to comment.