Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the host token logic. It was to greedy. Fix silent failures. Our … #271

Merged
merged 2 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {

}