2011-01-15 4 views
6

나는 새로운 모듈을 올바르게 빌드하기 위해 Drupal 7의 엔티티 및 필드 API를 사용하려고합니다. 문서에서 이해할 수 없었던 것은 새로운 API를 사용하여 Body와 같은 많은 수의 설정 필드가있는 '콘텐츠 유형'(노드 유형 아님)을 만드는 올바른 방법입니다.Drupal 7 엔티티 및 필드 API를 사용하는 올바른 방법

hook_entity_info를 사용하여 엔티티를 설정하려고하는데 field_create_instance를 사용하여 본문 필드를 추가해야한다고 생각합니다. 그러나 작동시키지 못합니다. 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; 
} 

다음이는 .install 파일에 갈 않습니다

?

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); 
} 
+0

이 블로그 게시물을 할 수있다 유용하다. http://www.istos.it/blog/drupal-entities/drupal-entities-part-3-programming-hello-drupal-entity – bkildow

답변

1

노드 모듈을 설치하면 이미 'body'라는 필드가있는 것이 문제라고 생각합니다. 필드의 이름을 'mycontenttype_body'(comment.module은 comment_body를 사용)로 다시 지정하거나 'body'필드를 다시 사용하고 필드 부분 추가를 건너 뛰고 인스턴스 인스턴스를 건너 뛰도록해야합니다. 전자는 후자에 비해 권장됩니다.

+0

네, 그게 내가하고 싶은 일입니다. 노드에서 바디 필드를 재사용하십시오. . 그래서 hook_install에서 인스턴스를 추가하는 것이 옳은가요? 그것은 작동하지 않는 것 같습니다. field_attach_form()을 사용하여 본문 필드가 양식에 자동으로 추가되는 것을 볼 수 있습니까? –

1

모든 필드에는 필드를 연결할 수있는 엔터티를 제한하는 배열 속성 인 entity_types가 있습니다. 내가 찾을 수있는 최고의 Drupal 솔루션 인 hook_field_create_field는 필드가 생성 될 때이를 변경할 수 있지만 설치시 생성되는 본문 필드에는 적합하지 않습니다. 그래서 내 솔루션은

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

그냥 여기 같은 경로가 좋은 REPO가 시작이야 여기 video from fago

0

입니다 다운 시작 내 hook_install에 데이터베이스를 직접 편집하는 것입니다 : Lawmakers entity