Skip to content

Instantly share code, notes, and snippets.

@purdy
Created June 15, 2016 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purdy/d98779e6519d6d316bb2d8ee5ebeb5e4 to your computer and use it in GitHub Desktop.
Save purdy/d98779e6519d6d316bb2d8ee5ebeb5e4 to your computer and use it in GitHub Desktop.
Drupal storage object example
<?php
namespace Drupal\gated_content;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
/**
* Class GCStorage.
*/
class GCStorage {
protected $connection;
public function __construct(Connection $connection = NULL) {
if ($connection) {
$this->connection = $connection;
}
else {
$this->connection = Database::getConnection();
}
}
public function get_reports($config = array()) {
$current_date = \Drupal::service('date.formatter')->format(REQUEST_TIME, 'custom', 'c');
$sth = $this->connection->select('gated_content', 'gc')
->fields('gc', array('id', 'title'))
->condition('post_date', $current_date, '<=')
->orderBy('post_date', 'DESC');
if (is_array($config) && isset($config['number_reports']) && $config['number_reports']) {
$sth->range(0, $config['number_reports']);
}
if (is_array($config) && isset($config['report_filter']) && $config['report_filter']) {
if ($config['report_filter'] == 2) { // only free reports
$sth->condition('cost', 0);
}
elseif ($config['report_filter'] == 3 && $config['filter_pattern']) { // aws match
$sth->condition('aws_key', $config['filter_pattern'], 'LIKE');
}
}
$results = $sth->execute();
$reports = $results->fetchAll(\PDO::FETCH_ASSOC);
// kint($reports);
return $reports;
}
}
/* End of the GCStorage.php file */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment