Create a gist now

Instantly share code, notes, and snippets.

@purdy /GCStorage.php Secret
Created Jun 15, 2016

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