Skip to content
Snippets Groups Projects
Commit 3a3788e6 authored by Miguel Guerreiro's avatar Miguel Guerreiro Committed by Miguel Guerreiro
Browse files

Issue #3165417 by gueguerreiro: Use Dependency Injection where possible

parent de158e9d
Branches
Tags
No related merge requests found
......@@ -35,88 +35,88 @@ class HelpfulnessAdminForm extends ConfigFormBase {
// Options if the user selected "Yes".
$form['yes_options'] = [
'#type' => 'details',
'#title' => t('"YES" Options'),
'#title' => $this->t('"YES" Options'),
];
$form['yes_options']['helpfulness_yes_title'] = [
'#type' => 'text_format',
'#title' => t('Title for the comments'),
'#title' => $this->t('Title for the comments'),
'#default_value' => $config->get('helpfulness_yes_title'),
'#description' => t('Markup for the title if the user selects "yes". This text will appear above the comments area.'),
'#description' => $this->t('Markup for the title if the user selects "yes". This text will appear above the comments area.'),
];
$form['yes_options']['helpfulness_yes_description'] = [
'#type' => 'text_format',
'#title' => t('Description for the comments'),
'#title' => $this->t('Description for the comments'),
'#default_value' => $config->get('helpfulness_yes_description'),
'#description' => t('Markup for the description if the user selects "yes". This text will appear below the comments area'),
'#description' => $this->t('Markup for the description if the user selects "yes". This text will appear below the comments area'),
];
// Options if the user selected "No".
$form['no_options'] = [
'#type' => 'details',
'#title' => t('"NO" Options'),
'#title' => $this->t('"NO" Options'),
];
$form['no_options']['helpfulness_no_title'] = [
'#type' => 'text_format',
'#title' => t('Title for the comments'),
'#title' => $this->t('Title for the comments'),
'#default_value' => $config->get('helpfulness_no_title'),
'#description' => t('Markup for the title if the user selects "no". This text will appear above the comments area.'),
'#description' => $this->t('Markup for the title if the user selects "no". This text will appear above the comments area.'),
];
$form['no_options']['helpfulness_no_description'] = [
'#type' => 'text_format',
'#title' => t('Description for the comments'),
'#title' => $this->t('Description for the comments'),
'#default_value' => $config->get('helpfulness_no_description'),
'#description' => t('Markup for the description if the user selects "no". This text will appear below the comments area'),
'#description' => $this->t('Markup for the description if the user selects "no". This text will appear below the comments area'),
];
// Options for the comments.
$form['comment_options'] = [
'#type' => 'details',
'#title' => t('Comment options'),
'#title' => $this->t('Comment options'),
];
$form['comment_options']['helpfulness_comment_required'] = [
'#type' => 'radios',
'#title' => t('Are comments required to submit feedback?'),
'#title' => $this->t('Are comments required to submit feedback?'),
'#default_value' => $config->get('helpfulness_comment_required') ?? 0,
'#options' => [0 => t('No'), 1 => t('Yes')],
'#options' => [0 => $this->t('No'), 1 => $this->t('Yes')],
];
$form['comment_options']['helpfulness_comment_required_message'] = [
'#type' => 'textarea',
'#title' => t('Message Text'),
'#title' => $this->t('Message Text'),
'#default_value' => $config->get('helpfulness_comment_required_message'),
'#description' => t('Text for error message if feedback was submitted without a comment.'),
'#description' => $this->t('Text for error message if feedback was submitted without a comment.'),
];
// Options for notification emails.
$form['email_options'] = [
'#type' => 'details',
'#title' => t('Email options'),
'#title' => $this->t('Email options'),
];
$form['email_options']['helpfulness_notification_email'] = [
'#type' => 'textfield',
'#title' => t('Email'),
'#title' => $this->t('Email'),
'#default_value' => $config->get('helpfulness_notification_email'),
'#description' => t('Enter an email to receive notifications if new feedback has been submitted.<br>If no email address is given then no notification email will be send.'),
'#description' => $this->t('Enter an email to receive notifications if new feedback has been submitted.<br>If no email address is given then no notification email will be send.'),
];
$form['email_options']['helpfulness_notification_subject'] = [
'#type' => 'textfield',
'#title' => t('Subject'),
'#title' => $this->t('Subject'),
'#default_value' => $config->get('helpfulness_notification_subject'),
'#description' => t('Subject for the notification email.'),
'#description' => $this->t('Subject for the notification email.'),
];
$form['email_options']['helpfulness_notification_message_prefix'] = [
'#type' => 'textarea',
'#title' => t('Prefix for message body'),
'#title' => $this->t('Prefix for message body'),
'#default_value' => $config->get('helpfulness_notification_message_prefix'),
'#description' => t('Text for the body of the notification email, will be prepended to values submitted by the user.'),
'#description' => $this->t('Text for the body of the notification email, will be prepended to values submitted by the user.'),
];
return parent::buildForm($form, $form_state);
......@@ -128,7 +128,7 @@ class HelpfulnessAdminForm extends ConfigFormBase {
public function validateForm(array &$form, FormStateInterface $form_state) {
$notification_email = trim($form_state->getValue('helpfulness_notification_email'));
if (!empty($notification_email) && !valid_email_address($notification_email)) {
$form_state->setErrorByName('helpfulness_notification_email', t('The email address you entered is not valid.'));
$form_state->setErrorByName('helpfulness_notification_email', $this->t('The email address you entered is not valid.'));
}
}
......
......@@ -2,18 +2,58 @@
namespace Drupal\helpfulness\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormBase;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a block form to leave helpfulness feedback.
*/
class HelpfulnessBlockForm extends FormBase {
/**
* A database connection service instance.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* A current path service instance.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected $currentPath;
/**
* A plugin manager mail service instance.
*
* @var \Drupal\Core\Mail\MailManager
*/
protected $mailManager;
/**
* A language manager service instance.
*
* @var \Drupal\Core\Language\LanguageManager
*/
protected $languageManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->database = $container->get('database');
$instance->currentPath = $container->get('path.current');
$instance->mailManager = $container->get('plugin.manager.mail');
$instance->languageManager = $container->get('language_manager');
return $instance;
}
/**
* {@inheritdoc}
*/
......@@ -32,11 +72,11 @@ class HelpfulnessBlockForm extends FormBase {
$form['helpfulness_rating'] = [
'#type' => 'radios',
'#options' => [1 => t('Yes'), 0 => t('No')],
'#options' => [1 => $this->t('Yes'), 0 => $this->t('No')],
];
// Build the title markup.
$config = \Drupal::config('helpfulness.settings');
$config = $this->config('helpfulness.settings');
$title = '<div class="helpfulness_yes_title">' . $config->get('helpfulness_yes_title') . '</div >';
$title .= '<div class="helpfulness_no_title">' . $config->get('helpfulness_no_title') . '</div >';
......@@ -66,7 +106,7 @@ class HelpfulnessBlockForm extends FormBase {
public function validateForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('helpfulness.settings');
if (NULL == ($form_state->getValue('helpfulness_rating'))) {
if (!$form_state->getValue('helpfulness_rating')) {
$form_state->setErrorByName('helpfulness_rating', $this->t('Please indicate if this page is helpful or not.'));
}
......@@ -85,40 +125,37 @@ class HelpfulnessBlockForm extends FormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Get the useragent, with a fall back in case the access is blocked.
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$user_agent = Html::escape(trim($_SERVER['HTTP_USER_AGENT']));
if ($user_agent = $this->getRequest()->server->get('HTTP_USER_AGENT')) {
$user_agent = Html::escape(trim($user_agent));
}
else {
$user_agent = 'not available';
$user_agent = $this->t('not available');
}
// Validate the comments.
$comments = Unicode::substr(trim(strip_tags($form_state->getValue('helpfulness_comments'))), 0, 1024);
if (empty($comments)) {
$comments = t('None');
$comments = $this->t('None');
}
// Get the authenticated user ID (if any).
$user_id = \Drupal::currentUser()->id();
$user_id = $this->currentUser()->id();
// Form values.
$fields = [
'uid' => $user_id,
'status' => HELPFULNESS_STATUS_OPEN,
'system_path' => \Drupal::service('path.current')->getPath(),
'system_path' => $this->currentPath->getPath(),
'path_alias' => Url::fromRoute("<current>")->toString(),
'base_url' => $GLOBALS['base_url'],
'base_url' => $this->getRequest()->getSchemeAndHttpHost(),
'helpfulness' => $form_state->getValue('helpfulness_rating'),
'message' => $comments,
'useragent' => $user_agent,
'timestamp' => REQUEST_TIME,
];
// Get the database connection.
$db = Database::getConnection();
// Insert the data to the helpfulness table.
$db->insert('helpfulness')
$this->database->insert('helpfulness')
->fields($fields)
->execute();
......@@ -131,16 +168,16 @@ class HelpfulnessBlockForm extends FormBase {
$params['helpfulness_comments'] = $form_state->getValue('helpfulness_comments');
// Get the site email address.
$site_mail = \Drupal::config('system.site')->get('mail');
$site_mail = $this->config('system.site')->get('mail');
// Default language of the site.
$language = \Drupal::languageManager()->getDefaultLanguage();
$language = $this->languageManager->getDefaultLanguage();
// Send the email.
\Drupal::service('plugin.manager.mail')->mail('helpfulness', 'new_feedback_notification', $notification_email, $language, $params, $site_mail, TRUE);
$this->mailManager->mail('helpfulness', 'new_feedback_notification', $notification_email, $language, $params, $site_mail);
}
drupal_set_message(t('Thank you for your feedback.'));
drupal_set_message($this->t('Thank you for your feedback.'));
}
}
......@@ -2,10 +2,10 @@
namespace Drupal\helpfulness\Form;
use Drupal\Core\Url;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Builds the form to confirm the deletion of a feedback submission.
......@@ -19,6 +19,22 @@ class HelpfulnessConfirmDeleteFeedbackForm extends ConfirmFormBase {
*/
protected $ids;
/**
* A database connection service instance.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->database = $container->get('database');
return $instance;
}
/**
* {@inheritdoc}
*/
......@@ -30,7 +46,7 @@ class HelpfulnessConfirmDeleteFeedbackForm extends ConfirmFormBase {
* {@inheritdoc}
*/
public function getQuestion() {
return t('Do you want to delete the feedbacks?');
return $this->t('Do you want to delete the feedbacks?');
}
/**
......@@ -45,23 +61,23 @@ class HelpfulnessConfirmDeleteFeedbackForm extends ConfirmFormBase {
*/
public function getDescription() {
if (count($this->ids) > 1) {
return t('Are you sure that you want to delete the selected %count feedback items?', ['%count' => count($this->ids)]);
return $this->t('Are you sure that you want to delete the selected %count feedback items?', ['%count' => count($this->ids)]);
}
return t('Are you sure that you want to delete the selected feedback item?');
return $this->t('Are you sure that you want to delete the selected feedback item?');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Yes, delete!');
return $this->t('Yes, delete!');
}
/**
* {@inheritdoc}
*/
public function getCancelText() {
return t('Cancel');
return $this->t('Cancel');
}
/**
......@@ -79,10 +95,10 @@ class HelpfulnessConfirmDeleteFeedbackForm extends ConfirmFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
if (!empty($this->ids)) {
// Build the update query and execute.
$db = Database::getConnection();
$query = $db->update('helpfulness');
$query->fields(['status' => HELPFULNESS_STATUS_DELETED]);
$query->condition('fid', $this->ids, 'IN');
/** @var \Drupal\Core\Database\Query\Update $query */
$query = $this->database->update('helpfulness')
->fields(['status' => HELPFULNESS_STATUS_DELETED])
->condition('fid', $this->ids, 'IN');
$query->execute();
}
......
......@@ -4,13 +4,37 @@ namespace Drupal\helpfulness\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Database;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a form for the report download.
*/
class HelpfulnessReportDownloadForm extends FormBase {
/**
* A database connection service instance.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* An entity type manager service instance.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->database = $container->get('database');
$instance->entityTypeManager = $container->get('entity_type.manager');
return $instance;
}
/**
* {@inheritdoc}
*/
......@@ -26,17 +50,17 @@ class HelpfulnessReportDownloadForm extends FormBase {
// Status selection.
$form['status'] = [
'#type' => 'select',
'#title' => t('Download messages of status:'),
'#title' => $this->t('Download messages of status:'),
'#options' => [
'-1' => t('New & Archived'),
HELPFULNESS_STATUS_OPEN => t('New'),
HELPFULNESS_STATUS_ARCHIVED => t('Archived'),
'-1' => $this->t('New & Archived'),
HELPFULNESS_STATUS_OPEN => $this->t('New'),
HELPFULNESS_STATUS_ARCHIVED => $this->t('Archived'),
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Download'),
'#value' => $this->t('Download'),
];
return $form;
......@@ -54,9 +78,9 @@ class HelpfulnessReportDownloadForm extends FormBase {
// Build the query to retrieve the feedbacks.
// Build the query.
$query = Database::getConnection()->select('helpfulness', 'hf');
$query->fields('hf');
$query->orderBy('timestamp', 'ASC');
$query = $this->database->select('helpfulness', 'hf')
->fields('hf')
->orderBy('timestamp', 'ASC');
switch ($requested_status) {
case HELPFULNESS_STATUS_OPEN:
......@@ -75,15 +99,15 @@ class HelpfulnessReportDownloadForm extends FormBase {
}
// Header for the output file.
$csv_output = t('"Status",');
$csv_output .= t('"User ID",');
$csv_output .= t('"Helpful",');
$csv_output .= t('"Message",');
$csv_output .= t('"Base URL",');
$csv_output .= t('"System Path",');
$csv_output .= t('"Path Alias",');
$csv_output .= t('"Browser Information",');
$csv_output .= t('"Time"');
$csv_output = $this->t('"Status",');
$csv_output .= $this->t('"User ID",');
$csv_output .= $this->t('"Helpful",');
$csv_output .= $this->t('"Message",');
$csv_output .= $this->t('"Base URL",');
$csv_output .= $this->t('"System Path",');
$csv_output .= $this->t('"Path Alias",');
$csv_output .= $this->t('"Browser Information",');
$csv_output .= $this->t('"Time"');
$csv_output .= "\n";
// Add the data from all requested feedbacks.
......@@ -91,35 +115,35 @@ class HelpfulnessReportDownloadForm extends FormBase {
// Status.
switch ($row->status) {
case HELPFULNESS_STATUS_OPEN:
$csv_output .= t('"New",');
$csv_output .= $this->t('"New",');
break;
case HELPFULNESS_STATUS_ARCHIVED:
$csv_output .= t('"Archived",');
$csv_output .= $this->t('"Archived",');
break;
default:
$csv_output .= t('"Unknown",');
$csv_output .= $this->t('"Unknown",');
break;
}
// User Id, and user name for convenience.
if ($row->uid == 0) {
$username = t('Anonymous');
$username = $this->t('Anonymous');
}
else {
/** @var \Drupal\user\Entity\User|null $tmp_user */
$tmp_user = \Drupal::entityTypeManager()->getStorage('user')->load($row->uid);
$username = $tmp_user ? $tmp_user->getDisplayName() : t('Deleted');
$tmp_user = $this->entityTypeManager->getStorage('user')->load($row->uid);
$username = $tmp_user ? $tmp_user->getDisplayName() : $this->t('Deleted');
}
$csv_output .= '"' . $row->uid . ' (\'' . $username . '\')",';
// Helpfulnes Rating.
if ($row->helpfulness) {
$csv_output .= t('"Yes",');
$csv_output .= $this->t('"Yes",');
}
else {
$csv_output .= t('"No",');
$csv_output .= $this->t('"No",');
}
// Feedback message.
......@@ -142,7 +166,7 @@ class HelpfulnessReportDownloadForm extends FormBase {
}// End foreach
// Build the filename and start the download.
$prefix = t('feedbacks');
$prefix = $this->t('feedbacks');
$filename = $prefix . "_" . format_date(time(), 'custom', "Y-m-d_His");
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: filename=" . $filename . ".csv");
......
......@@ -2,18 +2,42 @@
namespace Drupal\helpfulness\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
use Drupal\Core\Form\FormBase;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Query\TableSortExtender;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a form showing the submitted feedbacks.
*/
class HelpfulnessReportForm extends FormBase {
/**
* A database connection service instance.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* An entity type manager service instance.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->database = $container->get('database');
$instance->entityTypeManager = $container->get('entity_type.manager');
return $instance;
}
/**
* {@inheritdoc}
*/
......@@ -29,64 +53,67 @@ class HelpfulnessReportForm extends FormBase {
// Include the css.
$form['#attached']['library'] = ['helpfulness/helpfulness-block-form'];
$config = $this->config('helpfulness.settings');
// Build the array for the header of the table.
$header = [];
if (\Drupal::config('helpfulness.settings')->get('helpfulness_report_option_display_username')) {
if ($config->get('helpfulness_report_option_display_username')) {
$header['uid'] = [
'data' => t('User'),
'data' => $this->t('User'),
'field' => 'uid',
];
}
if (\Drupal::config('helpfulness.settings')->get('helpfulness_report_option_display_helpfulness')) {
if ($config->get('helpfulness_report_option_display_helpfulness')) {
$header['helpfulness'] = [
'data' => t('Helpful'),
'data' => $this->t('Helpful'),
'field' => 'helpfulness',
];
}
if (\Drupal::config('helpfulness.settings')->get('helpfulness_report_option_display_message')) {
if ($config->get('helpfulness_report_option_display_message')) {
$header['message'] = [
'data' => t('Message'),
'data' => $this->t('Message'),
'field' => 'message',
];
}
if (\Drupal::config('helpfulness.settings')->get('helpfulness_report_option_display_base_url')) {
if ($config->get('helpfulness_report_option_display_base_url')) {
$header['base_url'] = [
'data' => t('Base URL'),
'data' => $this->t('Base URL'),
'field' => 'base_url',
];
}
if (\Drupal::config('helpfulness.settings')->get('helpfulness_report_option_display_system_path')) {
if ($config->get('helpfulness_report_option_display_system_path')) {
$header['system_path'] = [
'data' => t('System Path'),
'data' => $this->t('System Path'),
'field' => 'system_path',
];
}
if (\Drupal::config('helpfulness.settings')->get('helpfulness_report_option_display_alias')) {
if ($config->get('helpfulness_report_option_display_alias')) {
$header['path_alias'] = [
'data' => t('Alias'),
'data' => $this->t('Alias'),
'field' => 'path_alias',
];
}
if (\Drupal::config('helpfulness.settings')->get('helpfulness_report_option_display_date')) {
if ($config->get('helpfulness_report_option_display_date')) {
$header['date'] = [
'data' => t('Date'),
'data' => $this->t('Date'),
'field' => 'timestamp',
];
}
if (\Drupal::config('helpfulness.settings')->get('helpfulness_report_option_display_time')) {
$header['time'] = t('Time');
if ($config->get('helpfulness_report_option_display_time')) {
$header['time'] = $this->t('Time');
}
if (\Drupal::config('helpfulness.settings')->get('helpfulness_report_option_display_useragent')) {
if ($config->get('helpfulness_report_option_display_useragent')) {
$header['useragent'] = [
'data' => t('Browser'),
'data' => $this->t('Browser'),
'field' => 'useragent',
];
}
// Setting the sort conditions.
if (isset($_GET['sort']) && isset($_GET['order'])) {
$query = $this->getRequest()->query;
if ($query->get('sort') && $query->get('order')) {
// Sort it Ascending or Descending?
if ($_GET['sort'] == 'asc') {
if ($query->get('sort') === 'asc') {
$sort = 'ASC';
}
else {
......@@ -94,7 +121,7 @@ class HelpfulnessReportForm extends FormBase {
}
// Which column will be sorted.
switch ($_GET['order']) {
switch ($query->get('order')) {
case 'User ID':
$order = 'uid';
break;
......@@ -141,7 +168,7 @@ class HelpfulnessReportForm extends FormBase {
// Build the query.
/** @var \Drupal\Core\Database\Query\TableSortExtender $query */
$query = Database::getConnection()->select('helpfulness', 'hf')
$query = $this->database->select('helpfulness', 'hf')
->fields('hf')
->orderBy($order, $sort)
->extend(TableSortExtender::class);
......@@ -171,8 +198,8 @@ class HelpfulnessReportForm extends FormBase {
$option = [];
/** @var \Drupal\user\Entity\User|null $tmp_user */
$tmp_user = \Drupal::entityTypeManager()->getStorage('user')->load($feedback['uid']);
$username = $tmp_user ? $tmp_user->getDisplayName() : t('Deleted');
$tmp_user = $this->entityTypeManager->getStorage('user')->load($feedback['uid']);
$username = $tmp_user ? $tmp_user->getDisplayName() : $this->t('Deleted');
$option['uid'] = $feedback['uid'] . ' (' . $username . ')';
$option['helpfulness'] = ($feedback['helpfulness']) ? 'Yes' : 'No';
......@@ -225,19 +252,19 @@ class HelpfulnessReportForm extends FormBase {
// Update actions.
$options = [
'' => t('Please Select...'),
HELPFULNESS_STATUS_OPEN => t('Set as "new"'),
HELPFULNESS_STATUS_ARCHIVED => t('Archive'),
'' => $this->t('Please Select...'),
HELPFULNESS_STATUS_OPEN => $this->t('Set as "new"'),
HELPFULNESS_STATUS_ARCHIVED => $this->t('Archive'),
];
// If the user has permissions to delete feedbacks add that option as well.
if (\Drupal::currentUser()->hasPermission('delete feedback')) {
$options += [HELPFULNESS_STATUS_DELETED => t('Delete')];
if ($this->currentUser()->hasPermission('delete feedback')) {
$options += [HELPFULNESS_STATUS_DELETED => $this->t('Delete')];
}
$form['update_action'] = [
'#type' => 'select',
'#title' => t('Update options:'),
'#title' => $this->t('Update options:'),
'#options' => $options,
'#required' => TRUE,
];
......@@ -245,33 +272,33 @@ class HelpfulnessReportForm extends FormBase {
// Submit Button.
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Update'),
'#value' => $this->t('Update'),
];
// Add the tables for open feedbacks to the form.
$form['helpfulness_feedback_open'] = [
'#type' => 'fieldset',
'#title' => t('New feedback'),
'#title' => $this->t('New feedback'),
];
$form['helpfulness_feedback_open']['helpfulness_feedbacks_open_table'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options_open,
'#empty' => t('No new feedback found'),
'#empty' => $this->t('No new feedback found'),
];
// Add the tables for archived feedbacks to the form.
$form['helpfulness_feedbacks_archived'] = [
'#type' => 'fieldset',
'#title' => t('Archived feedback'),
'#title' => $this->t('Archived feedback'),
];
$form['helpfulness_feedbacks_archived']['helpfulness_feedbacks_archived_table'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options_archived,
'#empty' => t('No archived feedback found'),
'#empty' => $this->t('No archived feedback found'),
];
return $form;
......@@ -317,10 +344,10 @@ class HelpfulnessReportForm extends FormBase {
}
// Build the update query and execute.
$db = Database::getConnection();
$query = $db->update('helpfulness');
$query->fields(['status' => $action]);
$query->condition('fid', $selected_fids, 'IN');
/** @var \Drupal\Core\Database\Query\Update $query */
$query = $this->database->update('helpfulness')
->fields(['status' => $action])
->condition('fid', $selected_fids, 'IN');
$query->execute();
}
......
......@@ -27,76 +27,76 @@ class HelpfulnessReportOptionsForm extends FormBase {
$form['helpfulness_report_option_info'] = [
'#type' => 'item',
'#title' => t('Select the columns you would like to have displayed in the report:'),
'#title' => $this->t('Select the columns you would like to have displayed in the report:'),
];
// Options for the display of columns in the feedback report.
$form['helpfulness_report_option_display_username'] = [
'#type' => 'checkbox',
'#title' => t('User'),
'#title' => $this->t('User'),
'#return_value' => 1,
'#default_value' => $config->get('helpfulness_report_option_display_username'),
];
$form['helpfulness_report_option_display_helpfulness'] = [
'#type' => 'checkbox',
'#title' => t('Helpfulness Rating'),
'#title' => $this->t('Helpfulness Rating'),
'#return_value' => 1,
'#default_value' => $config->get('helpfulness_report_option_display_helpfulness'),
];
$form['helpfulness_report_option_display_message'] = [
'#type' => 'checkbox',
'#title' => t('Message'),
'#title' => $this->t('Message'),
'#return_value' => 1,
'#default_value' => $config->get('helpfulness_report_option_display_message'),
];
$form['helpfulness_report_option_display_base_url'] = [
'#type' => 'checkbox',
'#title' => t('Base URL'),
'#title' => $this->t('Base URL'),
'#return_value' => 1,
'#default_value' => $config->get('helpfulness_report_option_display_base_url'),
];
$form['helpfulness_report_option_display_system_path'] = [
'#type' => 'checkbox',
'#title' => t('System Path'),
'#title' => $this->t('System Path'),
'#return_value' => 1,
'#default_value' => $config->get('helpfulness_report_option_display_system_path'),
];
$form['helpfulness_report_option_display_alias'] = [
'#type' => 'checkbox',
'#title' => t('Alias'),
'#title' => $this->t('Alias'),
'#return_value' => 1,
'#default_value' => $config->get('helpfulness_report_option_display_alias'),
];
$form['helpfulness_report_option_display_date'] = [
'#type' => 'checkbox',
'#title' => t('Date'),
'#title' => $this->t('Date'),
'#return_value' => 1,
'#default_value' => $config->get('helpfulness_report_option_display_date'),
];
$form['helpfulness_report_option_display_time'] = [
'#type' => 'checkbox',
'#title' => t('Time'),
'#title' => $this->t('Time'),
'#return_value' => 1,
'#default_value' => $config->get('helpfulness_report_option_display_time'),
];
$form['helpfulness_report_option_display_useragent'] = [
'#type' => 'checkbox',
'#title' => t('Browser Info'),
'#title' => $this->t('Browser Info'),
'#return_value' => 1,
'#default_value' => $config->get('helpfulness_report_option_display_useragent'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Update'),
'#value' => $this->t('Update'),
];
return $form;
......@@ -127,7 +127,7 @@ class HelpfulnessReportOptionsForm extends FormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Get the configuration.
$config = \Drupal::configFactory()->getEditable('helpfulness.settings');
$config = $this->configFactory()->getEditable('helpfulness.settings');
$config->set('helpfulness_report_option_display_username', $form_state->getValue('helpfulness_report_option_display_username'))
->set('helpfulness_report_option_display_helpfulness', $form_state->getValue('helpfulness_report_option_display_helpfulness'))
......
......@@ -4,6 +4,9 @@ namespace Drupal\helpfulness\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\helpfulness\Form\HelpfulnessBlockForm;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a 'Helpfulness' block.
......@@ -13,13 +16,29 @@ use Drupal\Core\Block\BlockPluginInterface;
* admin_label = @Translation("Helpfulness block"),
* )
*/
class HelpfulnessBlock extends BlockBase implements BlockPluginInterface {
class HelpfulnessBlock extends BlockBase implements BlockPluginInterface, ContainerFactoryPluginInterface {
/**
* A form builder service instance.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
$instance->formBuilder = $container->get('form_builder');
return $instance;
}
/**
* {@inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('\Drupal\helpfulness\Form\HelpfulnessBlockForm');
$form = $this->formBuilder->getForm(HelpfulnessBlockForm::class);
return $form;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment