function ViewsUIController::reportFields

Same name in other branches
  1. 8.9.x core/modules/views_ui/src/Controller/ViewsUIController.php \Drupal\views_ui\Controller\ViewsUIController::reportFields()
  2. 10 core/modules/views_ui/src/Controller/ViewsUIController.php \Drupal\views_ui\Controller\ViewsUIController::reportFields()
  3. 11.x core/modules/views_ui/src/Controller/ViewsUIController.php \Drupal\views_ui\Controller\ViewsUIController::reportFields()

Lists all instances of fields on any views.

Return value

array The Views fields report page.

1 string reference to 'ViewsUIController::reportFields'
views_ui.routing.yml in core/modules/views_ui/views_ui.routing.yml
core/modules/views_ui/views_ui.routing.yml

File

core/modules/views_ui/src/Controller/ViewsUIController.php, line 58

Class

ViewsUIController
Returns responses for Views UI routes.

Namespace

Drupal\views_ui\Controller

Code

public function reportFields() {
    $views = $this->entityTypeManager()
        ->getStorage('view')
        ->loadMultiple();
    // Fetch all fieldapi fields which are used in views
    // Therefore search in all views, displays and handler-types.
    $fields = [];
    $handler_types = ViewExecutable::getHandlerTypes();
    foreach ($views as $view) {
        $executable = $view->getExecutable();
        $executable->initDisplay();
        foreach ($executable->displayHandlers as $display_id => $display) {
            if ($executable->setDisplay($display_id)) {
                foreach ($handler_types as $type => $info) {
                    foreach ($executable->getHandlers($type, $display_id) as $item) {
                        $table_data = $this->viewsData
                            ->get($item['table']);
                        if (isset($table_data[$item['field']]) && isset($table_data[$item['field']][$type]) && ($field_data = $table_data[$item['field']][$type])) {
                            // The final check that we have a fieldapi field now.
                            if (isset($field_data['field_name'])) {
                                $fields[$field_data['field_name']][$view->id()] = $view->id();
                            }
                        }
                    }
                }
            }
        }
    }
    $header = [
        t('Field name'),
        t('Used in'),
    ];
    $rows = [];
    foreach ($fields as $field_name => $views) {
        $rows[$field_name]['data'][0]['data']['#plain_text'] = $field_name;
        foreach ($views as $view) {
            $rows[$field_name]['data'][1][] = Link::fromTextAndUrl($view, new Url('entity.view.edit_form', [
                'view' => $view,
            ]))->toString();
        }
        $item_list = [
            '#theme' => 'item_list',
            '#items' => $rows[$field_name]['data'][1],
            '#context' => [
                'list_style' => 'comma-list',
            ],
        ];
        $rows[$field_name]['data'][1] = [
            'data' => $item_list,
        ];
    }
    // Sort rows by field name.
    ksort($rows);
    $output = [
        '#type' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#empty' => t('No fields have been used in views yet.'),
    ];
    return $output;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.