LoftDataGrids.php in Loft Data Grids 8
File
src/LoftDataGrids.php
View source
<?php
namespace Drupal\loft_data_grids;
use AKlump\LoftDataGrids\ExportData;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class LoftDataGrids {
use StringTranslationTrait;
public function getExporterOptions($checkAccess = true, $includeDescription = true, $useShortname = true) {
$options =& drupal_static(__CLASS__ . '::' . __FUNCTION__, []);
if (empty($options)) {
foreach ($this
->getExporters($checkAccess) as $info) {
$label = '';
$label .= $useShortname && isset($info['shortname']) ? $info['shortname'] : $info['name'];
$label .= ' (' . $info['extension'] . ')';
if ($includeDescription) {
$label .= ' ' . $info['description'];
}
$options[$info['id']] = $label;
}
}
return $options;
}
public function getExporters($removeNoAccess = true) {
$exporters = [];
$cid = 'loft_data_grids:exporters';
if ($cache = \Drupal::cache()
->get($cid)) {
$exporters = $cache->data;
}
else {
$path = DRUPAL_ROOT . '/vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/';
$possible = file_scan_directory($path, '/.+Exporter\\.php$/');
foreach ($possible as $path => $data) {
$class = '\\AKlump\\LoftDataGrids\\' . $data->name;
$interfaces = class_implements($class);
if (in_array('AKlump\\LoftDataGrids\\ExporterInterface', $interfaces)) {
$obj = new \ReflectionClass($class);
if (!$obj
->isAbstract()) {
$info = new $class(new ExportData());
$i = $info
->getInfo();
$id = $this
->generateExporterId($i);
$exporters[$id] = [
'access' => true,
] + $i;
}
}
}
$info = new DrupalTableExporter(new ExportData());
$i = $info
->getInfo();
$id = $this
->generateExporterId($i);
$exporters[$id] = $i;
\Drupal::cache()
->set($cid, $exporters);
}
$user = \Drupal::currentUser();
array_walk($exporters, function (&$info) use ($user) {
$perm = 'loft_data_grids:' . $info['id'];
$info['access'] = $user
->hasPermission($perm);
});
\Drupal::moduleHandler()
->alter('loft_data_grids_exporters', $exporters);
if ($removeNoAccess) {
$exporters = array_filter($exporters, function ($value) {
return $value['access'];
});
}
return $exporters;
}
protected function generateExporterId(array &$info) {
$id = md5($info['class']);
$info = [
'id' => $id,
] + $info;
return $id;
}
}