I'd like to refer to this D6 CCK issue: #276988: nodereference doesnt work when titles exceeds 128 characters.

At the moment, using References in D7, you can't reference a node when the title of that node exceeds 128 characters. The following error is displayed:

field_mynoderef cannot be longer than 128 characters but is currently 147 characters long.

In Drupal 7 node titles can be 255 characters long. Any quick fixes for this?

Comments

aidanlis’s picture

Subscribe. In my opinion this is a release blocker.

damienmckenna’s picture

I believe this is because the default maxlength on a textfield in D7 is 128 characters:

damienmckenna’s picture

Status: Active » Needs review
StatusFileSize
new762 bytes

Here's a quick patch to add '#maxlength' => 255 to the fields. I didn't touch user_reference as the {users}->name field's length is 60.

MichaelP’s picture

Patch at #3 good for me. Thanks to @DamienMcKenna

scor’s picture

Status: Needs review » Reviewed & tested by the community

tested the patch and it fixes the validation issue.

yched’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks !

Status: Fixed » Closed (fixed)

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

golubovicm’s picture

Hello,

I'm running Drupal 7.2 and I got same problem - title limit for referenced nodes is 128 chars.
I found this patch and tried to implement it, but I found out that row

'#maxlength' => 255,

already exists in node_reference.module file?!? It's not 456th row, like patch says, but 540 (probably newer version of Drupal, but it won't work - limit is still 128 chars.

Is ti possible that patch is implemented badly, so it won't work. Here is how my function looks like:

function node_reference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
switch ($instance['widget']['type']) {
case 'node_reference_autocomplete':
$element += array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['nid']) ? $items[$delta]['nid'] : NULL,
'#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'] . '/' . $instance['entity_type'] . '/' . $field['field_name'],
'#size' => $instance['widget']['settings']['size'],
'#maxlength' => 255,
'#element_validate' => array('node_reference_autocomplete_validate'),
'#value_callback' => 'node_reference_autocomplete_value',
);
break;
}

return array('nid' => $element);
}

scor’s picture

@golubovicm this patch has already been committed and you should not have to try to apply it as long as you are using one of the beta version of this module.

golubovicm’s picture

Thank you for reply scor.

I didn't applied it - I just checked what it changes and found that it just adds one row:

'#maxlength' => 255,

which already exists in my version of module. But titles are still limited to 128 chars, so I don't know how to solve this? How to change title limit? Is problem dev version of reference module?

golubovicm’s picture

To be more precise, I'm using autocomplete deluxe node reference field.

scor’s picture

tested the latest beta and -dev, and the 255 limit works, I have maxlength="255" in the appropriate input HTML element. It looks like you might have another module which might alter the form. Please file a separate support issue to discuss this further.

golubovicm’s picture

Thank you scor. I'll look for that other module.

golubovicm’s picture

I have found that only "acdx_references" module (auto complete deluxe) is overriding that function and inserted maxlength there too, but unfortulatelly didn't help. So I give up on this... :(

scor’s picture

Status: Closed (fixed) » Needs review
StatusFileSize
new851 bytes

actually, 255 is not enough for nodes which reach the limit of the node title length because we also need to account for the ending [nid:xxx] which can take up at least 7 chars but be much more on large sites. so the maxlength is unpredictable. This patch unsets the maxlength altogether.

Status: Needs review » Needs work

The last submitted patch, 1064544_node_reference_maxlength.patch, failed testing.

grndlvl’s picture

StatusFileSize
new111.28 KB

This patch solves the problem of the field not accepting 255 characters(Necessary since node supports 255 characters).

We solve this by using a function that accounts for the nid information as well as the title that may be referenced throughout the module and other modules(node references dialog).

/**
 * Used to ensure that the value is never more than 255 characters.
 *
 * @param $title
 *  The string title of the node.
 * @param $nid
 *  The nid of the node.
 * @param $title_only
 *  Boolean value used to determine if only the safe title should be returned.
 *
 * @return string
 *  Either the full field value or only the title will be returned.
 */
function node_reference_field_safe_value($title, $nid, $title_only = FALSE) {
  $safe_title = $title = trim($title);
  $value = $title . " [nid:$nid]";

  $value_length = strlen($value);
  if ($value_length > 255) {
    $safe_title = truncate_utf8($safe_title, 255+255-$value_length);
    $value = $safe_title . " [nid:$nid]";
  }

  if ($title_only) {
    $value = $safe_title;
  }
  return $value;
}

Patch was applied to 7.x-2.x-dev

grndlvl’s picture

StatusFileSize
new3.15 KB

Apologies but please Ignore the patch from #17. Accidentally made from Drupal root.

The following patch contains the only the proposed fix, again for 7.x-2.x.

Thanks,

Jonathan

scor’s picture

Status: Needs work » Closed (duplicate)

closing this. This is being solved at #1183300: remove maxlength from node_reference_autocomplete where yched provided a valid rationale: "The value that we store from the widget is a nid anyway, so I'm not sure what's the point of settings a #maxlength on the textfield form input anyway ?"