Drupal Field API

三个重要的概念:

formatters
widgets
field settings form

http://api.drupal.org/api/drupal/modules%21field%21field.api.php/7

Field常用函数

field_info_field_types() //获取添加field 选择的类型

field_views_data()  //获取field数据,此函数在views中

_field_sql_storage_tablename($field_info);  // 根据field信息获取此field的table
_field_sql_storage_revision_tablename($field_info);  //根据field的信息获取此field的revision 表名

field_views_field_default_views_data  //万恶的

Hook:

hook_field_widget_form()  //创建和编辑这个字段时表单的Hook

http://www.thecarneyeffect.co.uk/describe-field-properties-make-custom-fields-work-entity-metadata-wrappers

  foreach(field_info_fields() as $field) {
    if ($field['type'] == 'image') {
      foreach($field['bundles'] as $entity_type => $bundles) {
        foreach($bundles as $bundle) {
          //$bundle_settings = field_bundle_settings($entity_type, $bundle); 
          //field_bundle_settings($entity_type, $bundle, array(
//            'alt_save_replace_token' => 0,
//            'title_save_replace_token' => 1
//          ));
          $field_instance = field_info_instance($entity_type, $field['field_name'], $bundle);
          $field_instance['settings']['alt_save_replace_token'] = 0;
          $field_instance['settings']['alt_save_replace_token'] = 0;
          field_update_field($field_instance);
        }
      }
    }
  }

Entity

Entity 两大概念:
1. Bundles 他相当于entity的扩展,比如一个node. 一个taxonomy
2. Property

Entity 统一的管理数据库操作,留了很多接口. 但是在Drupal7实现不够完美

他提供了下以下功能:

entity_load()
entity_label() 
entity_extract_ids()
entity_uri()
entity_get_info()

核心的entity不具备CRUD功能,所以有了entity api这个模块,他可以帮助我们实现CRUD功能

首先介绍ECK模块,UI下面创建 entity

http://api.drupal.org/api/drupal/includes%21entity.inc/class/DrupalDefaultEntityController/7

Entity 是一个核心功能:
includes/entity.inc
Oop方式实现

实例可参考 examples模块里面的entity_example

Hook_entity_info定义:
http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_entity_info/7

function dy_entity_entity_info() {
  //键是你的entity Name
  $info['dy_entity_form'] = array(
    // A human readable label to identify our entity.
    'label' => t('My Entity Form'),

    //你定义的控制器类名
    'controller class' => 'MyEntityFormController',

    //你的表名
    'base table' => 'my_entity_form',

    // Returns the uri elements of an entity
    'uri callback' => 'dy_entity_form_uri',

    // IF fieldable == FALSE, we can't attach fields.
    'fieldable' => TRUE,
    'entity keys' => array(
        'id' => 'id' , // The 'id' (basic_id here) is the unique id.
    ),
    'bundle keys' => array(
      'bundle' => 'id',
    ),
    'static cache' => TRUE,
    'bundles' => array(
        'first_example_bundle' => array(
          'label' => 'First example bundle',
          // 'admin' key is used by the Field UI to provide field and
          // display UI pages.
          'admin' => array(
            'path' => 'admin/structure/entity_example_basic/manage',
            'access arguments' => array('administer entity_example_basic entities'),
          ),
        ),
      ),
    'view modes' => array(
      'tweaky' => array(
        'label' => t('Tweaky'),
        'custom settings' =>  FALSE,
      ),
    )
  );

  return $info;
}

//定义你的Class,我这里继承的是系统默认的。而不是Entity API提供的
class MyEntityFormController extends DrupalDefaultEntityController {

    public function __construct($entityType) {
        parent::__construct($entityType);
    }

    public function save($entity) {
       //这里如果你的$entity->id 如果存在他会认为你是在更新数据。如果不存在他认为你是添加数据
       $primary_keys = $entity->id ? 'id' : array(); 

       //这个是系统提供的函数。因为没有继承Entity API提供的接口。所以要由自己写增加数据
       drupal_write_record('my_entity_form', $entity, $primary_keys);
    }

    public function create() {
        $entity = new stdClass();
        $entity->id = '';
        $entity->form_name = '';
        $entity->form_data = '';
        $entity->form_value = '';
        return $entity;
    }

}

//调用你定义的entity
$entity = entity_get_controller('dy_entity_form')->create();
$values = 'test';
$entity->form_data = $values['form_data'];
$entity->form_name = $values['form_name'];

//调用你定义的dy_entity_form中的sava函数
entity_get_controller('dy_entity_form')->save($entity);

//你可以打印出你当前的所有的Entity. entity_get_info他可以获取系统全部定义的entity  Bundles
dpm(entity_get_info())

$result = entity_load('dy_entity_form');  //获取我的所有数据
$result = entity_load('dy_entity_form',array(5));  //获取id为5的数据

Drupal Database API

查询的API:

http://api.drupal.org/api/drupal/includes!database!select.inc/class/SelectQuery/7

 
DB类:
http://api.drupal.org/api/drupal/includes!database!database.inc/group/database/7
 

Develop Database API:

http://drupal.org/developing/api/database

 
Schema:
http://drupal.org/node/146843

http://drupal.org/node/150223
Data Type: http://drupal.org/node/159605

API: http://api.drupal.org/api/drupal/includes%21database%21schema.inc/group/schemaapi/7

常用函数:

drupal_write_record('tablename','filed_array', $primary_keys) //添加数据, $primary_keys如果是空表示是添加数据,如果有字段。按字段条件更新数据
drupal_get_schema($table);    //根据表名获取这个表里面的所有字段

Drupal Menu/菜单 Api

Menu 总共有三个数据表:

menu_links : Link的些详细信息比如link标题。选项. 分类

menu_custom : 这个是菜单列表

menu_router : Link的路由情况. 比如到了这个页面要执行什么function.

—————————————————————–

下面是如何让每次都加载你的menu,而没有缓存:

function hook_init () { 
 menu_rebuild();
}

路由解析(menu_get_item)这个函数是查询menu_route表,获取路由信息

menu_execute_active_handler 这个函数是执行路由

Menu 的生成

menu生成函数: menu_rebuild();这个函数会重建你数据库中所有保存的记录 .一般在清空缓存后都会执行这个函数
这个函数有这些操作:

module_implements('menu')   #执行所有hook_menu,menu_router_build函数中.把所有数组保存于$callbacks中
drupal_alter('menu', $callbacks); #执行调用所有 hook_menu_alter($callbacks); 将$callbacks这个数组传过去过滤
_menu_router_build #执行全部的路由关系重建
_menu_router_save  #保存到数据库
_menu_navigation_links_rebuild  #更新menu_links
menu_cache_clear_all  #清空cache_menu缓存
_menu_clear_page_cache #清空页面缓存

Hook_menu 这个hook判断如何处理请求的URL
http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_menu/7

Function
http://api.drupal.org/api/drupal/includes%21menu.inc/7

Entity Api

这个模块是核心entity 的扩展,以提供统一的方式来处理entity和属性。它提供了一个实体的CRUD控制器,这有助于简化创建新的实体类型。

核心的entity只提供了下以下功能,并不具备CRUD功能

entity_load()
entity_label() 
entity_extract_ids()
entity_uri()
entity_get_info()

entity api提供的功能:

entity_id()
entity_export()
entity_import()
entity_save()
entity_create()
entity_delete()
entity_view()
entity_access()
entity_get_property_info()

http://drupal.org/project/entity

教程: http://drupal.org/node/878784

http://www.trellon.com/content/blog/creating-own-entities-entity-api

http://www.cyber-sundae.com/create-drupal-entity-based-custom-table-and-create-drupal-view-based-entity-table-including-date

Entity Metadata Wrapper:
http://drupal.org/node/1021556
http://drupalcontrib.org/api/drupal/contributions!entity!entity.module/function/entity_metadata_wrapper/7
http://pixeljets.com/blog/writing-robust-code-uses-fields-drupal-7

What is the purpose of the entity_metadata_wrapper() function, and why should I use it?
http://drupal.stackexchange.com/questions/33444/what-is-the-purpose-of-the-entity-metadata-wrapper-function-and-why-should-i

Metadata, what for? – Introducing Entity Metadata!
http://wolfgangziegler.net/introducing_entity_metadata

http://drupalcontrib.org/api/drupal/contributions!entity!includes!entity.wrapper.inc/class/EntityValueWrapper/7

http://drupalcontrib.org/api/drupal/7

$entity_id = 1;
$entity_type = 'node';
$entities = entity_load($entity_type, array($entity_id));
$wrapper = entity_metadata_wrapper($entity_type, array_shift($entities));
dpm($wrapper->getPropertyInfo())  //获取属性

Entity 创建数据, 这些数据是使用eck来创建的bundle.:
entity type: point_reward
bundle: point_code
下面的代码是为 point_code 添加数据, field_com_point_code和field_com_point分别为 point_code的字段

$point_code_entity = entity_create('point_reward', array('type'=>'point_code'));
$wrapper = entity_metadata_wrapper('point_reward', $point_code_entity);
$wrapper->field_com_point_code->set(sigvaris_unique());
$wrapper->field_com_point->set($point);
$wrapper->save();

更新Entity 数据:
使用的是 EntityFieldQuery


  /**
   * 查询一条数据,根据用户输入的Code
   * @see http://drupal.org/node/1343708
   */
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'point_reward', '=')  //先查询entity type 为point_reward
    ->entityCondition('bundle', 'point_code')              //bundle 为 point_code
    ->fieldCondition('field_com_point_code', 'value', trim($code), '=');  //字段的值比较
  
  $result = $query->execute();

  /**
   * 更新数据, 将结果进行数据更新
   */
  $wrapper = entity_metadata_wrapper('point_reward', reset($result['point_reward']));
  $wrapper->field_use_user->set(1);    //set() 设置值
  $wrapper->field_use_time->set(mktime());
  $wrapper->save();                   //保存结果
  
  
  //打印数据, value() 获取值, 
  $wrapper->field_use_user->value();

 //使用entity_load 函数来获取数据, 这样返回出来是一个数组
  $entity = entity_load('point_reward', (array)reset($result['point_reward']));
  

Entity 获取今天的数据

//因为entity默认不支持 Mysql date格式化函数,所以我们只有采用between来获取, 

//计算出今天的开始和结束的时间戳
$start_time = strtotime(date('Y-m-d'));
$end_time = strtotime('+1 day', $start_time);

$query = new EntityFieldQuery();
$query
  ->entityCondition('entity_type', 'activity_log', '=')   /先查询entity type 为activity_log
  ->entityCondition('bundle', 'shake')                  //bundle  为 shake
  ->propertyCondition('created', array($start_time, $end_time), 'BETWEEN');  //字段过滤

$result = $query->count()->execute();

查询并删除一些数据

    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'shake', '=')
      ->entityCondition('bundle', 'shake')
      ->entityCondition('uid', '0', '=');

    $query_results = $query->execute();
    foreach (array_pop($query_results) as $query_result) {
      $wrapper = entity_metadata_wrapper('shake', $query_result);
      $wrapper->delete();
    }