web-dev-qa-db-ja.com

hook_installで新しいコンテンツタイプを作成する

モジュールの hook_install メソッドで新しいコンテンツタイプを作成する方法の適切な例を誰かに見せてもらえますか?

また、混乱を解消するために hook_uninstall の間に何をすべきかを知りたいです。単にコンテンツを削除することについての議論があると私は読んだ。

このタスクを実行する適切な方法の例を見つけることができませんでした。助けていただければ幸いです。

11
Jane Panda

質問に正確に答えるには:

フックインストールでコンテンツタイプを作成するnode_type_save() を使用してコンテンツタイプを作成します。これがWebフォームの例です。インストール:

  // Create the default webform type.
  $webform_type = array(
    'type' => 'webform',
    'name' => st('Webform'),
    'base' => 'node_content',
    'description' => st('Create a new form or questionnaire accessible to users. Submission results and statistics are recorded and accessible to privileged users.'),
    'custom' => TRUE,
    'modified' => TRUE,
    'locked' => FALSE,
  );

  $webform_type = node_type_set_defaults($webform_type);
  node_type_save($webform_type);
  node_add_body_field($webform_type);

hook_uninstallで何をするか:基本的に自分でクリーンアップするので、モジュールが作成した変数を削除します( variable_del() を使用)、モジュールによってアップロードされたファイルを削除します( file_unmanaged_delete_recursive() を使用)、定義されたコンテンツタイプを削除します( node_type_delete() を使用)など。

お役に立てれば!

7
Alex Weber

この情報をコードに保持する最良の方法の1つは、 機能 を使用することです。 Feauresはコードに入れることができます:

  • コンテンツタイプ
  • CCKフィールド
  • 許可
  • 役割

リストは続きます。

機能に関する1つの素晴らしい機能は、drushの統合です

drush featuresは、サイト上のすべての機能とそのステータスのリストを提供します
drush features revert allは、すべての機能をコードの状態に戻します(展開後に実行する場合に非常に便利です)

これが役立つ

11
wiifm

D7 モジュールを確認してください。 node_exampleにはインストールとアンインストールがあります。

インターフェースツールを使用してタイプを具体化し、機能(機能モジュールを使用)を作成し、関連モジュールをカスタムモジュールのインストール機能に抽出することができる場合があります。

3
Drew

一部のフィールドで新しいコンテンツタイプを作成する場合は、以下のコードを使用できます。

このコードは完全に機能します。

function HOOK_install() {

 /* CREATE THE CONTENT TYPE */
 $t = get_t();
 $node_example = array(
    'type' => 'slider',
    'name' => $t('Slider Content'),
    'base' => 'node_content',
    'description' => $t('Add slider content.'),
    'body_label' => $t('Slider Description')
 );
 $content_type = node_type_set_defaults($node_example);

// Create a custom Field with our required field-type.
$field = array(
  'field_slider_images' => array (
    'field_name' => 'field_slider_images',
    'type' => 'image',
  ),
 'field_slider_links' => array (
   'field_name' => 'field_slider_links',
   'type' => 'text',
   'entity_types' => array('node'),
  ),

);
foreach ($field as $fields) {
  field_create_field($fields);
}

// Create a instances of that Field.
$instance = array(
'field_slider_images' => array (
  'field_name' => 'field_slider_images',
  'entity_type' => 'node',
  'bundle' => 'slider',
  'label' => t('Slider Image'),
  'description' => 'Add Slider Image.',
  'settings' => array(
    'file_directory' => 'field/document',
    'file_extensions' => 'png PNG jpg jpeg JPG JPEG',
    'max_filesize' => '10MB',
    'title_field' => '',
  ),
  'widget' => array(
     'type' => 'image_image',
     'weight'=> 10,
  ),
  'formatter' => array(
    'label' => t('label'),
    'format' => 'image'
  ),
  'settings' => array(
    'file_directory' => 'slider-image', // save inside "public://photos"
    'max_filesize' => '4M',
    'preview_image_style' => 'thumbnail',
    'title_field' => TRUE,
    'alt_field' => FALSE,
  )
),
'field_slider_links' => array (
  'field_name' => 'field_slider_links',
  'entity_type' => 'node',
  'bundle' => 'slider',
  'label' => t('Slider Link'),
  'widget' => array('type' => 'text_textfield'),
 ),
);

foreach ($instance as $fieldinstance) {
 field_create_instance($fieldinstance);
}

$status = node_type_save($content_type);
node_add_body_field($content_type);

// Replacement rule for the messages.
$t_args = array('%name' => $content_type->name);
if ($status == SAVED_UPDATED) { // update case
 drupal_set_message($t('The content type %name has been updated.', $t_args));
} 
 elseif ($status == SAVED_NEW) { // create case
   drupal_set_message($t('The content type %name has been added.', $t_args));
   watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l($t('view'), 'admin/structure/types')); 
}

}
0
Sanjay