diff --git a/modules/custom/dkan_data/dkan_data.module b/modules/custom/dkan_data/dkan_data.module index fa71554a4..b7b1dfad4 100644 --- a/modules/custom/dkan_data/dkan_data.module +++ b/modules/custom/dkan_data/dkan_data.module @@ -4,6 +4,7 @@ * @file */ +use Drupal\dkan_data\Exception\DataNodeLifeCycleEntityValidationException; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\dkan_common\UrlHostTokenResolver; @@ -75,8 +76,7 @@ function dkan_data_entity_presave(EntityInterface $entity) { try { (new DataNodeLifeCycle($entity))->presave(); } - catch (\Exception $e) { - // Nothing to do, this entity is not a data node. + catch (DataNodeLifeCycleEntityValidationException $e) { } } diff --git a/modules/custom/dkan_data/src/DataNodeLifeCycle.php b/modules/custom/dkan_data/src/DataNodeLifeCycle.php index 02344c376..1bac973ef 100644 --- a/modules/custom/dkan_data/src/DataNodeLifeCycle.php +++ b/modules/custom/dkan_data/src/DataNodeLifeCycle.php @@ -4,6 +4,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\dkan_common\UrlHostTokenResolver; +use Drupal\dkan_data\Exception\DataNodeLifeCycleEntityValidationException; use Drupal\node\Entity\Node; /** @@ -58,9 +59,6 @@ private function datasetPresave() { $title = isset($metadata->title) ? $metadata->title : $metadata->name; $entity->setTitle($title); - if (empty($entity->field_data_type->value)) { - $entity->field_data_type->value = "dataset"; - } // If there is no uuid add one. if (!isset($metadata->identifier)) { @@ -90,11 +88,11 @@ private function datasetPresave() { */ private function distributionPresave() { $metadata = $this->getMetaData(); - $host = \Drupal::request()->getSchemeAndHttpHost(); + $host = \Drupal::request()->getHost(); if (isset($metadata->data->downloadURL)) { $newUrl = $metadata->data->downloadURL; - if (substr_count($newUrl, $host) > 0) { - $parsedUrl = parse_url($newUrl); + $parsedUrl = parse_url($newUrl); + if ($parsedUrl['host'] == $host) { $parsedUrl['host'] = UrlHostTokenResolver::TOKEN; $metadata->data->downloadURL = $this->unparseUrl($parsedUrl); $this->setMetadata($metadata); @@ -126,11 +124,11 @@ private function setMetadata($metadata) { */ private function validate(EntityInterface $entity) { if (!($entity instanceof Node)) { - throw new \Exception("We only work with nodes."); + throw new DataNodeLifeCycleEntityValidationException("We only work with nodes."); } if ($entity->bundle() != "data") { - throw new \Exception("We only work with data nodes."); + throw new DataNodeLifeCycleEntityValidationException("We only work with data nodes."); } } diff --git a/modules/custom/dkan_data/src/Exception/DataNodeLifeCycleEntityValidationException.php b/modules/custom/dkan_data/src/Exception/DataNodeLifeCycleEntityValidationException.php new file mode 100644 index 000000000..974f64b45 --- /dev/null +++ b/modules/custom/dkan_data/src/Exception/DataNodeLifeCycleEntityValidationException.php @@ -0,0 +1,10 @@ +