你在这里

19. 如何给Drupal7 的内容管理页面添加一列?

问题:

如何给Drupal7 的内容管理页面(admin/content)添加一列?

回答:

admin_views(https://drupal.org/project/admin_views)模块把你想用代码实现的方式已经预先解决了。

通过这个模块 可以覆盖默认的/admin/content页面 可以在views里添加和删除不需要的列!使用此模块,可以不用写代码就可以轻松定制自己的内容管理页面了。除此之外,此模块还有评论和用户的管理页面,都是用views覆写了的!

(备注:需要注意的是:admin_views模块,有几个依赖的模块,如:ctools、views、entity、views_bulk_operations。)

当然,有些时候不想装太多的模块,想自己写代码,是否可以用代码方式来实现呢?

答案是肯定可以的。

比如:想加一列“Changed by”(更新者),需要自定义一个模块,可以使用如下的代码(注意:请把“MYMODULE” 改为你的模块机器名):

function MYMODULE_form_node_admin_content_alter(&$form, &$form_state, $form_id) {
  // Load the nodes. This incurrs very little overhead as 
  // "$nodes = node_load_multiple($nids);" has already been run on these
  // nids in node_admin_nodes(). The static cache will be used instead of
  // another db query being invoked
  $nodes = node_load_multiple(array_keys($form['admin']['nodes']['#options']));

  // Grab a list of all user ids that have been responsible for changing the node
  $uids = array();
  foreach ($nodes as $node) {
    $uids[] = $node->changed_by;
  }

  // Filter out duplicates (one user may have been the last to change more than one node)
  $uids = array_unique($uids);

  // Load a list of all involved users in one go. This is about as performant
  // as this is going to get, as you're going to need the user objects one
  // way or the other for the call to theme_username
  $users = user_load_multiple($uids);

  // Add another column to the table header
  $form['admin']['nodes']['#header']['changed_by'] = array('data' => t('Changed by'));

  // Loop through the rows in the table and add the changed by column
  foreach ($form['admin']['nodes']['#options'] as $nid => $row) {
    // Grab the user related to this node.
    $this_user = $users[$nodes[$nid]->changed_by];

    // Add data for the new column
    $form['admin']['nodes']['#options'][$nid]['changed_by'] = theme('username', array('account' => $this_user));
  }
}

效果如下图所示:

enter image description here