快轉到主要內容

drupal中获取 entity reference field 的 node type 值

·421 字·1 分鐘
Y Cheung
作者
Y Cheung
Blogger, Programer & Traveler.

标题很拗口我知道……

用过 entity reference 模组的朋友都知道,这个模组(module)能让你在节点(node)中添加一个 Entity Reference 的字段(Field),可以引用其他的实体(entity)。此次添加引用的是节点。根据需求,须输出节点类型(node type),但是 entity reference 的字段只能输出被引用实体的ID(本例是 nid),所以……

解决这个问题的思路是通过被引用节点的ID反查询它的节点类型然后输出到新的字段。但这里面有个问题就是当表单提交以后才能够获取到这个ID,所以实际上是要再编辑这个node的时候才能查询出这个node type。

代码如下:

 1  function entityreference_nodetype_form_alter(&$form, $form_state, $form_id) {
 2      if($form_id == 'test_1_node_form'){  //选择表单id
 3          $form['refer_node_type']['und']['#prefix'] = '<div id="refer-node-type-add-more-wrapper" style="display:none;">'; //为防止编辑者不慎改动这个nodetype值,将其设为不可见
 4          if(!empty($form['field_refer']['und'][0])){		//entity reference field 不为空
 5              $type = node_load($form['field_refer']['und'][0]['target_id']['#value'])->type; //查询该nid的node type
 6              if($form['refer_node_type']['und'][0]['value']['#default_value'] != $type){
 7                  $form['refer_node_type']['und'][0]['value']['#default_value'] = $type;    //设置字段值为千米那获取到的内容类型
 8                  drupal_set_message('內容類型設置完成,請點擊保存按鈕!');
 9              }
10          }
11      }
12  }

相關文章

Wordpress默认相册与文字内容分开显示及定制显示格式

·1319 字·3 分鐘
最近工作上要重新建站,公司有些站点是ASP的,程序老旧经常被黑。因为就是一般的企业展示类站点,就直接用wordpress重建了,页面样式还是用原来的,工作重点就是如何output出跟旧站一样的html结构。得益于这大半年的几个drupal项目,在做的时候思路跟以前不同了,不得不说drupal的核心思维还是很赞很科学的。

Wordpress在循环LOOP中获取 post_date 使用自定义日期时间格式

·146 字·1 分鐘
code示例: 1<?php $args = array('numberposts' => '16','category' => '8'); 2 $recent_posts = wp_get_recent_posts( $args, ARRAY_A ); 3 foreach ($recent_posts as $recent) { 4 echo '<li><a href="'. get_permalink($recent["ID"]).'">'.$recent["post_title"].'</a><span class="list-meta">'. mysql2date('Y-m-d', $recent["post_date"]) .'</span></li>'; 5 } 6?> 原本直接用$recent['post_date']输出的是2014-11-11 14:30:24这样的格式,但现在不需要时间的输出,Y.Cheung 只需要输出日期,找了一下用mysql2date函数就可以。

獲取最新文章並附特色圖片的代碼

·250 字·1 分鐘
幫朋友寫的,順便放在這裡mark一下好了。 以下代碼實現獲取最新10篇日誌及其100×100像素的特色圖片。 1<?php query_posts('posts_per_page=10'); if (have_posts()) : while (have_posts()) : the_post(); ?> 2 3<?php if (has_post_thumbnail()){the_post_thumbnail(array(100,100)); } ?> <?php the_title(); ?> 4 5<?php echo get_the_date(); ?> 6 7<?php endwhile; endif;?> COMMENTS (7) # 老武 · 2012-03-26T10:42:20+08:00

WordPress宕机一日

·616 字·2 分鐘
昨天突然无法访问我的blog了,还以为我又折腾什么出错了,却发现连服务器都进不去了。大件事啦! 联系了hugege(我在他那里租的主机),说是到国内的线路抽风喔。于是,我又定了定神,没事,等它抽完了就好了。果然,晚上的时候就恢复正常了,额,也不算太正常,时不时还是抽一下,不过,能访问就好了。(^U^)ノ~YO