Download & Extend

Field form structure incomplete if field_access() returns FALSE

Project:Drupal core
Version:7.x-dev
Component:field system
Category:bug report
Priority:major
Assigned:dags
Status:closed (fixed)
Issue tags:7.15 release blocker, 7.15 release notes, needs backport to D7, Needs issue summary update, Novice

Issue Summary

In field_default_form(), it only populates the form if field_access() returns true, this means you get a completely different form structure depending on whether access is true or false, and any code which might depend on that form structure can get horribly broken.

Instead we should have the same form structure, but set #access => FALSE.

Attaching a test (which should fail with one fail and two exceptions) first, then test and patch forthcoming.

Change records for this issue

AttachmentSizeStatusTest resultOperations
field_access.patch1.19 KBIdleFAILED: [[SimpleTest]]: [MySQL] 20,678 pass(es), 1 fail(s), and 2 exception(es).View details

Comments

#1

And the full patch.

AttachmentSizeStatusTest resultOperations
822418_field_access_form.patch7.58 KBIdleFAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 822418_field_access_form.patch.View details

#2

Hm, I think we went back and forth on this in, IIRC, #629252: field_attach_form() should make available all field translations on submit.
Trying to get my brain in 'remember' mode.

#3

Actually, this very change (or something very similar) went in with #636834: Field revision data messed up when user has no 'edit' access on the field.

And #629252-22: field_attach_form() should make available all field translations on submit was the reason this got changed back - quoting / adapting from there:
Imagine an integer field, with a 'min value' setting of '0'.
Create a node with value '1' for the field.
Then edit the field and set the min value to '2'.
If a user doesn't have 'edit' access for the field, and we include the widget in the form with #access = FALSE, any attempt at editing the node will fail validation, because there are some submitted values that are invalid.
And the user cannot do anything about it, because the invalid value is not accessible to him.

#4

Imagine an integer field, with a 'min value' setting of '0'.
Create a node with value '1' for the field.
Then edit the field and set the min value to '2'.
If a user doesn't have 'edit' access for the field, and we include the widget in the form with #access = FALSE, any attempt at editing the node will fail validation, because there are some submitted values that are invalid.
And the user cannot do anything about it, because the invalid value is not accessible to him.

That sounds like correct behaviour to me to be honest, a bit annoying if you're that user, but it's the fault of the administrator, not Drupal, that you get into that situation. Certainly it's more of an edge case than having code which works without field access then fails once you add it.

#5

"it's the fault of the administrator, not Drupal, that you get into that situation"
I don't really see how. The admin used the UI to do perfectly valid changes.

from #629252-23: field_attach_form() should make available all field translations on submit :
"Hm. Unless we explicitly make field_default_form_errors() *not* report errors on elements where #access == FALSE".

#6

Hmmm. I think I could go either way on this one. Seems like a reasonable compromise to add "field_default_form_errors() *not* report errors on elements where #access == FALSE"

#7

That seems like a decent compromise to me too. My main concern is that hook_field_attach_submit() gets the same $form and $form_state each time it's called - the way this was found was a hook implementation added four months ago, which worked fine for four months, then suddenly stopped working when hook_field_access() was implemented and the usual array structure disappeared from under it. I'll look at field_default_form_errors() a bit later today and see if I can add that to the patch.

#8

Priority:normal» major

bump. still buggy.

#9

subscribe

#10

subscribe

#12

subscribe

#14

subscribe

#15

Version:7.x-dev» 8.x-dev

#16

#1: 822418_field_access_form.patch queued for re-testing.

#17

Status:needs review» needs work

The last submitted patch, 822418_field_access_form.patch, failed testing.

#18

subscribe

#19

Reroll of patch to head

AttachmentSizeStatusTest resultOperations
form-field-acess-822418-19.patch7.52 KBIdleFAILED: [[SimpleTest]]: [MySQL] 32,657 pass(es), 57 fail(s), and 815 exception(es).View details

#20

Status:needs work» needs review

#21

Status:needs review» needs work

The last submitted patch, form-field-acess-822418-19.patch, failed testing.

#22

Status:needs work» needs review

Bad reroll missed a property lets try again.

AttachmentSizeStatusTest resultOperations
form-field-acess-822418-22.patch7.55 KBIdlePASSED: [[SimpleTest]]: [MySQL] 32,774 pass(es).View details

#23

Status:needs review» needs work

The last submitted patch, form-field-acess-822418-22.patch, failed testing.

#24

Status:needs work» needs review

#22: form-field-acess-822418-22.patch queued for re-testing.

#25

Tagging issues not yet using summary template.

#26

To quickly fix this problem on my D7 site, can I apply form-field-acess-822418-22.patch?

#27

A year and two months latter I looked at field_default_form_errors()...

Untested patch.

AttachmentSizeStatusTest resultOperations
form-field-acess-822418-27.patch10.22 KBIdleFAILED: [[SimpleTest]]: [MySQL] 33,615 pass(es), 25 fail(s), and 19 exception(es).View details

#28

Status:needs review» needs work

The last submitted patch, form-field-acess-822418-27.patch, failed testing.

#29

Status:needs work» needs review
AttachmentSizeStatusTest resultOperations
form-field-acess-822418-29.patch10.23 KBIdleFAILED: [[SimpleTest]]: [MySQL] 33,612 pass(es), 29 fail(s), and 0 exception(es).View details

#30

Status:needs review» needs work

The last submitted patch, form-field-acess-822418-29.patch, failed testing.

#31

+ if (!empty($element['#access'])) { <= that should be if (!isset($element['#access']) || $element['#access'])

#37

Status:needs work» needs review
AttachmentSizeStatusTest resultOperations
form-field-acess-822418.patch10.25 KBIdlePASSED: [[SimpleTest]]: [MySQL] 33,641 pass(es).View details

#38

Status:needs review» needs work

Just comparing side by side for now :

- reroll effect, patch reintroduces a t($instance['label']). t()s around labels have been removed meanwhile.

- "// Locate the correct element in the the form" : the typo is present in the current code, but let's fix it while we move the line around.

- We might want to add a line of comment above the #access check in field_default_form_errors()

Other than that, looks reasonable. We might want a test, though...

#39

subscribe

#40

Issue tags:+Needs tests

Adding "Needs tests" according to #38.

+++ b/modules/field/field.form.incundefined
@@ -37,76 +37,73 @@ function field_default_form($entity_type, $entity, $field, $instance, $langcode,
+  // If field module handles multiple values for this form element, and we
+  // are displaying an individual element, process the multiple value form.

"are" will fit on the previous line.

+++ b/modules/field/field.form.incundefined
@@ -37,76 +37,73 @@ function field_default_form($entity_type, $entity, $field, $instance, $langcode,
+ '#field_parents' => $parents,

Tabs here.

+++ b/modules/field/field.form.incundefined
@@ -37,76 +37,73 @@ function field_default_form($entity_type, $entity, $field, $instance, $langcode,
+  // Also aid in theming of field widgets by rendering a classified
+  // container.

"container" will fit on the previous line.

#41

Issue tags:-Needs tests

The patch includes a test and its failures are exposed in the original post. Is there additional test coverage that is needed?

#42

Issue tags:+Novice

Tagging novice for the cleanups mentioned in #38 and #40.

#43

Assigned to:Anonymous» dags

#44

Status:needs work» needs review

Reroll patch after /core move, address #38 and #40.

AttachmentSizeStatusTest resultOperations
drupal-822418-44.patch11.26 KBIdleFAILED: [[SimpleTest]]: [MySQL] 35,039 pass(es), 25 fail(s), and 32 exception(s).View details
interdiff.txt2.45 KBIgnored: Check issue status.NoneNone

#45

Status:needs review» needs work

The last submitted patch, drupal-822418-44.patch, failed testing.

#46

LANGUAGE_NONE has been renamed to LANGUAGE_NOT_SPECIFIED

#47

Thank you, davidjdagino.

Here's a quick review:

  1. Very minor: "make" would fit on the previous line.
    +++ b/core/modules/field/tests/field.testundefined
    @@ -1650,6 +1650,19 @@ class FieldFormTestCase extends FieldTestCase {
         $langcode = LANGUAGE_NOT_SPECIFIED;
  2. Minor point, too: This line appears to exceed 80 characters.
    +++ b/core/modules/field/tests/field.testundefined
    @@ -1650,6 +1650,19 @@ class FieldFormTestCase extends FieldTestCase {
    +    // Test that the form structure includes full information for each delta apart
  3. +++ b/core/modules/field/tests/field.testundefined
    @@ -1650,6 +1650,19 @@ class FieldFormTestCase extends FieldTestCase {
    +    $langcode = LANGUAGE_NONE;

    LANGUAGE_NONE has been removed from Drupal 8 as of LANGUAGE_NONE changed to LANGUAGE_NOT_SPECIFIED, LANGUAGE_NOT_APPLICABLE and LANGUAGE_MULTIPLE added.
    Note that $langcode is already set to LANGUAGE_NOT_SPECIFIED in the context.

#48

Changes from #47.

AttachmentSizeStatusTest resultOperations
drupal-822418-48.patch11.23 KBIdlePASSED: [[SimpleTest]]: [MySQL] 35,066 pass(es).View details
interdiff.txt1.81 KBIgnored: Check issue status.NoneNone

#49

Status:needs work» needs review

#50

Attached is identical to #48; I just wanted to make sure the tests still show the expected fails since it's been awhile since June 2010.

AttachmentSizeStatusTest resultOperations
drupal-822418-48-tests.patch1.05 KBIdleFAILED: [[SimpleTest]]: [MySQL] 35,074 pass(es), 2 fail(s), and 4 exception(s).View details
drupal-822418-48-complete.patch11.23 KBIdlePASSED: [[SimpleTest]]: [MySQL] 35,072 pass(es).View details

#51

Issue tags:-Novice

Alright, those test failures look correct. All the feedback from #38 on has also been addressed. I reviewed the patch myself and found only tiny stylistic issues; the solution looks complete to me.

+++ b/core/modules/field/field.form.incundefined
@@ -358,31 +355,34 @@ function field_default_form_errors($entity_type, $entity, $field, $instance, $la
+      $function_exists = function_exists($function);
+
+
+      $multiple_widget = field_behaviors_widget('multiple values', $instance) != FIELD_BEHAVIOR_DEFAULT;

Extra blank line here.

+++ b/core/modules/field/field.form.incundefined
@@ -358,31 +355,34 @@ function field_default_form_errors($entity_type, $entity, $field, $instance, $la
+        // For a multiple-value widget, all errors are passed to the main widget.

This line is 81 characters.

+++ b/core/modules/field/tests/field.testundefined
@@ -1650,6 +1650,18 @@ class FieldFormTestCase extends FieldTestCase {
+    $this->assertEqual($form[$field_name_no_access][$langcode][0]['value']['#entity_type'], $entity_type);
+    $this->assertFalse($form[$field_name_no_access]['#access']);

It would be good to have assertion messages for these because otherwise we get things like "Value FALSE is FALSE" in the results which isn't so helpful. (Note: the messages should not be translated; see http://drupal.org/simpletest-tutorial-drupal7#t).

#52

Issue tags:+Novice

Oops, didn't mean to untag!

#53

Assigned to:dags» ezheidtmann

I'm on it.

#54

Fixed those style issues, added an assert message. No attempted comprehension of the rest of the patch! Thanks for the help, xjm.

AttachmentSizeStatusTest resultOperations
tests-only.patch1.11 KBIdleFAILED: [[SimpleTest]]: [MySQL] 35,068 pass(es), 1 fail(s), and 2 exception(s).View details
complete.patch11.28 KBIdlePASSED: [[SimpleTest]]: [MySQL] 35,065 pass(es).View details

#55

Thanks @ezheidtmann! That looks good.

#57

Two more minor tweaks: Assertion message for the other assertion, plus putting FALSE in caps per our text standards.

AttachmentSizeStatusTest resultOperations
822418-56.patch11.34 KBIdlePASSED: [[SimpleTest]]: [MySQL] 35,070 pass(es).View details
interdiff.txt997 bytesIgnored: Check issue status.NoneNone

#58

Status:needs review» reviewed & tested by the community

Re-TBC. Thanks everyone!

#59

Status:reviewed & tested by the community» patch (to be ported)

Thanks folks, committed pushed to 8.x, moving back to 7.x for backport.

#60

Version:8.x-dev» 7.x-dev

#61

Status:patch (to be ported)» needs review

Rerolled.

Once again, I forgot that D8 uses 'complete_form' and not 'complete form' like D7.

AttachmentSizeStatusTest resultOperations
drupal-822418-61-combined.patch11.29 KBIdlePASSED: [[SimpleTest]]: [MySQL] 38,722 pass(es).View details
drupal-822418-61-tests.patch1.14 KBIdleFAILED: [[SimpleTest]]: [MySQL] 38,719 pass(es), 1 fail(s), and 2 exception(s).View details

#62

Status:needs review» needs work

The last submitted patch, drupal-822418-61-tests.patch, failed testing.

#63

Status:needs work» needs review

Excellent.

#64

Status:needs review» reviewed & tested by the community

Thanks @tim.plunkett!

#65

This looks like a legit bug fix, and I've no real problems backporting it, but I'd like to hold this until after Wednesday's release "just in case" it breaks something. It's possible (though unlikely) a contributed module is counting on this current situation.

#66

Assigned to:ezheidtmann» tim.plunkett
Status:reviewed & tested by the community» needs work

This needs a reroll, I'll do it right afte 7.13 comes out.

#67

tim.plunkett, was it rerolled finally?

#68

Status:needs work» needs review

Reroll needed because of #1541792: Enable dynamic allowed list values function with additional context.

AttachmentSizeStatusTest resultOperations
drupal-822418-68.patch11.49 KBIdlePASSED: [[SimpleTest]]: [MySQL] 39,118 pass(es).View details

#69

Status:needs review» reviewed & tested by the community

Re-roll looks good.

#70

Title:Field form structure incomplete if field_access() returns FALSE» Change notification for: Field form structure incomplete if field_access() returns FALSE
Category:bug report» task
Priority:major» critical
Status:reviewed & tested by the community» active
Issue tags:+7.15 release notes

Committed to 7.x and added to CHANGELOG.txt - thanks! http://drupalcode.org/project/drupal.git/commit/84e34e4

Have to say I'm a little scared of this one, but code in general should be checking #access before assuming that a form element will actually be displayed... so hopefully any custom/contrib code that this breaks is code that was already somewhat broken anyway. And we have time to roll this back before the next Drupal 7 release if anyone finds something wrong with it in the meantime.

In addition to the release notes, I think this will need a D7 change notification (since it does change the array structure and the behavior of the field validation code). Moving to a critical task for that.

#71

Issue tags:+7.15 release blocker

We probably need this change notification in place for Drupal 7.15, since I think we want to link to it from the release notes.

#72

Assigned to:tim.plunkett» dags

#73

Status:active» needs review

Added change notice.

#74

Title:Change notification for: Field form structure incomplete if field_access() returns FALSE» Field form structure incomplete if field_access() returns FALSE
Category:task» bug report
Priority:critical» major
Status:needs review» fixed

Looks good to me.

#75

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

#76

Thanks for the change notification (for reference, it's at http://drupal.org/node/1663020)! I've added a link to this in CHANGELOG.txt:
http://drupalcode.org/project/drupal.git/commit/4c0d034

However, this paragraph looked wrong to me:

With this change, site administrators should be particularly careful about modifying the allowed values of existing fields. If a field's existing value lies outside the bounds of allowed values, and a user does not have access to edit that field, then the user can not edit the value to make it valid and will therefore not be allowed to save any changes on the entire form.

Based on the above discussion, that was not supposed to happen in the final patch that was committed here (and I sure hope it doesn't)... I also did a quick test with the Field Permissions module (modifying the minimum allowed value of a private integer field and then trying to edit the node as a user without access to that field, per the scenario in @yched's comment above), and everything worked fine.

So, I have removed that paragraph from the change notification, and consequently also removed "Site builders, administrators, editors" from the list of affected audiences.

Obviously, if I made a mistake there somehow, it's not too late for someone to re-edit and fix it.

#77

Skipped the last posts there somehow. #76 is correct, and thus so is the current change notification.