Skip to content

Commit

Permalink
Fix the host token logic. It was to greedy. Fix silent failures. Our … (
Browse files Browse the repository at this point in the history
#271)

* Fix the host token logic. It was to greedy. Fix silent failures. Our presave try catch was capturing unintended errors in the data module.
* Fix code style issues.
  • Loading branch information
fmizzell authored and thierrydallacroce committed Dec 9, 2019
1 parent e4facd8 commit c30e947
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions modules/custom/dkan_data/dkan_data.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
}
}

Expand Down
14 changes: 6 additions & 8 deletions modules/custom/dkan_data/src/DataNodeLifeCycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Drupal\dkan_data\Exception;

/**
* Class DataNodeLifeCycleEntityValidationException.
*/
class DataNodeLifeCycleEntityValidationException extends \Exception {

}

0 comments on commit c30e947

Please sign in to comment.