API page: http://api.drupal.org/api/drupal/modules%21field%21field.api.php/functio...

function hook_field_attach_validate($entity_type, $entity, &$errors) {
  // @todo Needs function body.
}

Enter a descriptive title (above) relating to hook_field_attach_validate, then describe the problem you have found:

> See field_attach_validate() for details and arguments.

Except that doesn't explain how to use the $errors parameter

Comments

jhodgdon’s picture

Yeah, that hook definitely needs some documentation love! It should have a function body and the parameters should definitely be explained. Thanks for the report!

larowlan’s picture

Issue tags: +#pnx-sprint

Tagging

deviantintegral’s picture

As is, this hook is pretty difficult to use. None of the array keys needed for $errors are provided to the hook, so you have to call into the field info API to retrieve them. I've only ever seen this hook implemented twice, in relation_unique and in some custom code. For both of those modules, they were able to simplify the implementation since relations are by their definition not translatable and the field name is well-defined on the relation entity. For reference:

  // First, retrieve the array keys we need for the $errors array.
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  $instance = field_info_instance($entity_type, 'endpoints', $bundle);
  $field = field_info_field_by_id($instance['field_id']);
  $field_name = $field['field_name'];
  $langcode = LANGUAGE_NONE;

This hook probably needs attention for D8 so implementors don't need the boilerplate code. But for D7, I'm hesitant to roll a patch with the above since the field name might not be fixed and the field might be translatable.

Noe_’s picture

Are you guys sure hook_field_attach_validate() still exists in D8?

Because I cannot find it anywhere in the code.

clemens.tolboom’s picture

clemens.tolboom’s picture

Maybe better #1969728: Implement Field API "field types" as TypedData Plugins

Done with

git log -Shook_field_attach_validate
git show a2c2367bf841ca994c0534811dcb1c2843ac6856
jhodgdon’s picture

Version: 8.x-dev » 7.x-dev
Issue tags: -Needs backport to D7

7.x only issue now.

clemens.tolboom’s picture

Issue summary: View changes
clemens.tolboom’s picture

@Noe_ a patch bingo during a D8 sprint is risky.

sandykadam’s picture

Status: Active » Needs review
StatusFileSize
new758 bytes

Fixed added patch for missing param docs

jhodgdon’s picture

Status: Needs review » Needs work

Thanks! Looks pretty good. A few minor typographical things to fix:

+ *
+ * @param $entity_type
+ *   The type of $entity; e.g. 'node' or 'user'.
+ * @param $entity
+ *   The entity with fields to validate.
+ * @param array $errors
+ *   An associative array of error keyed by field_name, language and delta.
  */

- Punctuation: e.g. must be followed by a comma. Or better yet, use "for example,", which is less liable to be confused with i.e..
- In Drupal we try to have serial commas, so in the last line, there should be a comma between "language" and "and".
- In the last line, error -> errors

sandykadam’s picture

Status: Needs work » Needs review
StatusFileSize
new757 bytes

@jhodgdon Thanks for review, I have updated the patch. Kept e.g with comma because we are using everywhere in documentation.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! Looks fine.

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

Thanks again! Committed to 7.x.

sandykadam’s picture

Thanks @jhodgdon

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

Weird. Issue status in #14 says I changed it to Fixed, but it was back to RTBC. ?!?

sandykadam’s picture

Yeh you committed the code but you change the status to RTBC!

Status: Fixed » Closed (fixed)

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