Correct way to use Drupal 7 Entities and Field API
Asked Answered
A

4

6

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);
}
Abreaction answered 15/1, 2011 at 16:13 Comment(1)
This blog post may be useful: istos.it/blog/drupal-entities/…Colloquium
W
1

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.

Wormhole answered 16/1, 2011 at 6:42 Comment(1)
Yes, that's what I want to do - reuse the body field from nodes. So am I right to add the instance of it in hook_install? It doesn't seem to work. Should I expect to see the body field automatically added to forms using field_attach_form()?Abreaction
S
1

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();
Samara answered 12/3, 2011 at 17:1 Comment(0)
T
1

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

Tennison answered 24/3, 2011 at 19:17 Comment(0)
R
0

Here's a nice repo to start: Lawmakers entity

Rafflesia answered 29/7, 2014 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.