/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Gather the current user so the new record has ownership. $account = $this->currentUser(); // Save the submitted entry. $entry = array('name' => $form_state->getValue('name'), 'surname' => $form_state->getValue('surname'), 'age' => $form_state->getValue('age'), 'uid' => $account->id()); $return = DbtngExampleStorage::insert($entry); if ($return) { drupal_set_message(t('Created entry @entry', array('@entry' => print_r($entry, TRUE)))); } }
/** * Render a filtered list of entries in the database. */ public function entryAdvancedList() { $content = array(); $content['message'] = array('#markup' => $this->t('A more complex list of entries in the database.') . ' ' . $this->t('Only the entries with name = "John" and age older than 18 years are shown, the username of the person who created the entry is also shown.')); $headers = array(t('Id'), t('Created by'), t('Name'), t('Surname'), t('Age')); $rows = array(); foreach ($entries = DbtngExampleStorage::advancedLoad() as $entry) { // Sanitize each entry. $rows[] = array_map('Drupal\\Component\\Utility\\SafeMarkup::checkPlain', $entry); } $content['table'] = array('#type' => 'table', '#header' => $headers, '#rows' => $rows, '#attributes' => array('id' => 'dbtng-example-advanced-list'), '#empty' => t('No entries available.')); // Don't cache this page. $content['#cache']['max-age'] = 0; return $content; }
/** * Tests several combinations, adding entries, updating and deleting. */ public function testDbtngExampleStorage() { // Create a new entry. $entry = array('name' => 'James', 'surname' => 'Doe', 'age' => 23); DbtngExampleStorage::insert($entry); // Save another entry. $entry = array('name' => 'Jane', 'surname' => 'NotDoe', 'age' => 19); DbtngExampleStorage::insert($entry); // Verify that 4 records are found in the database. $result = DbtngExampleStorage::load(); $this->assertEqual(count($result), 4, 'Found a total of four entries in the table after creating two additional entries.'); // Verify 2 of these records have 'Doe' as surname. $result = DbtngExampleStorage::load(array('surname' => 'Doe')); $this->assertEqual(count($result), 2, 'Found two entries in the table with surname = "Doe".'); // Now find our not-Doe entry. $result = DbtngExampleStorage::load(array('surname' => 'NotDoe')); $this->assertEqual(count($result), 1, 'Found one entry in the table with surname "NotDoe'); // Our NotDoe will be changed to "NowDoe". $entry = $result[0]; $entry->surname = "NowDoe"; // update() returns the number of entries updated. $this->assertNotEqual(DbtngExampleStorage::update((array) $entry), 0, "NotDoe updated to NowDoe."); $result = DbtngExampleStorage::load(array('surname' => 'NowDoe')); $this->assertEqual(count($result), 1, "Found renamed 'NowDoe' surname"); // Read only John Doe entry. $result = DbtngExampleStorage::load(array('name' => 'John', 'surname' => 'Doe')); $this->assertEqual(count($result), 1, 'Found one entry for John Doe.'); // Get the entry. $entry = (array) end($result); // Change age to 45. $entry['age'] = 45; // Update entry in database. DbtngExampleStorage::update((array) $entry); // Find entries with age = 45. // Read only John Doe entry. $result = DbtngExampleStorage::load(array('surname' => 'NowDoe')); $this->assertEqual(count($result), 1, 'Found one entry with surname = Nowdoe.'); // Verify it is Jane NowDoe. $entry = (array) end($result); $this->assertEqual($entry['name'], 'Jane', 'The name Jane is found in the entry'); $this->assertEqual($entry['surname'], 'NowDoe', 'The surname NowDoe is found in the entry'); // Delete the entry. DbtngExampleStorage::delete($entry); // Verify that now there are only 3 records. $result = DbtngExampleStorage::load(); $this->assertEqual(count($result), 3, 'Found only three records, a record was deleted.'); }