Problem/Motivation

We shouldn’t be overwriting the $langcode variable that’s being passed to the hook. This can cause some unpredictable behavior when we put links inside nested entities. For example: A link inside a field collection inside a node.

link.module (line 420)

function _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &$optional_field_found, &$errors) {
    // . . . 
    // We should remove this line below 
    $langcode = !empty($entity) ? field_language($instance['entity_type'], $entity, $instance['field_name']) : LANGUAGE_NONE;
    if (!link_validate_url(trim($item['url']), $langcode)) {     

Refer to the core text.module (line 118).
This function doesn't overwrite the $langcode variable. We shouldn't either.

function text_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    // @todo Length is counted separately for summary and value, so the maximum
    //   length can be exceeded very easily.
    foreach (array('value', 'summary') as $column) {
      if (!empty($item[$column])) {
        if (!empty($field['settings']['max_length']) && drupal_strlen($item[$column]) > $field['settings']['max_length']) {
          switch ($column) {
            case 'value':
              $message = t('%name: the text may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
              break;

            case 'summary':
              $message = t('%name: the summary may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
              break;
          }
          $errors[$field['field_name']][$langcode][$delta][] = array(
            'error' => "text_{$column}_length",
            'message' => $message,
          );
        }
      }
    }
  }
}

This code was introduced in this issue.
https://www.drupal.org/node/2247261

Comments

partdigital created an issue. See original summary.

partdigital’s picture

Issue summary: View changes
partdigital’s picture

Issue summary: View changes
partdigital’s picture

Issue summary: View changes
partdigital’s picture

Issue summary: View changes
partdigital’s picture

Issue summary: View changes
partdigital’s picture

partdigital’s picture

Issue summary: View changes
Related issues: +#2247261: URLs are not validated
jcfiala’s picture

Good point.

jcfiala’s picture

Status: Active » Fixed

Patch applied to the 7.x-1.x branch, should show up in 24 hours or so.

partdigital’s picture

I don't know how clean your want your commit history but I noticed the commit message was wrong, it says "UPC-99: UPC-99: Apply patch: link-field_link_validate_overrites_langcode-2632728.patch' something more meaningful would be: 'Issue #2632728 by partdigital: field_link_validate overwrites $langcode variable. Is it possible to rewrite that commit?

Status: Fixed » Closed (fixed)

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