Correct way to use Drupal 7 Entities and Field API

11,718

Solution 1

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

Solution 2

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();

Solution 3

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.

Share:
11,718

Related videos on Youtube

Martin Petts
Author by

Martin Petts

Updated on June 04, 2022

Comments

  • Martin Petts
    Martin Petts over 1 year

    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);
    }
    
  • Martin Petts
    Martin Petts almost 13 years
    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()?

Related