You are here

class LoftDataGrids in Loft Data Grids 8

Hierarchy

Expanded class hierarchy of LoftDataGrids

1 string reference to 'LoftDataGrids'
loft_data_grids.services.yml in ./loft_data_grids.services.yml
loft_data_grids.services.yml
1 service uses LoftDataGrids
loft_data_grids.core in ./loft_data_grids.services.yml
Drupal\loft_data_grids\LoftDataGrids

File

src/LoftDataGrids.php, line 9

Namespace

Drupal\loft_data_grids
View source
class LoftDataGrids {
  use StringTranslationTrait;

  /**
   * Return an options array of exporter classes available.
   *
   * @param bool $checkAccess   Check to see if the current user has access to
   *                            each option.
   * @param bool $includeDescription
   * @param bool $useShortname  By default the label is used, set this to true
   *                            to use the shortname.
   *
   * @return array Keys are classnames, values are human options.
   */
  public function getExporterOptions($checkAccess = true, $includeDescription = true, $useShortname = true) {
    $options =& drupal_static(__CLASS__ . '::' . __FUNCTION__, []);
    if (empty($options)) {
      foreach ($this
        ->getExporters($checkAccess) as $info) {

        // Do not include html it will break for select lists
        $label = '';
        $label .= $useShortname && isset($info['shortname']) ? $info['shortname'] : $info['name'];
        $label .= ' (' . $info['extension'] . ')';
        if ($includeDescription) {
          $label .= ' ' . $info['description'];
        }
        $options[$info['id']] = $label;
      }
    }
    return $options;
  }

  /**
   * Return an array of all available exporters.
   *
   * @param bool $removeNoAccess    If $removeNoAccess is true, those
   *                                exporters which the current use does not
   *                                have access to will be removed.
   *
   * @return array Each value is an array of data about the exporter with
   *               keys:
   * - id
   * - name
   * - shortname
   * - description
   * - class
   * - extension
   * - access True if the current user has access.
   */
  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;
          }
        }
      }

      // Exporters added by our module.
      $info = new DrupalTableExporter(new ExportData());
      $i = $info
        ->getInfo();
      $id = $this
        ->generateExporterId($i);
      $exporters[$id] = $i;
      \Drupal::cache()
        ->set($cid, $exporters);
    }

    // Add in the access flag for the current user.
    $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;
  }

  /**
   * Return the id for an exporter based on it's info array.
   *
   * @param array $info
   *
   * @return string
   */
  protected function generateExporterId(array &$info) {
    $id = md5($info['class']);
    $info = [
      'id' => $id,
    ] + $info;
    return $id;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoftDataGrids::generateExporterId protected function Return the id for an exporter based on it's info array.
LoftDataGrids::getExporterOptions public function Return an options array of exporter classes available.
LoftDataGrids::getExporters public function Return an array of all available exporters.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.