Commit 9e5def44 authored by catch's avatar catch

Issue #1778122 by scor, linclark, effulgentsia: Fixed Enable modules to inject...

Issue #1778122 by scor, linclark, effulgentsia: Fixed Enable modules to inject attributes into field formatters, so that RDF attributes get output on the appropriate elements.
parent 8980243b
......@@ -31,6 +31,24 @@ public function __construct($entity_type) {
*/
public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) {
field_attach_prepare_view($this->entityType, $entities, $displays, $langcode);
// Initialize the field item attributes for the fields set to be displayed.
foreach ($entities as $entity) {
// The entity can include fields that aren't displayed, and the display
// can include components that aren't fields, so we want to iterate the
// intersection of $entity->getProperties() and $display->getComponents().
// However, the entity can have many more fields than are displayed, so we
// avoid the cost of calling $entity->getProperties() by iterating the
// intersection as follows.
foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) {
if ($entity->getPropertyDefinition($name)) {
foreach ($entity->get($name) as $item) {
$item->_attributes = array();
}
}
}
}
module_invoke_all('entity_prepare_view', $this->entityType, $entities, $displays, $view_mode);
foreach ($entities as $entity) {
......
......@@ -865,8 +865,16 @@ function template_preprocess_field(&$variables, $hook) {
$variables['attributes'] = isset($variables['attributes']) ? new Attribute($variables['attributes']) : clone $default_attributes;
$variables['title_attributes'] = isset($variables['title_attributes']) ? new Attribute($variables['title_attributes']) : clone($default_attributes);
$variables['content_attributes'] = isset($variables['content_attributes']) ? new Attribute($variables['content_attributes']) : clone($default_attributes);
// Modules (e.g., rdf.module) can add field item attributes (to
// $item->_attributes) within hook_entity_prepare_view(). Some field
// formatters move those attributes into some nested formatter-specific
// element in order have them rendered on the desired HTML element (e.g., on
// the <a> element of a field item being rendered as a link). Other field
// formatters leave them within $element['#items'][$delta]['_attributes'] to
// be rendered on the item wrappers provided by theme_field().
foreach ($variables['items'] as $delta => $item) {
$variables['item_attributes'][$delta] = isset($variables['item_attributes'][$delta]) ? new Attribute($variables['item_attributes'][$delta]) : clone($default_attributes);
$variables['item_attributes'][$delta] = !empty($element['#items'][$delta]['_attributes']) ? new Attribute($element['#items'][$delta]['_attributes']) : clone($default_attributes);
}
}
......
......@@ -568,7 +568,7 @@ function file_theme() {
return array(
// file.module.
'file_link' => array(
'variables' => array('file' => NULL, 'icon_directory' => NULL, 'description' => NULL),
'variables' => array('file' => NULL, 'icon_directory' => NULL, 'description' => NULL, 'attributes' => array()),
),
'file_icon' => array(
'variables' => array('file' => NULL, 'icon_directory' => NULL),
......@@ -1558,19 +1558,19 @@ function file_managed_file_pre_render($element) {
* files. Defaults to the value of the "icon.directory"
* variable.
* - description: A description to be displayed instead of the filename.
* - attributes: An associative array of attributes to be placed in the a tag.
*
* @ingroup themeable
*/
function theme_file_link($variables) {
$file = $variables['file'];
$options = array(
'attributes' => $variables['attributes'],
);
// Set options as per anchor format described at
// http://microformats.org/wiki/file-format-examples
$options = array(
'attributes' => array(
'type' => $file->getMimeType() . '; length=' . $file->getSize(),
),
);
$options['attributes']['type'] = $file->getMimeType() . '; length=' . $file->getSize();
// Use the description as the link text if available.
if (empty($variables['description'])) {
......
......@@ -37,6 +37,14 @@ public function viewElements(FieldInterface $items) {
'#file' => $item->entity,
'#description' => $item->description,
);
// Pass field item attributes to the theme function.
if (isset($item->_attributes)) {
$elements[$delta] += array('#attributes' => array());
$elements[$delta]['#attributes'] += $item->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and should not be rendered in the field template.
unset($item->_attributes);
}
}
}
......
......@@ -119,6 +119,14 @@ public function viewElements(FieldInterface $items) {
'#image_style' => $image_style_setting,
'#path' => isset($uri) ? $uri : '',
);
// Pass field item attributes to the theme function.
if (isset($item->_attributes)) {
$elements[$delta]['#item'] += array('attributes' => array());
$elements[$delta]['#item']['attributes'] += $item->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and should not be rendered in the field template.
unset($item->_attributes);
}
}
}
......
<?php
/**
* @file
* Contains \Drupal\rdf\Tests\Field\FieldRdfaDatatypeCallbackTest.
*/
namespace Drupal\rdf\Tests\Field;
/**
* Tests the RDFa output of a text field formatter with a datatype callback.
*/
class FieldRdfaDatatypeCallbackTest extends FieldRdfaTestBase {
/**
* {@inheritdoc}
*/
protected $fieldType = 'text';
/**
* {@inheritdoc}
*/
public static $modules = array('text', 'filter');
public static function getInfo() {
return array(
'name' => 'Field formatter: datatype callback',
'description' => 'Tests RDFa output for field formatters with a datatype callback.',
'group' => 'RDF',
);
}
public function setUp() {
parent::setUp();
$this->createTestField();
// Add the mapping.
$mapping = rdf_get_mapping('entity_test_render', 'entity_test_render');
$mapping->setFieldMapping($this->fieldName, array(
'properties' => array('schema:interactionCount'),
'datatype_callback' => array(
'callable' => 'Drupal\rdf\Tests\Field\TestDataConverter::convertFoo',
),
))->save();
// Set up test values.
$this->test_value = $this->randomName();
$this->entity = entity_create('entity_test_render', array());
$this->entity->{$this->fieldName}->value = $this->test_value;
$this->entity->save();
$this->uri = $this->getAbsoluteUri($this->entity);
}
/**
* Tests the default formatter.
*/
public function testDefaultFormatter() {
// Expected value is the output of the datatype callback, not the raw value.
$this->assertFormatterRdfa('text_default', 'http://schema.org/interactionCount', 'foo' . $this->test_value);
}
}
......@@ -58,9 +58,14 @@ abstract class FieldRdfaTestBase extends FieldUnitTestBase {
* The object's type, either 'uri' or 'literal'.
*/
protected function assertFormatterRdfa($formatter, $property, $value, $object_type = 'literal') {
$build = field_view_field($this->entity, $this->fieldName, array('type' => $formatter));
$rendered = "<div about='$this->uri'>" . drupal_render($build) . '</div>';
$graph = new \EasyRdf_Graph($this->uri, $rendered, 'rdfa');
// The field formatter will be rendered inside the entity. Set the field
// formatter in the entity display options before rendering the entity.
entity_get_display('entity_test_render', 'entity_test_render', 'default')
->setComponent($this->fieldName, array('type' => $formatter))
->save();
$build = entity_view($this->entity, 'default');
$output = drupal_render($build);
$graph = new \EasyRdf_Graph($this->uri, $output, 'rdfa');
$expected_value = array(
'type' => $object_type,
......@@ -74,13 +79,14 @@ protected function assertFormatterRdfa($formatter, $property, $value, $object_ty
*/
protected function createTestField() {
entity_create('field_entity', array(
'field_name' => $this->fieldName,
'name' => $this->fieldName,
'entity_type' => 'entity_test_render',
'type' => $this->fieldType,
))->save();
entity_create('field_instance', array(
'entity_type' => 'entity_test',
'entity_type' => 'entity_test_render',
'field_name' => $this->fieldName,
'bundle' => 'entity_test',
'bundle' => 'entity_test_render',
))->save();
}
......@@ -97,4 +103,5 @@ protected function getAbsoluteUri($entity) {
$uri_info = $entity->uri();
return url($uri_info['path'], array('absolute' => TRUE));
}
}
<?php
/**
* @file
* Contains \Drupal\rdf\Tests\Field\TaxonomyTermReferenceRdfaTest.
*/
namespace Drupal\rdf\Tests\Field;
use Drupal\rdf\Tests\Field\FieldRdfaTestBase;
use Drupal\Core\Language\Language;
/**
* Tests the RDFa output of the taxonomy term reference field formatter.
*/
class TaxonomyTermReferenceRdfaTest extends FieldRdfaTestBase {
/**
* {@inheritdoc}
*/
protected $fieldType = 'taxonomy_term_reference';
/**
* The term for testing.
*
* @var \Drupal\taxonomy\Entity\Term
*/
protected $term;
/**
* The URI of the term for testing.
*
* @var string
*/
protected $termUri;
/**
* {@inheritdoc}
*/
public static $modules = array('taxonomy');
public static function getInfo() {
return array(
'name' => 'Field formatter: taxonomy term reference',
'description' => 'Tests RDFa output by taxonomy term reference field formatters.',
'group' => 'RDF',
);
}
public function setUp() {
parent::setUp();
$this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy'));
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomName(),
'vid' => drupal_strtolower($this->randomName()),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
));
$vocabulary->save();
entity_create('field_entity', array(
'name' => $this->fieldName,
'entity_type' => 'entity_test_render',
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocabulary->id(),
'parent' => 0,
),
),
),
))->save();
entity_create('field_instance', array(
'entity_type' => 'entity_test_render',
'field_name' => $this->fieldName,
'bundle' => 'entity_test_render',
))->save();
$this->term = entity_create('taxonomy_term', array(
'name' => $this->randomName(),
'vid' => $vocabulary->id(),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
));
$this->term->save();
// Add the mapping.
$mapping = rdf_get_mapping('entity_test_render', 'entity_test_render');
$mapping->setFieldMapping($this->fieldName, array(
'properties' => array('schema:about'),
))->save();
// Set up test values.
$this->entity = entity_create('entity_test_render', array());
$this->entity->{$this->fieldName}->target_id = $this->term->id();
$this->entity->save();
$this->uri = $this->getAbsoluteUri($this->entity);
}
/**
* Tests the plain formatter.
*/
public function testPlainFormatter() {
$this->assertFormatterRdfa('taxonomy_term_reference_plain', 'http://schema.org/about', $this->term->label(), 'literal');
}
/**
* Tests the link formatter.
*/
public function testLinkFormatter() {
$term_uri = $this->getAbsoluteUri($this->term);
$this->assertFormatterRdfa('taxonomy_term_reference_link', 'http://schema.org/about', $term_uri, 'uri');
}
}
<?php
/**
* @file
* Contains \Drupal\rdf\Tests\Field\TestDataConverter.
*/
namespace Drupal\rdf\Tests\Field;
/**
* Contains methods for test data conversions.
*/
class TestDataConverter {
/**
* Converts data into a string for placement into a content attribute.
*
* @param array $data
* The data to be altered and placed in the content attribute.
*
* @return string
* Returns the data.
*/
static function convertFoo($data) {
return 'foo' . $data['value'];
}
}
......@@ -419,7 +419,8 @@ protected function assertRdfaArticleProperties($graph, $message_prefix) {
$this->assertTrue($graph->hasProperty($this->articleUri, 'http://schema.org/about', $expected_value), "$message_prefix tag was found (schema:about).");
// Tag type.
$this->assertEqual($graph->type($this->termUri), 'schema:Thing', 'Tag type was found (schema:Thing).');
// @todo enable with https://drupal.org/node/2072791
//$this->assertEqual($graph->type($this->termUri), 'schema:Thing', 'Tag type was found (schema:Thing).');
// Tag name.
$expected_value = array(
......@@ -427,7 +428,8 @@ protected function assertRdfaArticleProperties($graph, $message_prefix) {
'value' => $this->term->get('name')->offsetGet(0)->get('value')->getValue(),
'lang' => 'en',
);
$this->assertTrue($graph->hasProperty($this->termUri, 'http://schema.org/name', $expected_value), "$message_prefix name was found (schema:name).");
// @todo enable with https://drupal.org/node/2072791
//$this->assertTrue($graph->hasProperty($this->termUri, 'http://schema.org/name', $expected_value), "$message_prefix name was found (schema:name).");
}
/**
......
......@@ -123,23 +123,24 @@ function testNodeTeaser() {
'type' => 'uri',
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
);
$this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).');
// @todo enable with https://drupal.org/node/2072791
//$this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).');
$expected_value = array(
'type' => 'literal',
'value' => $term1->label(),
);
$this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).');
//$this->assertTrue($graph->hasProperty($taxonomy_term_1_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).');
// Term 2.
$expected_value = array(
'type' => 'uri',
'value' => 'http://www.w3.org/2004/02/skos/core#Concept',
);
$this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).');
//$this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Taxonomy term type found in RDF output (skos:Concept).');
$expected_value = array(
'type' => 'literal',
'value' => $term2->label(),
);
$this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).');
//$this->assertTrue($graph->hasProperty($taxonomy_term_2_uri, 'http://www.w3.org/2000/01/rdf-schema#label', $expected_value), 'Taxonomy term name found in RDF output (rdfs:label).');
}
/**
......
......@@ -196,6 +196,27 @@ function rdf_rdfa_attributes($mapping, $data = NULL) {
* @} End of "addtogroup rdf".
*/
/**
* Implements hook_entity_prepare_view().
*/
function rdf_entity_prepare_view($entity_type, array $entities, array $displays) {
// Iterate over the RDF mappings for each entity and prepare the RDFa
// attributes to be added inside field formatters.
foreach ($entities as $entity) {
$mapping = rdf_get_mapping($entity_type, $entity->bundle());
// Only prepare the RDFa attributes for the fields which are configured to
// be displayed.
foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) {
$field_mapping = $mapping->getPreparedFieldMapping($name);
if ($field_mapping['properties']) {
foreach ($entity->get($name) as $item) {
$item->_attributes += rdf_rdfa_attributes($field_mapping, $item->getValue());
}
}
}
}
}
/**
* Implements hook_comment_load().
*/
......@@ -317,43 +338,6 @@ function rdf_preprocess_node(&$variables) {
}
}
/**
* Implements hook_preprocess_HOOK() for field.tpl.php.
*/
function rdf_preprocess_field(&$variables) {
$element = $variables['element'];
$entity_type = $element['#entity_type'];
$bundle = $element['#bundle'];
$field_name = $element['#field_name'];
$field_mapping = rdf_get_mapping($entity_type, $bundle)
->getPreparedFieldMapping($field_name);
if (!empty($field_mapping)) {
foreach ($element['#items'] as $delta => $item) {
$variables['item_attributes'][$delta] = rdf_rdfa_attributes($field_mapping, $item);
// If this field is an image, RDFa will not output correctly when the
// image is in a containing <a> tag. If the field is a file, RDFa will
// not output correctly if the filetype icon comes before the link to the
// file. We correct this by adding a resource attribute to the div if
// this field has a URI.
if (isset($item['entity']->uri)) {
if (!empty($element[$delta]['#image_style'])) {
$variables['item_attributes'][$delta]['resource'] = entity_load('image_style', $element[$delta]['#image_style'])->buildUrl($item['entity']->getFileUri());
}
else {
$variables['item_attributes'][$delta]['resource'] = file_create_url($item['entity']->getFileUri());
}
}
// Convert the item_attributes for this field item back into an Attribute
// object for printing. This is necessary because at this time this
// preprocess function overwrites $variables['item_attributes'][$delta],
// which is initialized as an Attribute object in
// template_preprocess_field().
$variables['item_attributes'][$delta] = new Attribute($variables['item_attributes'][$delta]);
}
}
}
/**
* Implements hook_preprocess_HOOK() for user.tpl.php.
*/
......@@ -558,38 +542,6 @@ function rdf_preprocess_taxonomy_term(&$variables) {
drupal_add_html_head($term_label_meta, 'rdf_term_label');
}
/**
* Implements hook_field_attach_view_alter().
*/
function rdf_field_attach_view_alter(&$output, $context) {
// Append term mappings on displayed taxonomy links.
foreach (element_children($output) as $field_name) {
$element = &$output[$field_name];
if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') {
foreach ($element['#items'] as $delta => $item) {
// This function is invoked during entity preview when taxonomy term
// reference items might contain free-tagging terms that do not exist
// yet and thus have no $item['entity'].
if (isset($item['entity'])) {
$term = $item['entity'];
$mapping = rdf_get_mapping('taxonomy_term', $term->bundle());
$bundle_mapping = $mapping->getPreparedBundleMapping();
if (!empty($bundle_mapping['types'])) {
$element[$delta]['#options']['attributes']['typeof'] = $bundle_mapping['types'];
}
$name_field_mapping = $mapping->getPreparedFieldMapping('name');
if (!empty($name_field_mapping['properties'])) {
// A property attribute is used with an empty datatype attribute so
// the term name is parsed as a plain literal in RDFa 1.0 and 1.1.
$element[$delta]['#options']['attributes']['property'] = $name_field_mapping['properties'];
$element[$delta]['#options']['attributes']['datatype'] = '';
}
}
}
}
}
}
/**
* Implements hook_preprocess_HOOK() for theme_image().
*/
......
......@@ -68,4 +68,52 @@ function testEntityViewController() {
$this->assertRaw('full');
}
}
/**
* Tests field item attributes.
*/
public function testFieldItemAttributes() {
// Create a text field which will be rendered with custom item attributes.
entity_create('field_entity', array(
'name' => 'field_test_text',
'entity_type' => 'entity_test_render',
'type' => 'text',
))->save();
entity_create('field_instance', array(
'entity_type' => 'entity_test_render',
'field_name' => 'field_test_text',
'bundle' => 'entity_test_render',
))->save();
entity_get_display('entity_test_render', 'entity_test_render', 'full')
->setComponent('field_test_text', array('type' => 'text_default'))
->save();
// Create an entity and save test value in field_test_text.
$test_value = $this->randomName();
$entity = entity_create('entity_test_render', array());
$entity->field_test_text = $test_value;
$entity->save();
// Browse to the entity and verify that the attribute is rendered in the
// field item HTML markup.
$this->drupalGet('entity-test-render/' . $entity->id());
$xpath = $this->xpath('//div[@data-field-item-attr="foobar" and text()=:value]', array(':value' => $test_value));
$this->assertTrue($xpath, 'The field item attribute has been found in the rendered output of the field.');
// Enable the RDF module to ensure that two modules can add attributes to
// the same field item.
module_enable(array('rdf'));
// Set an RDF mapping for the field_test_text field. This RDF mapping will
// be turned into RDFa attributes in the field item output.
$mapping = rdf_get_mapping('entity_test_render', 'entity_test_render');
$mapping->setFieldMapping('field_test_text', array(
'properties' => array('schema:text'),
))->save();
// Browse to the entity and verify that the attributes from both modules
// are rendered in the field item HTML markup.
$this->drupalGet('entity-test-render/' . $entity->id());
$xpath = $this->xpath('//div[@data-field-item-attr="foobar" and @property="schema:text" and text()=:value]', array(':value' => $test_value));
$this->assertTrue($xpath, 'The field item attributes from both modules have been found in the rendered output of the field.');
}
}
......@@ -494,3 +494,19 @@ function _entity_test_record_hooks($hook, $data) {
$hooks[$hook] = $data;
$state->set($key, $hooks);
}
/**
* Implements hook_entity_prepare_view().
*/
function entity_test_entity_prepare_view($entity_type, array $entities, array $displays) {
// Add a dummy field item attribute on field_test_text if it exists.
if ($entity_type = 'entity_test_render') {
foreach ($entities as $entity) {
if ($entity->getPropertyDefinition('field_test_text')) {
foreach ($entity->get('field_test_text') as $item) {
$item->_attributes += array('data-field-item-attr' => 'foobar');
}
}
}
}
}
......@@ -48,6 +48,14 @@ public function viewElements(FieldInterface $items) {
'#href' => $uri['path'],
'#options' => $uri['options'],
);
if (!empty($item->_attributes)) {
$elements[$delta]['#options'] += array('attributes' => array());
$elements[$delta]['#options']['attributes'] += $item->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and should not be rendered in the field template.
unset($item->_attributes);
}
}
}
......
......@@ -23,7 +23,6 @@ fieldMappings:
uid:
properties:
- 'schema:author'
mapping_type: 'rel'
comment_count:
properties:
- 'schema:interactionCount'
......@@ -34,8 +33,6 @@ fieldMappings:
field_image:
properties:
- 'schema:image'
mapping_type: 'rel'
field_tags:
properties:
- 'schema:about'
mapping_type: 'rel'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment