Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #65 from ec-europa/drupal-8.6.x
Browse files Browse the repository at this point in the history
Run on 8.6.x
  • Loading branch information
claudiu-cristea authored Sep 6, 2018
2 parents 5590f48 + a2adf55 commit 400c24d
Show file tree
Hide file tree
Showing 22 changed files with 385 additions and 128 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ matrix:
exclude:
- php: 7.2
env: TEST=PHP_CodeSniffer
# Todo: remove this section once we have a passing test suite on 8.6.x.
# Ref. https://github.com/ec-europa/rdf_entity/issues/58
allow_failures:
- env: TEST=8.6.x

before_install:
- composer --verbose self-update
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ settings.php file.
'prefix' => '',
'host' => '127.0.0.1',
'port' => '8890',
'namespace' => 'Drupal\\rdf_entity\\Database\\Driver\\sparql',
'namespace' => 'Drupal\\Driver\\Database\\sparql',
'driver' => 'sparql',
// Optional. This is actually the endpoint path. If omitted, 'sparql' will
// be used.
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@
"require": {
"php": ">=7.1",
"easyrdf/easyrdf": "0.10.0-alpha.1 as 0.9.2"
},
"autoload": {
"classmap": [
"driver/sparql/Connection.php"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

declare(strict_types = 1);

namespace Drupal\rdf_entity\Database\Driver\sparql;
namespace Drupal\Driver\Database\sparql;

use Drupal\Core\Database\Log;
use Drupal\rdf_entity\Database\Driver\sparql\ConnectionInterface;
use Drupal\rdf_entity\Database\Driver\sparql\StatementStub;
use Drupal\rdf_entity\Exception\SparqlQueryException;
use EasyRdf\Http\Exception as EasyRdfException;
use EasyRdf\Sparql\Client;
Expand Down Expand Up @@ -265,7 +267,7 @@ protected function log(string $query, array $args, float $duration): void {
* @see \Drupal\Core\Database\Log
* @see \Drupal\Core\Database\StatementInterface
* @see \Drupal\rdf_entity\Database\Driver\sparql\StatementStub
* @see \Drupal\rdf_entity\Database\Driver\sparql\Connection::log()
* @see \Drupal\Driver\Database\sparql\Connection::log()
*/
protected function getStatement(): StatementStub {
if (!isset($this->statementStub)) {
Expand All @@ -274,6 +276,118 @@ protected function getStatement(): StatementStub {
return $this->statementStub;
}

/**
* Creates an array of database connection options from a URL.
*
* @param string $url
* The URL.
* @param string $root
* The root directory of the Drupal installation. Some database drivers,
* like for example SQLite, need this information.
*
* @return array
* The connection options.
*
* @throws \InvalidArgumentException
* Exception thrown when the provided URL does not meet the minimum
* requirements.
*
* @see \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()
*
* @internal
* This method should not be called. Use
* \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo() instead.
*/
public static function createConnectionOptionsFromUrl($url, $root) {
$url_components = parse_url($url);
if (!isset($url_components['scheme'], $url_components['host'], $url_components['path'])) {
throw new \InvalidArgumentException('Minimum requirement: driver://host/database');
}

$url_components += [
'user' => '',
'pass' => '',
'fragment' => '',
];

// Remove leading slash from the URL path.
if ($url_components['path'][0] === '/') {
$url_components['path'] = substr($url_components['path'], 1);
}

// Use reflection to get the namespace of the class being called.
$reflector = new \ReflectionClass(get_called_class());

$database = [
'driver' => $url_components['scheme'],
'username' => $url_components['user'],
'password' => $url_components['pass'],
'host' => $url_components['host'],
'database' => $url_components['path'],
'namespace' => $reflector->getNamespaceName(),
];

if (isset($url_components['port'])) {
$database['port'] = $url_components['port'];
}

if (!empty($url_components['fragment'])) {
$database['prefix']['default'] = $url_components['fragment'];
}

return $database;
}

/**
* Creates a URL from an array of database connection options.
*
* @param array $connection_options
* The array of connection options for a database connection.
*
* @return string
* The connection info as a URL.
*
* @throws \InvalidArgumentException
* Exception thrown when the provided array of connection options does not
* meet the minimum requirements.
*
* @see \Drupal\Core\Database\Database::getConnectionInfoAsUrl()
*
* @internal
* This method should not be called. Use
* \Drupal\Core\Database\Database::getConnectionInfoAsUrl() instead.
*/
public static function createUrlFromConnectionOptions(array $connection_options) {
if (!isset($connection_options['driver'], $connection_options['database'])) {
throw new \InvalidArgumentException("As a minimum, the connection options array must contain at least the 'driver' and 'database' keys");
}

$user = '';
if (isset($connection_options['username'])) {
$user = $connection_options['username'];
if (isset($connection_options['password'])) {
$user .= ':' . $connection_options['password'];
}
$user .= '@';
}

$host = empty($connection_options['host']) ? 'localhost' : $connection_options['host'];

$db_url = $connection_options['driver'] . '://' . $user . $host;

if (isset($connection_options['port'])) {
$db_url .= ':' . $connection_options['port'];
}

$db_url .= '/' . $connection_options['database'];

if (isset($connection_options['prefix']['default']) && $connection_options['prefix']['default'] !== '') {
$db_url .= '#' . $connection_options['prefix']['default'];
}

return $db_url;
}

}

/**
Expand Down
3 changes: 1 addition & 2 deletions modules/rdf_taxonomy/rdf_taxonomy.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ type: module
description: 'Replace the taxonomy sql storage backend with rdf storage.'
package: Custom
core: 8.x

# These modules are required by the tests, must be available at bootstrap time
dependencies:
- rdf_entity
- taxonomy
17 changes: 8 additions & 9 deletions modules/rdf_taxonomy/rdf_taxonomy.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\rdf_taxonomy\Entity\RdfTerm;
use Drupal\rdf_taxonomy\RdfTaxonomyTermListBuilder;
use Drupal\taxonomy\TermInterface;
use Drupal\rdf_taxonomy\TermRdfStorage;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\VocabularyInterface;

/**
Expand All @@ -22,7 +24,8 @@ use Drupal\taxonomy\VocabularyInterface;
* implementation of the Drupal\Core\Entity\EntityStorageInterface interface.
*/
function rdf_taxonomy_entity_type_alter(array &$entity_types) {
$entity_types['taxonomy_term']->setStorageClass('Drupal\rdf_taxonomy\TermRdfStorage');
$entity_types['taxonomy_term']->setClass(RdfTerm::class);
$entity_types['taxonomy_term']->setStorageClass(TermRdfStorage::class);
$entity_types['taxonomy_term']->setHandlerClass('views_data', NULL);
$entity_types['taxonomy_term']->setHandlerClass('list_builder', RdfTaxonomyTermListBuilder::class);
}
Expand Down Expand Up @@ -83,14 +86,10 @@ function rdf_taxonomy_form_taxonomy_term_form_alter(&$form, FormStateInterface $
$term = $form_state->getFormObject()->getEntity();
$form['langcode']['widget'][0]['value']['#default_value'] = $term->language()->getId();

// Convert the root option value from 0 (as integer) to '' (empty string).
$root = array_shift($form['relations']['parent']['#options']);
$form['relations']['parent']['#options'] = ['' => $root] + $form['relations']['parent']['#options'];
if (($index = array_search(0, $form['relations']['parent']['#default_value'], TRUE)) !== FALSE) {
$form['relations']['parent']['#default_value'][$index] = '';
}
// RDF Taxonomy doesn't support multiple parents yet.
$parent = reset($form['relations']['parent']['#default_value']);
$form['relations']['parent']['#default_value'] = $parent;

// Visual enhancements.
$form['relations']['#open'] = TRUE;
$form['relations']['parent']['#size'] = 16;
}
4 changes: 4 additions & 0 deletions modules/rdf_taxonomy/rdf_taxonomy.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ services:
class: \Drupal\rdf_taxonomy\RdfTaxonomyConfigOverrides
tags:
- {name: config.factory.override, priority: 5}
rdf_taxonomy.outbound_value.term_parent.subscriber:
class: Drupal\rdf_taxonomy\EventSubscriber\OutboundTermParentSubscriber
tags:
- { name: event_subscriber }
41 changes: 41 additions & 0 deletions modules/rdf_taxonomy/src/Entity/RdfTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Drupal\rdf_taxonomy\Entity;

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\taxonomy\Entity\Term;

/**
* Provides an alternative entity class for 'taxonomy_term'.
*/
class RdfTerm extends Term {

/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$base_fields = parent::baseFieldDefinitions($entity_type);
// Support also Drupal 8.5.x.
if (isset($base_fields['status'])) {
$base_fields['status']->setCustomStorage(TRUE);
}
return $base_fields;
}

/**
* {@inheritdoc}
*/
public function isPublished() {
// The RDF taxonomy term doesn't support the published flag.
return TRUE;
}

/**
* {@inheritdoc}
*/
public function setPublished($published = NULL) {
// The RDF taxonomy term doesn't support the published flag.
return $this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Drupal\rdf_taxonomy\EventSubscriber;

use Drupal\rdf_entity\Event\OutboundValueEvent;
use Drupal\rdf_entity\Event\RdfEntityEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Massages outbound term parent value.
*/
class OutboundTermParentSubscriber implements EventSubscriberInterface {

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
RdfEntityEvents::OUTBOUND_VALUE => 'fixParentTermId',
];
}

/**
* Fixes the term parent ID.
*
* Drupal core uses taxonomy terms with numeric IDs. If case, we convert the
* term ID, from a numeric type to string.
*
* @param \Drupal\rdf_entity\Event\OutboundValueEvent $event
* The outbound value event.
*/
public function fixParentTermId(OutboundValueEvent $event) {
if ($event->getEntityTypeId() === 'taxonomy_term' && $event->getField() === 'parent') {
$event->setValue((string) $event->getValue());
}
}

}
Loading

0 comments on commit 400c24d

Please sign in to comment.