-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_book.module
99 lines (82 loc) · 3.22 KB
/
contact_book.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Contact Book Module Logic
*/
// Provide access to the URL Class
use Drupal\Core\Url;
// Provide access to the URL Class
function contact_book_create_table() {
// Table haeders row
$header = [
'id' => t('Contact'),
'name' => t('Name'),
'email' => t('Email'),
'phone' => t('Phone'),
'address' => t('Address'),
'country' => t('Country'),
'manage' => t('Manage'),
'remove' => t('Remove'),
];
// checking contacts from table
$db_query = \Drupal::database()->select('contact_book', 'contacts');
$db_query->fields('contacts', ['id','name','email','phone','address','country']);
$db_results = $db_query->execute()->fetchAll();
$rows=array();
foreach($db_results as $contact){
$edit_contact = Url::fromUserInput('/set/contact?cid='.$contact->id);
$delete_contact = Url::fromUserInput('/delete/contact/'.$contact->id);
$p_contact = Url::fromUserInput('/show/contact/'.$contact->id);
//print the contact from table
$rows[] = array(
'id' =>$contact->id,
'name' => \Drupal::l($contact->name, $p_contact),
'email' => $contact->email,
'phone' => $contact->phone,
'address' => $contact->address,
'country' => $contact->country,
\Drupal::l('Edit', $edit_contact),
\Drupal::l('Delete', $delete_contact),
);
}
// $rows = [
// // [Markup::create('<strong>test 1</strong>'),'test'],
// // ['1', test', '[email protected]', 'test12345', 'test-bcd', 'Kenya'],
// // ['2', test', '[email protected]', 'test12345', 'test-bcd', 'Kenya'],
// ];
$build['table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No contacts have been found.'),
];
return [
'#type' => '#markup',
'#markup' => render($build)
];
}
function contact_book_show_contact($cid) {
// checking contacts from table
$db_query = \Drupal::database()->select('contact_book', 'contact')->condition('id', $cid);
$db_query->fields('contact', ['id','name','email','phone','address','country']);
$db_result = $db_query->execute()->fetchAll();
// dd($db_result);
$row=array();
foreach($db_result as $contact){
// dd($contact->name);
$back = Url::fromUserInput('/view/contacts');
$edit_contact = Url::fromUserInput('/set/contact?cid='.$contact->id);
$delete_contact = Url::fromUserInput('/delete/contact/'.$contact->id);
return array(
'#title' => 'PhoneBook Details:',
'#markup' => '<h2>'.'Contact Name: <u>'.$contact->name.'</u></h2>'.
'<small>'.'<b>Email:</b> '.$contact->email.'</small>'.
'<small>'.'<b> Mobile:</b> '.$contact->phone.'</small>'.'</br>'.
'<strong>'.'Country: '.'</strong>'.'<i>'.$contact->country.'</i>'.'</br>'.
'<b>'.'Address: '.'</b>'.'</i>'.$contact->address.'</i>'.'</br>'.'</br>'.
'<h6>'.'<b> Manage Contact: </b> '
.\Drupal::l('Edit', $edit_contact).' '.'<b>|</b>'.' '.\Drupal::l('Delete', $delete_contact).
'</h6>'.'</br>'.
'<p>'.'<b> Go Back:</b> '.\Drupal::l('Return to Contacts Table', $back).'</p>'
);
}
}