Field API, Field Attach API, Field Instance API, Field CRUD API OR Schema API?


Personally I wouldn't bother doing it yourself, Drupal has it all set up for you and it's just a matter of working out the format it wants data in and you can use all the goodness of the entity/field system.

The following function is one I use all the time. Given an existing entity type, field name and bundle it will produce the code you need to run to create said field instance:

function __export_field($entity_type, $field_name, $bundle_name) {
  $info_config = field_info_field($field_name);
  $info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
  unset($info_instance['id'], $info_instance['field_id']);
  include_once DRUPAL_ROOT . '/includes/';
  $output = "field_create_field(" . drupal_var_export($info_config) . ");\n";
  $output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";
  drupal_set_message("<textarea rows=30 style=\"width: 100%;\">". $output .'</textarea>');

For example, if you have a field called field_image attached to the standard article content type, you would export both the field creation code, and field instance creation code with this:

__export_field('node', 'field_image', 'article');

The code prints out to the standard drupal message area and can just be copied and pasted to wherever you need it.

For your specific needs, the value list for a select widget is added in the settings part of the field array, under allowed_values:

$field = array(
  'translatable' => '0',
  'entity_types' => array(),
  'settings' => array(
    'allowed_values' => array(
      1 => 'First Choice',
      2 => 'Second Choice'
  // etc...

If you haven't seen it already check out the Examples module, there's loads of example code in there to help with entity/field manipulation.

Tags: Entities

Similar questions

Cannot change an existing field's type. in field_update_field() line 240 of
My instance of drupal is throwing an error. The file that is throwing the error is modules/field/ The error is FieldException: Cannot change an existing field's type. in field_update_field() (line 240 of modules/field/ A local var dump displays the following results. Variable $field Variable $prior_field
CRUD operations with custom fields
I am using Services 3.x with Drupal 7 and trying to expose a custom field added to the User object through the services. What is the easiest way to implement it for all operations (create, retrieve, update, delete)?
How can I create a simple non-CRUD resource action with Drupal 7 / Services 3?
The resource "username" appears but my action does not appear in the list in services admin. Response to a POST is HTTP/1.1 404 Not found: Could not find resource username. I replaced my module name with "module" here:
How Do I Create a User Role that Can Only CRUD a Single Content-type?
OK, so I know how to create roles and messed around with the People->Permissions->Roles screen. My goal is to create a single user role that will have only one function -- to administer the calendar events for the client site. The first thing I did was to create a view to act as the overview of the available content. I added in the Login Destinatio...
How to automate database table access as objects and provide common operations, like CRUD and views integraion?
I think this is a pretty normal use-case: Developer has a database. Tables and columns have meaningful names and helpful comments, possibly also sane set of relations. Now he needs a module to be build on it. Is there any automated way to provide CRUD and views integration, either dynamically or by creating a module backbone for developers to edit?...
What's the easiest way to create CRUD interface for custom entities?
What is the fastest way to create admin UI for new entity type? Like at content types. I just need basic list/add/edit/delete actions. One would think this comes with entity api? I have found a lot of solutions but all of them included a lot of custom code, which is prone to errors. is DrupalDefaultUIEntityController the answer? how can one use it?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.