Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to use Drupal 7 Entities and Field API

I'm trying to use Drupal 7's entities and field API to correctly build a new module. What I have been unable to understand from the documentation is the correct way to use the new API to create a 'content type' (not a node type) with a number of set fields, such as Body.

I'm trying to set up the entity using hook_entity_info, then I believe I need to add the body field using field_create_instance, but I can't seem to get it to work.

In mycontenttype.module:

/**
 * Implements hook_entity_info().
 */
function mycontenttype_entity_info() {
  $return = array(
    'mycontenttype' => array(
      'label' => t('My Content Type'),
      'controller class' => 'MyContentTypeEntityController',
      'base table' => 'content_type',
      'uri callback' => 'content_type_uri',
      'entity keys' => array(
        'id' => 'cid',
        'label' => 'title',
      ),
      'bundles' => array(
        'mycontenttype' => array(
          'label' => 'My Content Type',
          'admin' => array(
            'path' => 'admin/contenttype',
            'access arguments' => array('administer contenttype'),
          ),
        ),
      ),
      'fieldable' => true,
    ),
  );
  return $return;
}

/**
 * Implements hook_field_extra_fields().
 */
function mycontenttype_field_extra_fields() {
  $return['mycontenttype']['mycontenttype'] = array(
    'form' => array(
      'body' => array(
        'label' => 'Body',
        'description' => t('Body content'),
        'weight' => 0,
      ),
    ),
  );
  return $return;
} 

Then does this go in the .install file?

function mycontenttype_install() {
  $field = array(
    'field_name' => 'body',
    'type' => 'text_with_summary',
    'entity_types' => array('survey'),
    'translatable' => TRUE,
  );
  field_create_field($field);

  $instance = array(
    'entity_type' => 'mycontenttype',
    'field_name' => 'body',
    'bundle' => 'mycontenttype',
    'label' => 'Body',
    'widget_type' => 'text_textarea_with_summary',
    'settings' => array('display_summary' => TRUE),
    'display' => array(
      'default' => array(
        'label' => 'hidden',
        'type' => 'text_default',
      ),
      'teaser' => array(
        'label' => 'hidden',
        'type' => 'text_summary_or_trimmed',
      ),
    ),
  );
  field_create_instance($instance);
}
like image 245
Martin Petts Avatar asked Jan 15 '11 16:01

Martin Petts


People also ask

What is entity and entity API?

The Entity System is the API for entities manipulation (CRUD: create, read, update, delete). Entity validation has its own API (which could validate an Entity saved via REST, rather than a form, for example).

Which API is used by Drupal CMS for handling field types?

Plugin API The Plugins API is used to extend Drupal and add new functionality, and allow modules to define new plugin types.

What is the entity type field used for?

A special ChoiceType field that's designed to load options from a Doctrine entity. For example, if you have a Category entity, you could use this field to display a select field of all, or some, of the Category objects from the database.

What are entities in Drupal?

In Drupal, entity is a general concept that represents a noun (person, place or thing). Out of the box, there are a number of different types of entities in Drupal, each meant to represent a specific type of data. One type of entity is a user.


3 Answers

I think your problem is that if node module is installed, there is already a field named 'body'. You should either re-name your field to something like 'mycontenttype_body' (comment.module uses comment_body), or re-use the 'body' field and skip the adding the field part and skip to adding the instance of it. The former is recommended over the latter.

like image 71
Dave Reid Avatar answered Oct 06 '22 00:10

Dave Reid


Every field has an array property, entity_types, which limits the entities to which the field can be attached. The best Drupal solution I can find, hook_field_create_field, can alter fields as they are created, but that's no good for the body field which is created on installation. So my solution is just to edit the database directly in my hook_install

  $data_col = db_query("SELECT data from field_config where field_name = 'body'")->fetchAssoc();
  $data = unserialize($data_col['data']);
  $data['entity_types'][] = 'MY_ENTITY_TYPE';
  db_update('field_config')
    ->fields(array('data' => array('data' => serialize($data))))
    ->condition('field_name', 'body')
    ->execute();
like image 21
Matthew Slater Avatar answered Oct 06 '22 00:10

Matthew Slater


just started down the same path here is a video from fago

like image 38
Lathan Britz Avatar answered Oct 05 '22 22:10

Lathan Britz