跳转到主要内容
learningxm 提交于 1 December 2012

drupal8使用以下方法单独定义列表头和内容在CategoryListController.php文件

<?php /** * Definition of Drupal\contact\CategoryListController. */ namespace Drupal\contact; use Drupal\Core\Config\Entity\ConfigEntityListController; use Drupal\Core\Entity\EntityInterface; /** * Provides a listing of contact categories. */ class CategoryListController extends ConfigEntityListController { /** * Overrides Drupal\Core\Entity\EntityListController::buildHeader(). */ public function buildHeader() { $row['category'] = t('Category'); $row['recipients'] = t('Recipients'); $row['selected'] = t('Selected'); $row['operations'] = t('Operations'); return $row; } /** * Overrides Drupal\Core\Entity\EntityListController::buildRow(). */ public function buildRow(EntityInterface $entity) { $row['category'] = check_plain($entity->label()); $row['recipients'] = check_plain(implode(', ', $entity->recipients)); $default_category = config('contact.settings')->get('default_category'); $row['selected'] = ($default_category == $entity->id() ? t('Yes') : t('No')); $row['operations']['data'] = $this->buildOperations($entity); return $row; } } ?>

调用方法,是不是很简洁呢: <?php /** * Page callback: Lists contact categories. * * @return array * A build array in the format expected by drupal_render(). * * @see contact_menu() */ function contact_category_list() { drupal_set_title(t('Contact form categories')); return entity_list_controller('contact_category')->render(); } ?>