API page: https://api.drupal.org/api/drupal/modules%21field%21field.api.php/functi...

This hook has no example code. However, there's a good example in one of the comments: https://api.drupal.org/comment/53203#comment-53203

This should be incorporated into the documentation.

Comments

jhodgdon’s picture

Good idea -- patch welcome!

opdavies’s picture

Issue tags: +Amsterdam2014
StatusFileSize
new725 bytes

Here's a patch that adds the example code from the comment. Is this all we need?

opdavies’s picture

Status: Active » Needs review
jhodgdon’s picture

Status: Needs review » Needs work

Thanks!

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 ...

opdavies’s picture

When documenting a hook, the example code goes into the function body

OK, I see. Makes sense.

Maybe there is an example in contrib somewhere?

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?

jhodgdon’s picture

Um, we're looking for a function ending in _field_attach_validate(), not _field_attach_value() -- that's a different hook.

opdavies’s picture

Sorry, I meant taxonomy_access_field_attach_validate().

Mirroar’s picture

taxonomy_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.

Mirroar’s picture

So say a client wants to enforce the alt attribute on any image on article nodes. Something like the following could be done, right?

  if ($entity_type == 'node' && $entity->type == 'article') {
    if (!empty($entity->field_image)) {
      foreach ($entity->field_image as $langcode => $values) {
        foreach ($values as $delta => $value) {
          // make sure any images have an alt text
          if (!empty($value['fid']) && empty($value['alt'])) {
            $errors['field_image'][$langcode][$delta][] = array(
              'error' => 'field_example_invalid',
              'message' => t('All images in articles need to have an alternative text set.'),
            );
          }
        }
      }
    }
  }

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 $error gets 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.

joachim’s picture

Looks 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.

Mirroar’s picture

Status: Needs work » Needs review
StatusFileSize
new1.01 KB

Thanks 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.

jhodgdon’s picture

Status: Needs review » Needs work

Looks 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).

nlisgo’s picture

Status: Needs work » Needs review

Improved the documentation for errors parameter

nlisgo’s picture

StatusFileSize
new1.49 KB

Would be useful to supply the patch :)

joachim’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me!

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

Thanks! Looks exellent to me too. Committed to 7.x.

  • jhodgdon committed 57236b0 on 7.x
    Issue #2329189 by nlisgo, joachim, Mirroar, opdavies: Fix up docs and...

Status: Fixed » Closed (fixed)

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