Commit 8b1ce342 authored by alexpott's avatar alexpott

Issue #1987708 by mtift, swentel, smiletrl, vijaycs85, mparker17,...

Issue #1987708 by mtift, swentel, smiletrl, vijaycs85, mparker17, tim.plunkett: Convert field_ui_fields_list() to a new style controller.
parent ab22b87b
......@@ -5,58 +5,6 @@
* Administrative interface for custom field type creation.
*/
/**
* Page callback: Lists all defined fields for quick reference.
*
* @see field_ui_menu()
*/
function field_ui_fields_list() {
$instances = field_info_instances();
$field_types = field_info_field_types();
$bundles = entity_get_bundles();
$entity_manager = Drupal::entityManager();
$modules = system_rebuild_module_data();
$header = array(
t('Field name'),
array('data' => t('Field type'), 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)),
t('Used in'),
);
$rows = array();
foreach ($instances as $entity_type => $type_bundles) {
foreach ($type_bundles as $bundle => $bundle_instances) {
foreach ($bundle_instances as $field_name => $instance) {
$field = field_info_field($field_name);
// Initialize the row if we encounter the field for the first time.
if (!isset($rows[$field_name])) {
$rows[$field_name]['class'] = $field['locked'] ? array('menu-disabled') : array('');
$rows[$field_name]['data'][0] = $field['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name;
$module_name = $field_types[$field['type']]['module'];
$rows[$field_name]['data'][1] = $field_types[$field['type']]['label'] . ' ' . t('(module: !module)', array('!module' => $modules[$module_name]->info['name']));
}
// Add the current instance.
$admin_path = $entity_manager->getAdminPath($entity_type, $bundle);
$rows[$field_name]['data'][2][] = $admin_path ? l($bundles[$entity_type][$bundle]['label'], $admin_path . '/fields') : $bundles[$entity_type][$bundle]['label'];
}
}
}
foreach ($rows as $field_name => $cell) {
$rows[$field_name]['data'][2] = implode(', ', $cell['data'][2]);
}
if (empty($rows)) {
$output = t('No fields have been defined yet.');
}
else {
// Sort rows by field name.
ksort($rows);
$output = theme('table', array('header' => $header, 'rows' => $rows));
}
return $output;
}
/**
* Returns HTML for Field UI overview tables.
*
......
......@@ -56,10 +56,8 @@ function field_ui_menu() {
$items['admin/reports/fields'] = array(
'title' => 'Field list',
'description' => 'Overview of fields on all entity types.',
'page callback' => 'field_ui_fields_list',
'access arguments' => array('administer content types'),
'route_name' => 'field_list',
'type' => MENU_NORMAL_ITEM,
'file' => 'field_ui.admin.inc',
);
$items['admin/reports/fields/list'] = array(
'title' => 'Entities',
......@@ -238,6 +236,7 @@ function field_ui_element_info() {
*/
function field_ui_entity_info(&$entity_info) {
$entity_info['field_instance']['controllers']['form']['delete'] = 'Drupal\field_ui\Form\FieldDeleteForm';
$entity_info['field_entity']['controllers']['list'] = 'Drupal\field_ui\FieldListController';
}
/**
......
field_list:
pattern: 'admin/reports/fields'
defaults:
_entity_list: 'field_entity'
requirements:
_permission: 'administer content types'
<?php
/**
* @file
* Contains \Drupal\field_ui\FieldListController.
*/
namespace Drupal\field_ui;
use Drupal\Core\Config\Entity\ConfigEntityListController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\field\FieldInfo;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a listing of fields.
*/
class FieldListController extends ConfigEntityListController {
/**
* An array of information about field types.
*
* @var array
*/
protected $fieldTypes;
/**
* An array of field data.
*
* @var array
*/
protected $fieldInfo;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* An array of entity bundle information.
*
* @var array
*/
protected $bundles;
/**
* Constructs a new EntityListController object.
*
* @param string $entity_type
* The type of entity to be listed.
* @param array $entity_info
* An array of entity info for the entity type.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke hooks on.
* @param \Drupal\field\FieldInfo $field_info
* The field info service.
*/
public function __construct($entity_type, array $entity_info, EntityManager $entity_manager, ModuleHandlerInterface $module_handler, FieldInfo $field_info) {
parent::__construct($entity_type, $entity_info, $entity_manager->getStorageController($entity_type), $module_handler);
$this->fieldTypes = field_info_field_types();
$this->fieldInfo = $field_info->getFieldMap();
$this->entityManager = $entity_manager;
$this->bundles = entity_get_bundles();
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static(
$entity_type,
$entity_info,
$container->get('plugin.manager.entity'),
$container->get('module_handler'),
$container->get('field.info')
);
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$row['id'] = t('Field name');
$row['type'] = array(
'data' => t('Field type'),
'class' => array(RESPONSIVE_PRIORITY_MEDIUM),
);
$row['usage'] = t('Used in');
return $row;
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
if ($entity->locked) {
$row['class'] = array('menu-disabled');
$row['data']['id'] = t('@field_name (Locked)', array('@field_name' => $entity->id()));
}
else {
$row['data']['id'] = $entity->id();
}
$field_type = $this->fieldTypes[$entity->getFieldType()];
$row['data']['type'] = t('@type (module: @module)', array('@type' => $field_type['label'], '@module' => $field_type['module']));
$usage = array();
foreach($this->fieldInfo[$entity->id()]['bundles'] as $entity_type => $field_bundles) {
foreach($field_bundles as $bundle) {
$admin_path = $this->entityManager->getAdminPath($entity_type, $bundle);
$usage[] = $admin_path ? l($this->bundles[$entity_type][$bundle]['label'], $admin_path . '/fields') : $this->bundles[$entity_type][$bundle]['label'];
}
}
$row['data']['usage'] = implode(', ', $usage);
return $row;
}
}
......@@ -72,6 +72,7 @@ function testCRUDFields() {
$this->updateField();
$this->addExistingField();
$this->cardinalitySettings();
$this->fieldListAdminPage();
}
/**
......@@ -595,4 +596,13 @@ function testHelpDescriptions() {
$this->assertRaw('<strong>Test with an upload field.</strong>');
$this->assertRaw('<em>Test with a non upload field.</em>');
}
/**
* Tests that the field list administration page operates correctly.
*/
function fieldListAdminPage() {
$this->drupalGet('admin/reports/fields');
$this->assertText($this->field_name, 'Field name is displayed in field list.');
$this->assertTrue($this->assertLinkByHref('admin/structure/types/manage/' . $this->type . '/fields'), 'Link to content type using field is displayed in field list.');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment