public function 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 drupal/core/modules/views_ui/views_ui.routing.yml
drupal/core/modules/views_ui/views_ui.routing.yml

File

drupal/core/modules/views_ui/lib/Drupal/views_ui/Controller/ViewsUIController.php, line 94
Contains \Drupal\views_ui\Controller\ViewsUIController.

Class

ViewsUIController
Returns responses for Views UI routes.

Namespace

Drupal\views_ui\Controller

Code

public function reportFields() {
  $views = $this->entityManager
    ->getStorageController('view')
    ->load();

  // Fetch all fieldapi fields which are used in views
  // Therefore search in all views, displays and handler-types.
  $fields = array();
  $handler_types = ViewExecutable::viewsHandlerTypes();
  foreach ($views as $view) {
    $executable = $view
      ->get('executable');
    $executable
      ->initDisplay();
    foreach ($executable->displayHandlers as $display_id => $display) {
      if ($executable
        ->setDisplay($display_id)) {
        foreach ($handler_types as $type => $info) {
          foreach ($executable
            ->getItems($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 = array(
    t('Field name'),
    t('Used in'),
  );
  $rows = array();
  foreach ($fields as $field_name => $views) {
    $rows[$field_name]['data'][0] = check_plain($field_name);
    foreach ($views as $view) {
      $rows[$field_name]['data'][1][] = l($view, "admin/structure/views/view/{$view}");
    }
    $rows[$field_name]['data'][1] = implode(', ', $rows[$field_name]['data'][1]);
  }

  // Sort rows by field name.
  ksort($rows);
  $output = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No fields have been used in views yet.'),
  );
  return $output;
}