Inject Database in form and fet count of rows

use DateTime;
use DateTimeZone;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class FeesTestFormTEDx2020.
*/
class FeesTestFormTEDx2020 extends FormBase {

/**
* The current database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'fees_test_form_tedx2020';
}

/**
* Constructs a Drupal\checkform\Form\FeesTestFormTEDx2020 object.
*
* @param \Drupal\Core\Database\Connection $database
* Database Service Object.
*/
public function __construct(Connection $database) {
$this->database = $database;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('database'));
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {

$text_closed = '<div class="bg-danger"><strong>Registration is closed until further notice!</strong></div>';

$date_now = new DateTime();
// m-d-Y and is considered as 02/11/2020 00:00:00 Increase one for next date
date_default_timezone_set('Europe/Athens');
$date2 = new DateTime("02/14/2020");
$expired = FALSE;
$numOfOrders = $this->getResults();
dd($numOfOrders);
if ($date_now > $date2 || $numOfOrders > 100) {
$expired = TRUE;
}


.

.

.

.


/**
* Get test order for TEDxUTH.
*
* @return int
* Number of CAPTURED orders so far.
*/
protected function getResults() {
$sql = "SELECT count(*) FROM `alpha_ecommerce` where var3 like '%TEDxUTH%' and state = 'CAPTURED'"; 
$result = $this->database->prepare($sql); 
$result->execute(); 
return $result->fetchColumn(); 
}
}
Inject Database in form and fet count of rows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.