Commit 01c6898d authored by Cristian Romanescu's avatar Cristian Romanescu
Browse files

Issue #3238560 by VladimirAus, cristiroma, stefan.butura: Implement export to CSV

parent 56911fe0
Loading
Loading
Loading
Loading

entity_reports.csv.inc

0 → 100644
+52 −0
Original line number Diff line number Diff line
<?php

function entity_reports_export_csv($entity_type) {
  $entity_info = _entity_reports_get_entity_type_info($entity_type);
  $header = [
    t('Entity type'),
    t('Bundle label'),
    t('Bundle machine name'),
    t('Field name'),
    t('Data type'),
    t('Machine name'),
    t('Description'),
    t('Required'),
    t('Translatable'),
    t('Cardinality'),
  ];

  header('Content-Type: text/csv; charset=utf-8');
  header("Content-Disposition: attachment; filename=report-$entity_type.csv");

  $fp = fopen('php://output', 'w');
  fputcsv($fp, $header);

  foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
    if (!empty($bundle_info['field_instances'])) {
      foreach ($bundle_info['field_instances'] as $field_name => $field_data) {
        if ($field_data['cardinality'] === '-1') {
          $cardinality = t('Unlimited values');
        }
        else {
          $cardinality = format_plural($field_data['cardinality'], 'One value', '@count values', ['@count' => $field_data['cardinality']]);
        }

        $row = [
          $entity_type,
          $bundle_info['label'],
          $bundle_name,
          $field_data['label'],
          $field_data['data_type'],
          $field_data['field_name'],
          !empty($field_data['description']) ? $field_data['description'] : '-',
          $field_data['required'] ? t('Yes') : t('No'),
          $field_data['translatable'] ? t('Yes') : t('No'),
          $cardinality,
        ];
        fputcsv($fp, $row);
      }
    }
  }

  fclose($fp);
}
+37 −0
Original line number Diff line number Diff line
@@ -22,6 +22,20 @@ function entity_reports_menu() {
    ];
  }

  foreach (['csv'] as $format) {
    foreach (_entity_reports_get_entity_types() as $entity_type => $entity_type_info) {
      $items["admin/reports/entity/{$entity_type}/export/$format"] = [
        'title' => isset($entity_type_info['label'])
          ? "{$entity_type_info['label']} export in $format"
          : "{$entity_type} export in $format",
        'page callback' => 'entity_reports_entity_export_page',
        'page arguments' => [3, 5],
        'access arguments' => ['view entity reports'],
        'type' => MENU_LOCAL_TASK,
      ];
    }
  }

  return $items;
}

@@ -61,6 +75,20 @@ function entity_reports_summary_page() {
  return $build;
}

/**
 * Export page.
 */
function entity_reports_entity_export_page($entity_type, $format) {
  switch ($format) {
    case 'csv':
      module_load_include('csv.inc', 'entity_reports', 'entity_reports');
      entity_reports_export_csv($entity_type);
      break;
  }

  exit();
}

/**
 * Statistics and fields report for the requested entity type.
 */
@@ -85,6 +113,15 @@ function entity_reports_entity_report_page($form, &$form_state) {
    ];
  }

  foreach (['csv'] as $format) {
    $form['export'][$format] = [
      '#type' => 'link',
      '#title' => t('Export as @format', ['@format' => strtoupper($format)]),
      '#href' => "admin/reports/entity/$entity_type/export/$format",
      '#suffix' => '<br>',
    ];
  }

  if (!empty($rows)) {
    $form['properties'] = [
      '#type' => 'fieldset',