Closed (fixed)
Project:
Drupal core
Version:
7.x-dev
Component:
documentation
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
28 Aug 2014 at 13:43 UTC
Updated:
2 Nov 2014 at 13:14 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
jhodgdonGood idea -- patch welcome!
Comment #2
opdaviesHere's a patch that adds the example code from the comment. Is this all we need?
Comment #3
opdaviesComment #4
jhodgdonThanks!
When documenting a hook, the example code goes into the function body. See https://www.drupal.org/node/1354#hooks
Also, I guess the example needs a bit more work. As it is, it doesn't stand alone as an example of how to implement the hook. Maybe there is an example in contrib somewhere? .. maybe this isn't quite a Novice project ...
Comment #5
opdaviesOK, I see. Makes sense.
drupalcontrib.org references taxonomy_access_field_attach_value() from hook_field_attach_validate() (see http://drupalcontrib.org/api/drupal/drupal!modules!field!field.api.php/f...). Is that of any use?
Comment #6
jhodgdonUm, we're looking for a function ending in _field_attach_validate(), not _field_attach_value() -- that's a different hook.
Comment #7
opdaviesSorry, I meant taxonomy_access_field_attach_validate().
Comment #8
Mirroar commentedtaxonomy_access_field_attach_validate seems to be doing a lot. I'm trying to come up with a much more simple example, like adding validation for a very specific field.
Comment #9
Mirroar commentedSo say a client wants to enforce the alt attribute on any image on article nodes. Something like the following could be done, right?
However, I think it still looks a bit ugly, especially with those nested loops. It probably needs some more work.
Also, I noticed that in the documentation for FieldValidationException::__construct (which is where
$errorgets passed to), it says the array is supposed to be keyed by field name and delta. The examples I've seen also include the language code for the field. So either the documentation needs to be updated, or the examples (and the contrib module) are using the hook slightly wrong.Should the langcode not be necessary, I'd change the example to use field_get_items instead of iterating over the entity's field_image property.
Comment #10
joachim commentedLooks good!
> However, I think it still looks a bit ugly, especially with those nested loops. It probably needs some more work.
Most implementations of this hook will need those ugly nested loops, so if anything, it's a good thing to have them in the sample code.
> // make sure any images have an alt text
Needs to have capital & full stop.
> foreach ($values as $delta => $value) {
FieldAPI code calls this $items, not $values. Best to be consistent here, as it will make it easier for developers to understand other parts of FieldAPI.
Comment #11
Mirroar commentedThanks for the feedback. I made it into a patch and also moved the comment to the beginning of the function body to be more in line with the documentation of other hooks. Joining the first two if-statements also got rid of a level of indentation.
Comment #12
jhodgdonLooks great to me!
Could we fix one more thing in this hook documentation though? The doc block says "See field_attach_validate() for details and arguments." However, field_attach_validate() doesn't actually provide any useful documentation about $errors, as far as I can see. I found information about $errors on hook_field_validate(). So I think we should either link to that hook or copy its documentation about $errors (I had to look there to see whether the example code in the patch was doing the right thing with the errors or not; I think it is).
Comment #13
nlisgo commentedImproved the documentation for errors parameter
Comment #14
nlisgo commentedWould be useful to supply the patch :)
Comment #15
joachim commentedLooks good to me!
Comment #16
jhodgdonThanks! Looks exellent to me too. Committed to 7.x.