Commit 71a4ab13 authored by alexpott's avatar alexpott

Issue #1972514 by Wim Leers, effulgentsia, Gábor Hojtsy, ekl1773: Fixed...

Issue #1972514 by Wim Leers, effulgentsia, Gábor Hojtsy, ekl1773: Fixed Impossible to set attributes for all entities.
parent f7ded179
......@@ -14,6 +14,7 @@
use Drupal\Core\Template\Attribute;
use Drupal\Core\Utility\ThemeRegistry;
use Drupal\Core\Theme\ThemeSettings;
use Drupal\Component\Utility\NestedArray;
/**
* @defgroup content_flags Content markers
......@@ -1056,9 +1057,7 @@ function theme($hook, $variables = array()) {
if (!empty($info[$phase])) {
foreach ($info[$phase] as $processor_function) {
if (function_exists($processor_function)) {
// We don't want a poorly behaved process function changing $hook.
$hook_clone = $hook;
$processor_function($variables, $hook_clone);
$processor_function($variables, $hook, $info);
}
}
}
......@@ -2567,7 +2566,7 @@ function _theme_table_cell($cell, $header = FALSE) {
* @see theme()
* @see template_process()
*/
function template_preprocess(&$variables, $hook) {
function template_preprocess(&$variables, $hook, $info) {
// Tell all templates where they are located.
$variables['directory'] = path_to_theme();
......@@ -2582,11 +2581,16 @@ function template_preprocess(&$variables, $hook) {
if (!isset($default_variables)) {
$default_variables = _template_preprocess_default_variables();
}
$variables += $default_variables + array(
'attributes' => array(),
'title_attributes' => array(),
'content_attributes' => array(),
);
$variables += $default_variables;
// When theming a render element, merge its #attributes into
// $variables['attributes'].
if (isset($info['render element'])) {
$key = $info['render element'];
if (isset($variables[$key]['#attributes'])) {
$variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], $variables[$key]['#attributes']);
}
}
}
/**
......@@ -2595,6 +2599,9 @@ function template_preprocess(&$variables, $hook) {
function _template_preprocess_default_variables() {
// Variables that don't depend on a database connection.
$variables = array(
'attributes' => array(),
'title_attributes' => array(),
'content_attributes' => array(),
'title_prefix' => array(),
'title_suffix' => array(),
'db_is_active' => !defined('MAINTENANCE_MODE'),
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Utility\NestedArray;
/**
* Denotes that a block is not enabled in any region and should not be shown.
......@@ -539,6 +540,16 @@ function template_preprocess_block(&$variables) {
$variables['attributes']['class'][] = 'block';
$variables['attributes']['class'][] = drupal_html_class('block-' . $variables['configuration']['module']);
// The block template provides a wrapping element for the content. Render the
// #attributes of the content on this wrapping element rather than passing
// them through to the content's #theme function/template. This allows the
// content to not require a function/template at all, or if it does use one,
// to not require it to output an extra wrapping element.
if (isset($variables['content']['#attributes'])) {
$variables['content_attributes'] = NestedArray::mergeDeep($variables['content_attributes'], $variables['content']['#attributes']);
unset($variables['content']['#attributes']);
}
// Add default class for block content.
$variables['content_attributes']['class'][] = 'content';
......
......@@ -178,16 +178,14 @@ function contextual_element_info() {
* @see contextual_page_build()
* @see \Drupal\contextual\ContextualController::render()
*/
function contextual_preprocess(&$variables, $hook) {
$hooks = theme_get_registry(FALSE);
function contextual_preprocess(&$variables, $hook, $info) {
// Determine the primary theme function argument.
if (!empty($hooks[$hook]['variables'])) {
$keys = array_keys($hooks[$hook]['variables']);
if (!empty($info['variables'])) {
$keys = array_keys($info['variables']);
$key = $keys[0];
}
elseif (!empty($hooks[$hook]['render element'])) {
$key = $hooks[$hook]['render element'];
elseif (!empty($info['render element'])) {
$key = $info['render element'];
}
if (!empty($key) && isset($variables[$key])) {
$element = $variables[$key];
......
......@@ -14,6 +14,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\edit\Form\EditFieldForm;
use Drupal\Component\Utility\NestedArray;
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
/**
* Implements hook_menu().
......@@ -142,35 +143,10 @@ function edit_preprocess_field(&$variables) {
}
/**
* Implements hook_preprocess_HOOK() for node.html.twig.
*
* @todo Remove this, handle in generic way: http://drupal.org/node/1972514.
*/
function edit_preprocess_node(&$variables) {
$node = $variables['elements']['#node'];
$variables['attributes']['data-edit-entity'] = 'node/' . $node->nid;
}
/**
* Implements hook_preprocess_HOOK() for taxonomy-term.tpl.php.
*
* @todo Remove this, handle in generic way: http://drupal.org/node/1972514.
* Implements hook_entity_view_alter().
*/
function edit_preprocess_taxonomy_term(&$variables) {
$term = $variables['elements']['#term'];
$variables['attributes']['data-edit-entity'] = 'taxonomy_term/' . $term->id();
}
/**
* Implements hook_preprocess_HOOK() for block.html.twig.
*
* @todo Remove this, handle in generic way: http://drupal.org/node/1972514.
*/
function edit_preprocess_block(&$variables) {
if (isset($variables['elements']['content']['#custom_block'])) {
$custom_block = $variables['elements']['content']['#custom_block'];
$variables['attributes']['data-edit-entity'] = 'custom_block/' . $custom_block->id();
}
function edit_entity_view_alter(&$build, EntityInterface $entity, EntityDisplay $display) {
$build['#attributes']['data-edit-entity'] = $entity->entityType() . '/' . $entity->id();
}
/**
......
......@@ -35,6 +35,28 @@ function setUp() {
theme_enable(array('test_theme'));
}
/**
* Test attribute merging.
*
* Render arrays that use a render element and templates (and hence call
* template_preprocess()) must ensure the attributes at different occassions
* are all merged correctly:
* - $variables['attributes'] as passed in to theme()
* - the render element's #attributes
* - any attributes set in the template's preprocessing function
*/
function testAttributeMerging() {
$output = theme('theme_test_render_element', array(
'elements' => array(
'#attributes' => array('data-foo' => 'bar'),
),
'attributes' => array(
'id' => 'bazinga',
),
));
$this->assertIdentical($output, '<div id="bazinga" data-foo="bar" data-variables-are-preprocessed></div>' . "\n");
}
/**
* Test function theme_get_suggestions() for SA-CORE-2009-003.
*/
......
......@@ -17,6 +17,10 @@ function theme_test_theme($existing, $type, $theme, $path) {
$items['theme_test_foo'] = array(
'variables' => array('foo' => NULL),
);
$items['theme_test_render_element'] = array(
'render element' => 'elements',
'template' => 'theme-test-render-element',
);
$items['theme_test_render_element_children'] = array(
'render element' => 'element',
);
......@@ -153,6 +157,13 @@ function theme_theme_test_foo($variables) {
return $variables['foo'];
}
/**
* Process variables for theme-test-render-element.tpl.php.
*/
function template_preprocess_theme_test_render_element(&$variables) {
$variables['attributes']['data-variables-are-preprocessed'] = TRUE;
}
/**
* Theme function for testing rendering of child elements via drupal_render().
*
......
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