Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8156831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:04:43+00:00 2026-06-06T17:04:43+00:00

I need to programmatically set up a node reference field. My module successfully creates

  • 0

I need to programmatically set up a node reference field.

My module successfully creates a and associates a pair of ‘CCK’ fields to my nodes. One of these fields is a node_reference field. My code is a follows:

$field_ref_name = 'field_custom_reference';
$field = field_info_field($field_ref_name);

if (empty($field)) {
    $field = array(
        "field_name"=>$field_ref_name,
        "label"=>"Custom Reference",
        "type"=>"node_reference",
        "cardinality"=>"1",
        'locked' => TRUE,
    );
    field_create_field($field);
}

$instance = array(
    "field_name"=>$field_ref_name,
    "label"=>"Sequence Reference",
    "type"=>"node_reference",
    "widget"=>array(
        "type"=>"node_reference_autocomplete"
    ),
    "description" => "text describing purpose of this field",
);

$instance["entity_type"] = "node";
$instance["bundle"] = $type;

if( !in_array($type, $field['bundles']['node']) )
    field_create_instance($instance);

Now, the code works but when I edit a node inputting a valid value into the node reference field and attempt to save, I get the following error:

…: this post can’t be referenced.

I realized the reason for the error is because the node reference field settings does not have any selected nodes as "Content types that can be referenced".

How can I adjust my code to set referenceable content types?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:04:44+00:00Added an answer on June 6, 2026 at 5:04 pm

    The reference-able types is a field setting. So it should be put under a “settings” array in the field definition. Something like –

    $field = array(
            "field_name"=>$field_ref_name,
            "label"=>"Custom Reference",
            "type"=>"node_reference",
            "cardinality"=>"1",
            'locked' => TRUE,
            'settings' => array(
              'referenceable_types' => array('article'),
            ),
        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to set the height for a single UITableViewCell in my UITableView programmatically.
I'm programmatically creating a folder using C# and need to set the default view
I would really need to programmatically click on all nodes in the collection, but
I need to programmatically set a cell in editing mode. I know that setting
I need to programmatically add a set of controls with some amount of pixels
I need set the date/time from Android programmatically, but I'm not having success! I
I am building a VPN application where I need to set DNS server programmatically.
I have a situation where I need to programmatically set something that has a
I'm using Apache.NMS 1.4. I need to be able to programmatically set the maximum
How to assign programmatically to SeekBar discrete set of values ? I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.