简体   繁体   English

Drupal 7.x:如何用PHP输入格式创建一个新节点?

[英]Drupal 7.x: how to create a new node with PHP input format?

I want execute custom PHP script that will delete all nodes of a particular type (Feed items). 我想执行自定义PHP脚本,该脚本将删除特定类型的所有节点(Feed项目)。 How to create a new node with PHP input format, so I can paste code into the node body? 如何使用PHP输入格式创建新节点,以便可以将代码粘贴到节点主体中? Please specify detailed steps. 请指定详细步骤。

$node_type = 'Feed items';

  //fetch the nodes we want to delete
  $result = db_query("SELECT nid FROM {node} WHERE type='%s'",$node_type);
  while ($row = db_fetch_object($result)){
    node_delete($row->nid);
    $deleted_count+=1;
  }
  //simple debug message so we can see what had been deleted.
  drupal_set_message("$deleted_count nodes have been deleted");

It's not entirely clear what you want to achieve. 尚不清楚您要实现什么。 Do you simply want to execute that PHP code, once? 您是否只想一次执行该PHP代码? Or whenever a node of a certain type is created? 还是每当创建某种类型的节点时?

If you just want to execute the PHP code once, you can install the devel module ( http://drupal.org/project/devel ) and paste your code in <your_site_url>/devel/php 如果只想执行一次PHP代码,则可以安装devel模块( http://drupal.org/project/devel )并将代码粘贴到<your_site_url>/devel/php

--EDIT - 编辑

Another option would be to create a php page that bootstraps Drupal and runs your code: http://www.avenuewebmedia.com/drupalbootstrap-connect-drupal-external-php-script 另一个选择是创建一个引导Drupal并运行您的代码的php页面: http : //www.avenuewebmedia.com/drupalbootstrap-connect-drupal-external-php-script

OK, I managed myself how to do this: I had to enable the "PHP filter" module first (in Drupal core, disabled by default). 好的,我设法做到了这一点:我必须首先启用“ PHP过滤器”模块(在Drupal核心中,默认情况下处于禁用状态)。 In admin > Configuration > Content authoring > Text formats > Add text format, in "Enabled filters" mark "PHP evaluator", in Roles select 'admin', save configuration. 在admin>配置>内容创作>文本格式>添加文本格式中,在“启用的过滤器”中标记“ PHP评估程序”,在“角色”中选择“ admin”,保存配置。 Then I created a new node (article), set the input format to PHP, pasted the code into the node body, added a title and clicked submit. 然后,我创建了一个新节点(文章),将输入格式设置为PHP,将代码粘贴到节点主体中,添加了标题并单击提交。 Code has been executed. 代码已执行。 Sure, we need to delete the node when done. 当然,完成后我们需要删除该节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM