Login

Drupal 7 Adding Custom Content Type with Custom Fields (Field API)

In this post we are going to use the custom field "news_date" described in the previous post "Drupal 7 Field API - A simple example".

The module in this post automatically installs a content type with two instances of our "news_date" field (begin and end). 

First a little background:

Drupal 7 introduces the  concept of "entities"  A "node" is a specialization of an "entity". Content types are a further specialization of "node".

An entity can be defined as "fieldable" (which node is).  Thus, "content types" are also "fieldable" (i.e. we can add fields).

Our new module is called "newletter". Here is the "newletter.info" file.

name = News Letter
description = news letter for Drupal 7
dependencies[] = news_date
package = public-action newsletter
version = VERSION
core = 7.x
files[] = newsletter.module
files[] = newsletter.install

Note! We set our "news_date" module as a dependency. We don't want to try and install our new content type without the required field (i.e. news_date) installed.

Next our "newletter.install" file. Just a few simple arrays and method calls:

<?php
function newsletter_install() {
 
  // Ensure the forum node type is available.
  node_types_rebuild();
  $types = node_type_get_types();
  node_add_body_field($types['newsletter']);
 
  foreach (_newsletter_installed_fields() as $field) {
    field_create_field($field);
  }
 
  foreach (_newsletter_installed_instances() as $instance) {
    field_create_instance($instance);
  }
 
}
 
function _newsletter_installed_fields() {
  $t = get_t();
  return array(
    'field_start' => array(
      'field_name' => 'field_start',
      'cardinality' => 1,
      'type'        => 'news_date',
    ),
    'field_end' => array(
 
      'field_name' => 'field_end',
      'cardinality' => 1,
      'type'        => 'news_date',
    ),
   );
 
}
function _newsletter_installed_instances() {
  $t = get_t();
  return array( 
   'field_start' => array(
      'field_name' => 'field_start',
      'label'       => $t('The Start Date.'),
      'bundle' => 'newsletter',
      'entity_type' => 'node',
      'widget'      => array(
        'type'    => 'news_date',
      ),
      'display' => array(
        'default' => array(
          'label' => 'above',
          'type' => 'news_date_formatter',
        ),
      ),
      'description' => 'Begin Date',
    ),
   'field_end' => array(
 
      'field_name' => 'field_end',
      'label'       => $t('The End Date.'),
      'bundle' => 'newsletter',
      'entity_type' => 'node',
      'widget'      => array(
        'type'    => 'news_date',
      ),
      'display' => array(
        'default' => array(
          'label' => 'above',
          'type' => 'news_date_formatter',
        ),
      ),
     'description' => 'End Date',
    ),
 
  );
}
 
function newsletter_uninstall() {
 
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'newsletter'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }
 
  // Delete all the nodes at once
  node_delete_multiple($nids);
 
  $instance = array('field_name' => 'comment_body','bundle' => 'comment_node_newsletter','entity_type' => 'comment');
  field_delete_instance($instance);
 
  $instance = array('field_name' => 'body','bundle' => 'newsletter','entity_type' => 'node');
  field_delete_instance($instance);
 
   $fields = array_keys(_newsletter_installed_fields());
   foreach ($fields as $field) {
      field_delete_field($field);
  }
 
  // Purge all field infromation
   field_purge_batch(1000);
}

To summarize, we are performing the steps normally done manually through the administration screens (i.e. adding and configuring fields with our content type). Note! Our entity type is "node" and our content type is referred to as a bundle "newletter".

In Drupal 7 you must explicitly add a "body" field when you create a custom content type, thus the call to node_add_body_field($types['newsletter']);.

Our actual module file only needs to do a couple things. Define the "content type" by implementing hook_node_info(). Implement hook_form() to make sure our "title" is rendered in the input form.

<?php
 
function newsletter_node_info() {
  return array(
      'newsletter' => array(
          'name' => t('News Letter'),
          'base' => 'newsletter',
          'description' => t('A news letter content type'),
          'has_title' => TRUE,
          'title_label' => t('Newsletter Title'),
          'locked' => FALSE,
      ),
  );
}
 
function newsletter_form($node, $form_state) {
  $type = node_type_get_type($node);
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#default_value' => !empty($node->title) ? $node->title : '',
    '#required' => TRUE, '#weight' => -5
  );
 
  return $form;
}

Comments

よく介護と福祉の保険料をされて

あなたはほぼ確実に効果的にすべてを知っているように、医療や健康保険率の値は、実質的に以前に一般的なインフレの電荷を上記金額で増加し続ける。これらの特別費用の増加のために提供さ要因はいくつかありますし、 シューズ ランニング 組み込む:医療分野における技術開発、健康関連のソリューションや処方薬の上昇需要、住民の高齢化が進ん、価格保険と政府の償還価格に起因するシフト状態をと連邦政府の権限、および健康関連に関連する訴訟に関連する費用。

個々の客として、我々は医療の価値に貢献する側面のいくつかの上にほとんど制御することができます。しかし、私たちのすべては、生活習慣健康保険の約束についてのコントロールを持っています。 nike シューズ ランニング 日常の運動の許容量を見つけること、バランスのとれたダイエットプログラムを摂取毎年健康診断や他の提案され、プログラムの処理に参加し、酒の摂取量を制限し、たばこの品目の使用を除去する基本的な式は、間違いなく私たちの個々の医療費を軽減しません。医療費を減少させることに加えて、式のその後のこの種の他の肯定的な側面は、よりはるかに少ない活力、自己自信、ストレス、増加した生産性が組み込まれています。そのように現在実行されていない場合、私はあなたとあなたの家族のために栄養価の高いライフスタイルを市場に考慮賢明な手法を取ること刺激する。ヘルスクラブのメンバーシップの年間料金の25%を相殺する選択をしていると、たとえば、個々の一つは、よく保険会社である私の組織が実行されます。簡単なことは、これらのワンダ、自転車旅行を取るように種類の、あるいは実際に行く水泳には、 ナイキ ショックス 2つの健康な人間の身体と脳をアドバタイズします。あなたは甘い歯を持っている場合、一週間ほど早くかかる砂漠に自分で制限を意図している。あなたは、はるかにそれをお楽しみいただけますし、あなたの全体の体はあなたに感謝します。

健康保険の保険料は、長い間医療の費用が上がるように続けて増加に進みます。ヘルスケアのトータルコストを削減するための最良の方法は、医療のための私達のシングルクォを減少させることです。 ナイキエアマックス 健康的な生活スタイルの選択と医療制度の慎重な使用は、我々の医療費をの世話を取得する最大かつ簡単な技術である。多分栄養価の高いライフスタイルの最高の報酬は最大限に地球上でここで私たちの貴重な時間に喜びを取るために私たちの可能性があります。