1

I'm trying to create a field that I can add to content types via my module, here is how I'm doing it:

/* Create the field to hold RSVP info */
function kade_rsvp_enable(){
    if(field_info_field(FIELD_REQUIRE_RSVP)) return; //field already created

    $field = array(
        "field_name" => FIELD_REQUIRE_RSVP,
        'cardinality' => 1,
        'entity_type' => 'node',
        'type' => 'list_boolean',
        'description' => 'Enable RSVP for this event?',
        'label' => 'Enable RSVP',
        'widget' => array(
            'type' => 'options_onoff',
        ),
        'settings' => array(
            'allowed_values' => array(
                '0' => 'No',
                '1' => 'Yes',
            ),
        ),
    );

    field_create_field($field);
}

And the field is getting created when the module is enabled. Here is the var_dump of the field_info_field for it's id:

array(20) {
  ["entity_type"]=>
  string(4) "node"
  ["description"]=>
  string(27) "Enable RSVP for this event?"
  ["label"]=>
  string(11) "Enable RSVP"
  ["widget"]=>
  array(1) {
    ["type"]=>
    string(13) "options_onoff"
  }
  ["settings"]=>
  array(2) {
    ["allowed_values"]=>
    array(2) {
      [0]=>
      string(2) "No"
      [1]=>
      string(3) "Yes"
    }
    ["allowed_values_function"]=>
    string(0) ""
  }
  ["entity_types"]=>
  array(0) {
  }
  ["translatable"]=>
  string(1) "0"
  ["storage"]=>
  array(5) {
    ["type"]=>
    string(17) "field_sql_storage"
    ["settings"]=>
    array(0) {
    }
    ["module"]=>
    string(17) "field_sql_storage"
    ["active"]=>
    string(1) "1"
    ["details"]=>
    array(1) {
      ["sql"]=>
      array(2) {
        ["FIELD_LOAD_CURRENT"]=>
        array(1) {
          ["field_data_field_require_rsvp"]=>
          array(1) {
            ["value"]=>
            string(24) "field_require_rsvp_value"
          }
        }
        ["FIELD_LOAD_REVISION"]=>
        array(1) {
          ["field_revision_field_require_rsvp"]=>
          array(1) {
            ["value"]=>
            string(24) "field_require_rsvp_value"
          }
        }
      }
    }
  }
  ["foreign keys"]=>
  array(0) {
  }
  ["indexes"]=>
  array(1) {
    ["value"]=>
    array(1) {
      [0]=>
      string(5) "value"
    }
  }
  ["id"]=>
  string(2) "23"
  ["field_name"]=>
  string(18) "field_require_rsvp"
  ["type"]=>
  string(12) "list_boolean"
  ["module"]=>
  string(4) "list"
  ["active"]=>
  string(1) "1"
  ["locked"]=>
  string(1) "0"
  ["cardinality"]=>
  string(1) "1"
  ["deleted"]=>
  string(1) "0"
  ["columns"]=>
  array(1) {
    ["value"]=>
    array(2) {
      ["type"]=>
      string(3) "int"
      ["not null"]=>
      bool(false)
    }
  }
  ["bundles"]=>
  array(0) {
  }
}

However, it doesn't show up when I go to add it to a content type

enter image description here

What am I doing wrong?

2
  • It appears this might be a bug, of I create an instance of the field, I can see it now.
    – Malfist
    Aug 28, 2014 at 2:35
  • 1
    it's not a bug. the Add Existing Field means add an additional instance of an existing field instance to a different content type. Your field type should show up under Select Field Type once field_create_field has been run by your hook_enable function. ... Add Existing Field is running field_create_instance() ... get it ?!
    – tenken
    Aug 28, 2014 at 3:40

1 Answer 1

1

You also need field instance to add that field to a content type.

field_create_instance() : Creates an instance of a field, binding it to a bundle.


  // Create field.
  $field_name = 'field_rsvp';
  if (!field_info_field($field_name)) {
    $field = array(
      "field_name" => $field_name,
      'cardinality' => 1,
      'entity_type' => 'node',
      'type' => 'list_boolean',
      'description' => 'Enable RSVP for this event?',
      'label' => 'Enable RSVP',
      'widget' => array(
        'type' => 'options_onoff',
      ),
      'settings' => array(
        'allowed_values' => array(
          '0' => 'No',
          '1' => 'Yes',
        ),
      ),
    );
    field_create_field($field);

    // Create the instance.
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => 'node',
      'bundle' => 'article',
      'label' => 'Enable RSVP',
      'description' => 'Enable RSVP for this event?',
      'required' => TRUE,
    );

    field_create_instance($instance);
  }

If you created a new field say for article content type, then you can see that field for other content types in the Add existing field field as mentioned in the comment by tenken

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.