Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
We are eliminating the process layer from Drupal 8, since the reasons for it's existence can now be solved in different ways. One of the functions on the chopping block is template_process_field(). Since all this function does is add attributes, now renderable Attribute objects, these variables can safely be moved to the preprocess layer.
function template_process_field(&$variables, $hook) {
static $default_attributes;
// The default theme implementation is a function, so template_process() does
// not automatically run, so we need to flatten the classes and attributes
// here. For best performance, only instantiate Drupal\Core\Template\Attribute
// when needed, and note that template_preprocess_field() does not initialize
// the *_attributes variables.
if (!isset($default_attributes)) {
$default_attributes = new Attribute;
}
$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);
foreach ($variables['items'] as $delta => $item) {
$variables['item_attributes'][$delta] = isset($variables['item_attributes'][$delta]) ? new Attribute($variables['item_attributes'][$delta]) : clone($default_attributes);
}
}
Proposed resolution
The template_process_field function needs to be removed from Drupal 8.
Remaining tasks
- Move all attribute variables into the preprocess layer.
- remove this function.
User interface changes
None.
API changes
TBD
Related Issues
#1843650: Remove the process layer (hook_process and hook_process_HOOK)
Comment | File | Size | Author |
---|---|---|---|
#28 | field-before.png | 40.83 KB | jenlampton |
#28 | field-after.png | 40.58 KB | jenlampton |
#27 | 1843728-27.patch | 3.04 KB | star-szr |
#27 | interdiff.txt | 2.46 KB | star-szr |
#16 | 1843528-15.patch | 2.1 KB | mr.baileys |
Comments
Comment #1
jenlamptonRe the comment inside the process function...
I believe is a left-over from when attributes *were* flattened in the process layer. They are not anymore since they are an attributes object that gets flattened at render-time, which is why I think it is safe to move these variables up to preprocess. All attributes are printed correctly with the attached patch.
Comment #3
jenlamptonI think the problem here is that array_merge and array_merge_recursive aren't going to work if one of those "arrays" is actually an Attributes object already. We may need another way for modules to alter attributes. :/
Comment #4
steveoliver CreditAttribution: steveoliver commentedGood news: #1982024: Lazy-load Attribute objects later in the rendering process only if needed
Comment #5
star-szr#1: core-remove_template_process_field-1843728-1.patch queued for re-testing.
Comment #7
patrickfgoddard CreditAttribution: patrickfgoddard commentedre-rolling.
Comment #8
patrickfgoddard CreditAttribution: patrickfgoddard commentedPer: #1982024: Lazy-load Attribute objects later in the rendering process only if needed
and IRC discussion with @steveoliver and Cottser, I set attributes, title_attributes, content_attributes variables in field.module to empty array()
Hopefully this helps move this along. Please let me know if any other assistance needed with changes and/or patches for this issue.
Comment #10
johnshortess#8: core-remove_template_process_field-1843728-8.patch queued for re-testing.
Comment #11
johnshortessComment #13
johnshortessComment #14
mr.baileysTackling this as part of the DC PDX Friday sprint.
Comment #15
mr.baileysPatch from #8 no longer applied so re-rolled it.
The empty array() initialization in #8 is causing "Array to String"-conversion errors during tests, so for now I converted these to "new Attribute" inits in
theme_preprocess_field
.Not sure if this is the approach we are going for in light of #1982024: Lazy-load Attribute objects later in the rendering process only if needed, but this patch removes
field_process_field
and makes the tests pass.Comment #16
mr.baileysActual patch file...
Comment #17
mr.baileysComment #19
mr.baileys#16: 1843528-15.patch queued for re-testing.
Comment #21
jenlampton#16: 1843528-15.patch queued for re-testing.
Comment #23
jenlamptonTests may have been passing in #15, but they aren't now, and these test failures are legit. I'm seeing problems with the body field attributes:
Ideas?
Comment #24
star-szrI don't think we can just clear out those Attribute objects. This one is complex because at least according to the docs both theme functions and template files go through these (pre)process functions. We may need to keep those isset's for now.
Comment #25
jenlamptonI tried adding the issets back but still got the
array=""
on the body tag for comments. I don't think that's the only issue. :/Comment #26
star-szrGoing to see if I can sort this one out.
Comment #27
star-szrLet's see what testbot thinks about this, the failing tests above are green locally.
Before patch:
rdf_preprocess_field() - creates new array of attributes at $variables['item_attributes'][$delta], disregarding any existing value
template_process_field() - converts $variables['item_attributes'][$delta] to an Attribute object
After patch:
template_preprocess_field() - creates new Attribute object for $variables['item_attributes'][$delta]
rdf_preprocess_field() - creates new array of attributes at $variables['item_attributes'][$delta], disregarding any existing value. In this case the Attribute object that we need for printing the attributes gets clobbered so we convert it back to an Attribute object.
I will ping @scor about this issue, maybe there is a better way to solve this. My concern is that currently rdf_preprocess_field() overwrites $variables['item_attributes'][$delta] completely - but arguably that is a separate bug/limitation that can be addressed in a new issue (I couldn't find an existing one).
Comment #28
jenlamptonSounds good. I think we can resolve the RDF overwriting issue in a follow-up issue (and in the RDF module?) Would love to kill process first. :) Testing...
Source before move:
Source after move:
Looking good!
Comment #29
YesCT CreditAttribution: YesCT commentedThis issue was RTBC and passing tests on July 1, the beginning of API freeze.
Comment #30
catchLet's do the RDF fix here, I don't think that's covered by any existing issues.
Comment #31
scor CreditAttribution: scor commentedGiven that this bug in rdf.module will have to be backported to D7, and in the interest of separation of concerns/modules, I've created a dedicated issue to fix the bug in RDF: #2034003: item_attributes are overridden in rdf_preprocess_field() with a patch forthcoming...
Setting this issue back to RTBC.
Comment #32
catchOK thanks for the other issue and the patch. Committed/pushed this one to 8.x.
Comment #33
star-szrThanks @scor and @catch!