我们在创建系列Drupal网站的时候,都是用Drush脚本,其中运行PHP程序调用field_create_instance来创建新的字段。
现在需要将已经创建好的字段进行一些设置修改,例如在teaser模式显示一部分字段内容,以后需要手工在网站菜单中操作,系列网站多的时候很麻烦,今天在网上查了一下资料,可以实现程序批量处理,具体PHP程序代码如下:
# change content type fields' teaser display
drush ev '$instance=field_info_instance("node", "field_city", "poi");$instance["display"]["teaser"]["type"]="taxonomy_term_reference_link";$instance["display"]["teaser"]["module"]="taxonomy";field_update_instance($instance);'
drush ev '$instance=field_info_instance("node", "field_address", "poi");$instance["display"]["teaser"]["type"]="text_default";$instance["display"]["teaser"]["module"]="text";field_update_instance($instance);'
上面是在sh脚本中使用drush命令来运行两小段php代码,分别修改一个分类属于字段和一个纯文本字段的teaser显示。当然也可以不在drush脚本中,直接用php程序来执行。
参考信息:
- how to update a field created with field_create_instance
- field_create_instance
- field_info_instance
- field_update_instance
2014-10-9补充:如果需要修改“字段设置”中“值的数量”cardinality等其它设置,可以这样:
drush ev '$field=field_info_field("field_category");$field["cardinality"]=-1;field_update_field($field);'
参考信息:
2016-4-13补充:中文网站留言的地方如果提示是“Comment”就不太友好,可以修改为“更好”,这时需要修改“field_config_instance”表中“comment_body”字段中“label”的值,可以运行:
drush ev '$field=field_info_instance("comment","comment_body","comment_node_hangye");$field["label"]="评论";field_update_instance($field);'
其中hangye是内容类型的机器码。
2016-6-28补充:修改所有内容类型的每个字段的显示,将default标签里面的设置内容复制到drupalgap标签里面去,逐个内容类型、逐个字段设置起来太麻烦,可以用两个循环:外面一个内容类型的循环,里面一个字段的循环,就可以方便地全部执行,代码如下:
drush ev '
$types = node_type_get_types();
foreach ($types as $type => $value) {
print "type = $type\n";
$fields = field_info_instances('node', $type);
//print_r($fields);
foreach ($fields as $field => $instance) {
print " $field,";
$instance["display"]["drupalgap"] = $instance["display"]["default"];
field_update_instance($instance);
}
print "\n";
}
'
以上代码是用在《以Drupal网站为基础创建App》中批量修改系列站点的设置。
评论6
能示例一下PHP 的代码吗
能示例一下PHP 的代码吗上面已经是php代码了
只需要执行很简单的几行php代码就可以了,用drush的方式可以免除编写一个完整的php程序,如果不用drush、也不编写完整php程序的话,你可以编辑一个允许php代码的页面,嵌入这几句php代码就可以,如果编写一个完整php程序也不难、也不长,只有开头调用一些bootstrap等,类似这样:
<?php
$_SERVER['HTTP_HOST'] = 'www.example.com';
$_SERVER['SCRIPT_NAME'] = '/sample.php';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$drupal_path = '/usr/local/apache2/htdocs/www.example.com/';
chdir($drupal_path);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
......
大神能给个完整点的范例吗。php不太懂
大神能给个完整点的范例吗。php不太懂我可不是大神,也是自己一点点摸索的
我在搞Drupal之前也没有了解PHP程序,后来被迫自学的一点,程序都不讲规范,对付着凑合用。下面这个算是完整例子:
<?php $_SERVER['HTTP_HOST'] = 'www.example.com'; $_SERVER['SCRIPT_NAME'] = '/sample.php'; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $drupal_path = '/usr/local/apache2/htdocs/www.example.com/'; chdir($drupal_path); require_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); # change content type fields' teaser display $instance = field_info_instance("node", "field_city", "poi"); $instance["display"]["teaser"]["type"] = "taxonomy_term_reference_link"; $instance["display"]["teaser"]["module"] = "taxonomy"; field_update_instance($instance); $instance = field_info_instance("node", "field_address", "poi"); $instance["display"]["teaser"]["type"] = "text_default"; $instance["display"]["teaser"]["module"] = "text"; field_update_instance($instance); ?>上面这是适用于drupal 7的网站,其中网址、文件名、IP地址、路径、内容类型、字段名称、字段类型等都要根据你自己实情来调整修改。我都是参照博文上面的几个英文参考链接一点一点试出来的。
主要是drush怎么都装不上
主要是drush怎么都装不上,所以才迫不得已让您写下php的谢谢了
谢谢了