Drupal 自己定义主题实体 Theming Custom Entities

在自己定义主题中输出结果时,有三个部分或很多其它特殊的函数。如 hook_menu,Page Callback。MODULE_theme 钩子

1、hook_menu

为了使用自己定义的实体。像创建、编辑、删除、查看实体的功能,就必须要创建一些 Menu path。这里创建、编辑、删除是与Drupal's Form API相关的,通过hook_menu,能够定义我们须要的路径来訪问这个新创建的实体内容

function my_module_menu() {

  $items['my_entity/%my_entity'] = array(

    'title callback'   => 'my_entity_page_title',

    'title arguments'  => array(1),

    'page callback'    => 'my_entity_page_view',

    'page arguments'   => array(1),

    'access arguments' => array('view entities'),

    'type'             => MENU_CALLBACK,

  );

  return $items;

}

2、Page Callback

在上面的样例中。我们在訪问这个路径时,定义了 page callback 相应的 my_entity_page_view 函数,因此,接下来就须要创建这个函数,例如以下

/**

 * This is the callback we defined to be executed when a user

 * requests http://mysite.com/my_entity/1 (1 is just an example ID,

 * it could be anything). This function will set up the data and

 * prepare the render array(s). You will specify the template to

 * use in this callback. The critical thing to note below is the

 * order in which field_attach_prepare_view, entity_prepare_view

 * and field_attach_view are called. These functions must be called

 * in this order and they must be called before you specify which

 * theme to use.

 */

function my_entity_page_view($entity, $view_mode='full') {

  $entity_type = $entity->entityType();

  $entity_id = entity_id($entity_type, $entity);

  //

  // Remove previously built content, if exists

  //

  $entity->content = array();

  $entity->title = filter_xss($entity->title);

  //

  // Build the fields content

  //

  field_attach_prepare_view($entity_type, array($entity_id => $entity), $view_mode);

  entity_prepare_view($entity_type, array($entity_id => $entity));

  $entity->content += field_attach_view($entity_type, $entity, $view_mode);

   // Specify the theme to use and set the #element. Note that the key

   // you use to pass the entity object must match the key you set in the

   // variables in my_module_theme(). So in the case below, we use the key

   // named #element because in my_module_theme() we set the following code:

   //

   // array(

   //   'my_entity' => array(

   //     'variables' => array('element' => null),

   //     'template' => 'my_entity'

   //   ),

   // );

   //

  $entity->content += array(

    '#theme'     => $entity_type,

    '#element'   => $entity,

    '#view_mode' => $view_mode,

    '#language'  => LANGUAGE_NONE,

  );

  return $entity->content;

}

3、MODULE_theme() Hook

到眼下为止,为了这个实体我们已经定义了菜单项还有CALL BACK返回值,接下来。剩下的就须要创建一个指向模板的文件。看上面部分内容,能够看到内容为:

$entity->content += array(

  '#theme' => 'my_entity'

);

意思是说。指向 my_entity ,那么,应该怎样定义呢?
function my_module_theme($existing, $type, $theme, $path) {

  return array(

    'my_entity' => array(

      'variables' => array('element' => null),

      'template' => 'my_entity_template'

    ),

  );

}

4、依据第三部分的内容。我们则须要创建名为 my_entity_template.tpl.php 的模板文件

[php

  // In a real module variables should be manipulated in a preprocess function.

  $content = $element->content;

]
<div class="[php print $classes; ]">
[php print render($content['title']); ]
[php print render($content['field_date']); ]
 [php print render($content['field_author']);]
 [php print render($content['field_image']);]
[php print render($content['field_description']);]

原文链接:https://drupal.org/node/1238606

转载于:https://www.cnblogs.com/blfbuaa/p/7080092.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值