2017-08-09 74 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)); 
}