diff --git a/core/modules/field_ui/field_ui.routing.yml b/core/modules/field_ui/field_ui.routing.yml index 98db1e0789..4d086c6e27 100644 --- a/core/modules/field_ui/field_ui.routing.yml +++ b/core/modules/field_ui/field_ui.routing.yml @@ -1,7 +1,7 @@ entity.field_storage_config.collection: path: '/admin/reports/fields' defaults: - _entity_list: 'field_storage_config' + _form: '\Drupal\field_ui\Form\FieldConfigListForm' _title: 'Field list' requirements: _permission: 'administer content types' diff --git a/core/modules/field_ui/src/FieldStorageConfigListBuilder.php b/core/modules/field_ui/src/FieldStorageConfigListBuilder.php index fd87b619a6..7538f3780e 100644 --- a/core/modules/field_ui/src/FieldStorageConfigListBuilder.php +++ b/core/modules/field_ui/src/FieldStorageConfigListBuilder.php @@ -136,4 +136,49 @@ public function buildRow(EntityInterface $field_storage) { return $row; } + /** + * Loads entity IDs using a pager sorted by the entity id. + * + * @return array + * An array of entity IDs. + */ + protected function getEntityIds() { + $query = $this->getStorage()->getQuery() + ->sort($this->entityType->getKey('id')); + + if ($this->fieldTypeFilter) { + $query->condition('type', $this->fieldTypeFilter); + } + + if ($this->entityTypeFilter) { + $query->condition('entity_type', $this->entityTypeFilter); + } + + // Only add the pager if a limit is specified. + if ($this->limit) { + $query->pager($this->limit); + } + + return $query->execute(); + } + + /** + * Set Field Type Filter. + * + * @param $fieldTypeFilter string + * Name of field type. + */ + public function setFieldTypeFilter($fieldTypeFilter) { + $this->fieldTypeFilter = $fieldTypeFilter; + } + + /** + * Set Entity Type Filter. + * + * @param $entityTypeFilter string + * Name of entity type. + */ + public function setEntityTypeFilter($entityTypeFilter) { + $this->entityTypeFilter = $entityTypeFilter; + } } diff --git a/core/modules/field_ui/src/Form/FieldConfigListForm.php b/core/modules/field_ui/src/Form/FieldConfigListForm.php new file mode 100644 index 0000000000..c21892ec9f --- /dev/null +++ b/core/modules/field_ui/src/Form/FieldConfigListForm.php @@ -0,0 +1,182 @@ +entityTypeManager = $entityTypeManager; + $this->fieldTypePluginManager = $fieldTypePluginManager; + $this->entityTypeRepository = $entityTypeRepository; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity_type.manager'), + $container->get('plugin.manager.field.field_type'), + $container->get('entity_type.repository') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'field_config_list_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $listBuilder = $this->entityTypeManager->getListBuilder('field_storage_config'); + $fieldStorageConfigs = $listBuilder->load(); + + $formValues = $form_state->getValues(); + if (!empty($formValues['field_type'])) { + $listBuilder->setFieldTypeFilter($formValues['field_type']); + } + if (!empty($formValues['entity_type'])) { + $listBuilder->setEntityTypeFilter($formValues['entity_type']); + } + + $form['filters'] = [ + '#type' => 'container', + '#attributes' => ['class' => ['form--inline', 'clearfix']], + ]; + + $form['filters']['entity_type'] = [ + '#type' => 'select', + '#title' => $this->t('Entity type'), + '#options' => $this->entityTypeOptions($fieldStorageConfigs), + '#empty_option' => $this->t('- Select an entity type -'), + ]; + + $form['filters']['field_type'] = [ + '#type' => 'select', + '#title' => $this->t('Field type'), + '#options' => $this->fieldTypeOptions($fieldStorageConfigs), + '#empty_option' => $this->t('- Select a field type -'), + ]; + + $form['filters']['actions']['#type'] = 'actions'; + $form['filters']['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Filter'), + '#button_type' => 'primary', + ]; + + $form['table'] = $listBuilder->render(); + + return $form; + } + + /** + * Build entity types options for select list. + * + * @param array $fieldStorageConfigs + * The array of field storage configs. + * + * @return array + * The array of options. + */ + protected function entityTypeOptions(array $fieldStorageConfigs) { + $entityLabels = $this->entityTypeRepository->getEntityTypeLabels(); + + // Gather valid entity types. + $entityTypeOptions = []; + foreach ($fieldStorageConfigs as $fieldStorageConfig) { + $entityName = $fieldStorageConfig->getTargetEntityTypeId(); + + if (array_key_exists($entityName, $entityLabels)) { + if (isset($entityTypeOptions[$entityName])) { + continue; + } + + $entityLabel = $entityLabels[$entityName]->render(); + $entityTypeOptions[$entityName] = "$entityLabel ($entityName)"; + } + } + + return $entityTypeOptions; + } + + /** + * Build field types options for select list. + * + * @param array $fieldStorageConfigs + * The array of field storage configs. + * + * @return array + * The array of options. + */ + protected function fieldTypeOptions(array $fieldStorageConfigs) { + foreach ($fieldStorageConfigs as $fieldStorageConfig) { + $existed_field_types[] = $fieldStorageConfig->getType(); + } + + // Gather valid field types. + $fieldTypeOptions = []; + foreach ($this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions()) as $category => $field_types) { + foreach ($field_types as $name => $field_type) { + if (in_array($name, $existed_field_types)) { + $fieldTypeOptions[$category][$name] = $field_type['label']; + } + } + } + + return $fieldTypeOptions; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $form_state->setRebuild(); + } + +}