diff --git a/core/modules/field_ui/field_ui.admin.inc b/core/modules/field_ui/field_ui.admin.inc index 4f429cc8c06155fac1bc11f81cd15c8a40196681..624f1245819ffa2bce18072dbad201c9d0ae27a3 100644 --- a/core/modules/field_ui/field_ui.admin.inc +++ b/core/modules/field_ui/field_ui.admin.inc @@ -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. * diff --git a/core/modules/field_ui/field_ui.module b/core/modules/field_ui/field_ui.module index 7f617aea5cab6b14ff955eb6449da046077c1de2..01afcbc828e7ddb9b043311de16165a452a5b1fe 100644 --- a/core/modules/field_ui/field_ui.module +++ b/core/modules/field_ui/field_ui.module @@ -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'; } /** diff --git a/core/modules/field_ui/field_ui.routing.yml b/core/modules/field_ui/field_ui.routing.yml new file mode 100644 index 0000000000000000000000000000000000000000..946d60efd062360eca99eced51e64b1d2e8a4bf4 --- /dev/null +++ b/core/modules/field_ui/field_ui.routing.yml @@ -0,0 +1,6 @@ +field_list: + pattern: 'admin/reports/fields' + defaults: + _entity_list: 'field_entity' + requirements: + _permission: 'administer content types' diff --git a/core/modules/field_ui/lib/Drupal/field_ui/FieldListController.php b/core/modules/field_ui/lib/Drupal/field_ui/FieldListController.php new file mode 100644 index 0000000000000000000000000000000000000000..cd3d640a2bb51cd0010493347aedd662b477552f --- /dev/null +++ b/core/modules/field_ui/lib/Drupal/field_ui/FieldListController.php @@ -0,0 +1,125 @@ +<?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; + } + +} diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php index b978964baea6a8442bb8d8748b194eeb877d9226..56370add8a1dc9cd95a252e9f51f80f84f6143ce 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php @@ -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.'); + } }