Advertising sustains the DA. Ads are hidden for members. Join today

Computed Field

Making a node link to itself

Last updated on
30 April 2025

These examples demonstrate how to output the node ID (nid) for the current page in the form of a link to itself.

Drupal 7 Example - Linking to Title

In your computed field's configuration:

Computed Code:

// ensure the node has an id by saving it if it is new.
if (!$entity->nid) {
  node_save($entity);
}
// store the nid in our computed field
$entity_field[0]['value'] = $entity->nid . '.' . check_plain($entity->title);

Display Format:

$temp = explode('.', $entity_field_item['value'], 2);
// $display_output can hold other HTML markup if you need it.
$display_output  = l($temp[1], 'node/'.$temp[0]);

When creating the computed field, be sure to allow the Data Length to be long enough to hold the computed field.

Drupal 7 Example - Linking with Text Value of Another Text Field

In your computed field's configuration:

Computed Code:

// ensure the node has an id by saving it if it is new.
if (!$entity->nid) {
  node_save($entity);
}
// store the nid and text from another text field in our computed field
$variable_to_hold_other_field_items = field_get_items($entity_type, $entity, 'field_machine_name_for_other_field_here');
// store the nid with the value of our text field in our computed field
$entity_field[0]['value'] = $entity->nid.".".$variable_to_hold_other_field_items[0]['value'];

When creating the computed field, be sure to allow the Data Length to be long enough to hold the computed field.

Display Format:

$temp = explode('.', $entity_field_item['value'], 2);
// $display_output can hold other HTML markup if you need it.
$display_output  = l($temp[1], 'node/'.$temp[0]);

Drupal 6 Example

In your computed field's configuration:

Computed Code:

<?php
// ensure the node has an id by saving it if it is new.
if (!$node->nid) {
  node_save($node);
}
// store the nid in our computed field
$node_field[0]['value'] = $node->nid . '.' . check_plain($node->title);
?>

Check 'Display this field'

Display Format:

<?php
$temp = explode('.', $node_field_item['value'], 2);
$display = l($temp[1], 'node/'.$temp[0]);
?>

Uncheck 'Store using the database settings below'. You could store this if you wanted to, but it's not costly to compute this field and is already stored in the node table, so I wouldn't bother.

When you display a node of the content type containing this field it should now have a link to itself.

Help improve this page

Page status: Not set

You can: