This module makes it possible to enrich content with data from an external SPARQL endpoint, based on fields or any other data available. Since it is entirely loaded client side (with JSONP), it is very lightweight, but it can still be very flexible.

It also has options for using the Drupal instance as a proxy, if the endpoint you want does not return JSON with a callback.

What is this SPARQL stuff?

From Wikipedia:
"SPARQL (pronounced "sparkle", a recursive acronym for SPARQL Protocol and RDF Query Language) is an RDF query language, that is, a query language for databases, able to retrieve and manipulate data stored in Resource Description Framework format."

One of the cool things about SPARQL is that it exists a SPARQL endpoint that includes all of Wikipedia in a structured, semantic way. It is called DBPedia. Basically that means you can easily generate blocks like "Related in Wikipedia" with this module. SPARQL queries can give you all kinds of useful and non-useful information like "all football players playing a certain position that were born in the year that the current node references". All because of the structure of the data that the semantic web provides. But you have to have some basic knowledge of SPARQL and SPARQL endpoints to start using this module. Recommended reading follows:
http://en.wikipedia.org/wiki/SPARQL
http://semanticweb.org/wiki/SPARQL_endpoint
http://jena.sourceforge.net/ARQ/Tutorial/
https://dbpedia.org/About

Installation

Drupal 8

Download and enable this module with your preferred method

Drupal 7 (version 1)

  • Download and extract this module and the Libraries module into your module directory. And then enable them like any other module.
  • If you want to load the SPARQL queries client side (and you would want to, that's the point of this module), you have to download the jQuery JSONP plugin. This module has been tested with >=v. 2.2.0. Put the JS file in a folder called "jquery.jsonp" in sites/all/libraries and name the file either jquery-jsonp.min.js or jquery-jsonp.js
  • Visit the status report (admin/reports/status) to confirm the plugin was installed correctly
  • You are good to go!

Configuration and a simple walkthrough

Drupal 8

  • Navigate to the Block layout settings, located under Structure->block layout
  • Place a block in the region you want it. For example by clicking the "place block" button next to a region (for example sidebar).
  • Now choose the block type "Sparql block" and click "place block"
  • Choose an input method. For this example we are going to use token value. Choose that.
  • Enter a token value. For this example we will use [node:title]. This will use the title of the node as an input for the SPARQL query
  • For the endpoint, use https://dbpedia.org/sparql
  • For the query, one can use the placeholder {{ value }} to dynamically build the query based on the input from the (in this case) node title replacement. Our query will look like this:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia2: <http://dbpedia.org/ontology/>
 
SELECT *
WHERE {
<{{ value }}> dbpedia2:abstract ?abs .
<{{ value }}> rdfs:label ?label .
filter langMatches( lang(?abs), 'en') 
filter langMatches( lang(?label), 'en') 
}
  • Enter something for empty value and error message, if you want.
  • Now, for the data template, we can choose what to do with each row of result. The rows will have the same variables available as you define in the query. So in our case we can do something like this:
<h2>Label is {{ label }}</h2>
<p>{{ abs }}</p>

Lastly, save the block and optionally display it only on pages you want. For example only on a specific content type

As a test you can now try to add a node with the title http://dbpedia.org/resource/SPARQL and you should get results in the sidebar (provided you followed the instructions correctly

Drupal 7 (version 1)

  • Navigate to the settings for the module, located under configuration->search and metadata (or just admin/config/search/jsonp_sparql).
  • Enter a number of blocks you want available
  • Navigate to the Blocks admin section (strucure->blocks or /admin/structure/blocks) and edit the JSONP SPARQL block you want to use
  • Enter an input value, either in the form of a machine-readable name of your field in the correct place (i.e field_text), Token value (token helper available through the Token module) or PHP code (if you have the PHP filter available).
  • Now in the "SPARQL settings" tab enter a sparql query to use, where you can replace parts of the query with "[1]" as the placeholder for your dynamic value (your field). Here is one example for the DBPedia endpoint:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia2: <http://dbpedia.org/ontology/>
 
SELECT *
WHERE {
<[1]> dbpedia2:abstract ?abs .
<[1]> rdfs:label ?label .
filter langMatches( lang(?abs), 'en') 
filter langMatches( lang(?label), 'en') 
}
  • This will assume you are using the dynamic value to input an URI, and retrieve the english abstract of the corresponding Wikipedia article.
  • Enter "https://dbpedia.org/sparql/?" as the endpoint. Remember the question mark!
  • Press the test button to test your query. You should now get prompted for a value for the dynamic placeholder. Enter for example "http://dbpedia.org/resource/SPARQL".
  • You should now get some good news back. In the tab for presentation, things are now filled out for you. You would most likely want to change this, but save this for now.
  • You can now enable the JSONP SPARQL block. Place it in a region and make it visible only on the content type(s) that has the field you are using (if using field value).
  • If you are using field value, create some content in that content type, and place a DBPedia URI in the field you are using. You should now see data from DBPedia in the block you created.
  • Customize and tweak settings to make the page awesome!

If you want to use PHP for input value for your query just enable the (core) php module.
If you want to use the awesome token replacement tree, enable the Token module

This module was made in a project funded by Vestlandsforskning, Sogn og Fjordane fylkeskommune and Norsk kulturråd.

Supporting organizations: 

Project information

Releases