2017-08-09 72 views
0

在我的drupal程序的这一段中,我使用页面回调函数在页面上显示节点。现在我需要做的是删除基于其ID的节点。根据drupal中的id删除节点7

我希望通过在每个显示的节点旁提供一个“删除”链接来完成此操作。谁能帮我这个? 我对Drupal很新。所以一个详细的答案将不胜感激。 在此先感谢。

function mfrp_nodelist() { 
$query = new EntityFieldQuery(); 
$result = $query 
->entityCondition('entity_type', 'node') 
->entityCondition('bundle', 'article') //this part also confused me, is it bundle, type or both? bundle works for me 
->propertyCondition('status', 1) 
->propertyCondition('uid', '1'); 
$result = $query->execute(); 
$nids = array_keys($result['node']); 
$nodes = node_load_multiple($nids); 
$output = node_view_multiple($nodes); 
return $output; 
} 

回答

0

PHP中通过节点ID删除节点的快速示例。

$nid= 22; // $node id , which needs to be retrieved from delete link but hardcoded for now; 
    node_delete($nid); 

你需要做一个删除链接是这样的:

http://mydomain/nodes/delete?nid_delete=22

然后在PHP中您可以通过PHP超全局获取价值filter_xss($_GET['nid_delete]);

+0

$ nids = array_keys($ result ['node']);. $ nids是一个包含所有节点的节点ID的数组吗?如果是这样,为什么不能使用foreach循环打印节点ID? – Saikat

+0

根据您的解决方案进行链接,我需要单个节点ID。我怎样才能获得这些? – Saikat

+0

你能分享你的查询输出吗? –

0

如果你有视图模块,您可以添加一个页面(将它们显示为网格或表格)并列出文章内容(添加标题,所需字段和删除链接)。通过使用删除链接,您可以删除节点(确保用户有权删除节点内容类型)

+0

你能否帮我解决如何以列表的形式显示它们? – Saikat

1

首先,您需要在drupal主题表中创建节点列表,并且此代码将在删除链接上工作

function yourmodule_rows() { 
$rows = array() 
$header = array(t('Sn no.'),t('Nid'),t('Node Title'),t('Op'),); 
$query = db_select('node', 'n'); 
    $query->fields('n', array('title','nid')); 
    $result = $query->execute(); 
    $sn = 0; 
    while ($record = $result->fetchObject()) { 
    $rows[] = array(++$sn, $record->nid, check_plain($record->title), l('Delete', 'node/'.$record->nid . '/delete'),); 
} 
    $render_array['yourmodule'] = array(array('#theme' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => t('No Record found!'),), array('#theme' => 'pager',),); 
    return $render_array; 
} 
0

我做了一些研究,并找到了一种方式来显示我的节点的列表形式。并且还创建了删除链接。但是,我不明白如何使链接工作。在哪个页面上连接链接以及如何根据链接最终删除节点?任何帮助将不胜感激。

function mfrp_nodelist() { 
$output = array(); 
$query = new EntityFieldQuery(); 
$result = $query 
->entityCondition('entity_type', 'node') 
->entityCondition('bundle', 'article') //this part also confused me, is it 
bundle, type or both? bundle works for me 
->propertyCondition('status', 1) 
->propertyCondition('uid', '1'); 
$result = $query->execute(); 
$nids = (array_keys($result['node'])); 
$nodes = node_load_multiple($nids); 

$nid=array(); 
foreach ($nodes as $nid){ 
$outputs[] = array('data' => array($nid->nid, $nid->title, "<a 
href='delete/{$nid->nid}'>" . t('Delete') . "</a>"."|"."<a href='edit/{$nid- 
>nid}'>" . t('Edit') . "</a>")); 
} 
dpm($output); 
echo "The number of records are ".count($outputs); 

$per_page = 4; 
// Initialize the pager 
$current_page = pager_default_initialize(count($outputs), $per_page); 
// Split your list into page sized chunks 
$chunks = array_chunk($outputs, $per_page, TRUE); 
// Show the appropriate items from the list 
$header = array(t('nid'),t('title')); 

$output = theme('table', 
array('header' => $header, 
'rows' => $chunks[$current_page], 
'attributes' => array(), 
'empty' => t("No records found"))); 
// Show the pager 
$output .= theme('pager', array('quantity',count($outputs))); 
//return $output .= theme('pager',array('quantity',count($rows))); 
return theme('mfrp_list', array('results' => $output)); 
}