Skip to content

Commit 512330b

Browse files
committed
CS cleanups, remove t() from tests and otherwise, some test cleanups
1 parent 000c39f commit 512330b

File tree

41 files changed

+183
-325
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+183
-325
lines changed

ajax_example/ajax_example.routing.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ ajax_example.autocomplete_user:
9191
path: '/examples/ajax_example/user_autocomplete'
9292
defaults:
9393
_form: '\Drupal\ajax_example\Form\EntityAutocomplete'
94-
_title: 'Autocomplete users with entity_autocomplete.'
94+
_title: 'Autocomplete users with entity_autocomplete'
9595
requirements:
9696
_permission: 'access content'

ajax_example/src/Form/DynamicFormSections.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $nojs = N
6060
$form['question_type_select'] = [
6161
// This is our select dropdown.
6262
'#type' => 'select',
63-
'#title' => t('Question style'),
63+
'#title' => $this->t('Question style'),
6464
// We have a variety of form items you can use to get input from the user.
6565
'#options' => [
6666
'Choose question style' => 'Choose question style',
@@ -80,7 +80,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $nojs = N
8080
// The CSS for this module hides this next button if JS is enabled.
8181
$form['question_type_submit'] = [
8282
'#type' => 'submit',
83-
'#value' => t('Choose'),
83+
'#value' => $this->t('Choose'),
8484
'#attributes' => ['class' => ['ajax-example-inline']],
8585
// No need to validate when submitting this.
8686
'#limit_validation_errors' => [],
@@ -117,7 +117,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $nojs = N
117117
if (!empty($question_type) && $question_type !== 'Choose question style') {
118118

119119
$form['questions_fieldset']['question'] = [
120-
'#markup' => t('Who was the first president of the U.S.?'),
120+
'#markup' => $this->t('Who was the first president of the U.S.?'),
121121
];
122122

123123
// Build up a secondary form, based on the type of question the user
@@ -126,7 +126,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $nojs = N
126126
case 'Multiple Choice':
127127
$form['questions_fieldset']['question'] = [
128128
'#type' => 'radios',
129-
'#title' => t('Who was the first president of the United States'),
129+
'#title' => $this->t('Who was the first president of the United States'),
130130
'#options' => [
131131
'George Bush' => 'George Bush',
132132
'Adam McGuire' => 'Adam McGuire',

ajax_example/src/Form/EntityAutocomplete.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ public function getFormId() {
6969
*/
7070
public function buildForm(array $form, FormStateInterface $form_state) {
7171
$form['info'] = [
72-
'#markup' => '<div>' . t("This example uses the entity_autocomplete form "
73-
. "element to select users. You'll need a few users on your system for "
74-
. "it to make sense.") . '</div>',
72+
'#markup' => '<div>' . $this->t("This example uses the <code>entity_autocomplete</code> form element to select users. You'll need a few users on your system for it to make sense.") . '</div>',
7573
];
7674

7775
// Here we use the delightful entity_autocomplete form element. It allows us
@@ -85,7 +83,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
8583
// Specifying #tags as TRUE allows for multiple selections, separated by
8684
// commas.
8785
'#tags' => TRUE,
88-
'#title' => t('Choose a user. Separate with commas.'),
86+
'#title' => $this->t('Choose a user (Separate with commas)'),
8987
];
9088

9189
$form['actions'] = [

ajax_example/src/Form/Wizard.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $no_js_us
3737
// We want to deal with hierarchical form values.
3838
$form['#tree'] = TRUE;
3939
$form['description'] = [
40-
'#markup' => t('This example is a step-by-step wizard. The @link does it without page reloads; the @link1 is the same code but simulates a non-javascript environment, showing it with page reloads.', [
40+
'#markup' => $this->t('This example is a step-by-step wizard. The @link does it without page reloads; the @link1 is the same code but simulates a non-javascript environment, showing it with page reloads.', [
4141
'@link' => $linktwo,
4242
'@link1' => $link,
4343
]),
@@ -155,7 +155,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
155155
unset($form['next']);
156156
$form['step2'] = [
157157
'#type' => 'fieldset',
158-
'#title' => t('Step 2: Street address info'),
158+
'#title' => $this->t('Step 2: Street address info'),
159159
];
160160
$form['step2']['address'] = [
161161
'#type' => 'textfield',
@@ -197,7 +197,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
197197
if ($step > 1 && !isset($form['prev'])) {
198198
$form['prev'] = [
199199
'#type' => 'submit',
200-
'#value' => t("Previous step"),
200+
'#value' => $this->t("Previous step"),
201201
// Since all info will be discarded, don't validate on 'prev'.
202202
'#limit_validation_errors' => [],
203203
// #submit is required to use #limit_validation_errors.

batch_example/src/Form/BatchExampleForm.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public function buildForm(array $form, FormStateInterface $form_state) {
2424

2525
$form['description'] = [
2626
'#type' => 'markup',
27-
'#markup' => t('This example offers two different batches. The first does 1000 identical operations, each completed in on run; the second does 20 operations, but each takes more than one run to operate if there are more than 5 nodes.'),
27+
'#markup' => $this->t('This example offers two different batches. The first does 1000 identical operations, each completed in on run; the second does 20 operations, but each takes more than one run to operate if there are more than 5 nodes.'),
2828
];
2929
$form['batch'] = [
3030
'#type' => 'select',
3131
'#title' => 'Choose batch',
3232
'#options' => [
33-
'batch_1' => t('batch 1 - 1000 operations'),
34-
'batch_2' => t('batch 2 - 20 operations.'),
33+
'batch_1' => $this->t('batch 1 - 1000 operations'),
34+
'batch_2' => $this->t('batch 2 - 20 operations.'),
3535
],
3636
];
3737
$form['submit'] = [
@@ -93,12 +93,12 @@ public function generateBatch1() {
9393
'batch_example_op_1',
9494
[
9595
$i + 1,
96-
t('(Operation @operation)', ['@operation' => $i]),
96+
$this->t('(Operation @operation)', ['@operation' => $i]),
9797
],
9898
];
9999
}
100100
$batch = [
101-
'title' => t('Creating an array of @num operations', ['@num' => $num_operations]),
101+
'title' => $this->t('Creating an array of @num operations', ['@num' => $num_operations]),
102102
'operations' => $operations,
103103
'finished' => 'batch_example_finished',
104104
];
@@ -123,7 +123,7 @@ public function generateBatch2() {
123123
for ($i = 0; $i < $num_operations; $i++) {
124124
$operations[] = [
125125
'batch_example_op_2',
126-
[t('(Operation @operation)', ['@operation' => $i])],
126+
[$this->t('(Operation @operation)', ['@operation' => $i])],
127127
];
128128
}
129129
$batch = [
@@ -137,10 +137,10 @@ public function generateBatch2() {
137137
// operations, so @total will always be 20, even though there are multiple
138138
// nodes per operation.
139139
// Defaults to t('Completed @current of @total.').
140-
'title' => t('Processing batch 2'),
141-
'init_message' => t('Batch 2 is starting.'),
142-
'progress_message' => t('Processed @current out of @total.'),
143-
'error_message' => t('Batch 2 has encountered an error.'),
140+
'title' => $this->t('Processing batch 2'),
141+
'init_message' => $this->t('Batch 2 is starting.'),
142+
'progress_message' => $this->t('Processed @current out of @total.'),
143+
'error_message' => $this->t('Batch 2 has encountered an error.'),
144144
];
145145
return $batch;
146146
}

block_example/src/Plugin/Block/ExampleUppercaseBlock.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ExampleUppercaseBlock extends BlockBase {
1919
*/
2020
public function build() {
2121
return [
22-
'#markup' => t("This block's title is changed to uppercase. Any block title which contains 'uppercase' will also be changed to uppercase."),
22+
'#markup' => $this->t("This block's title is changed to uppercase. Any block title which contains 'uppercase' will also be changed to uppercase."),
2323
];
2424
}
2525

content_entity_example/tests/src/Functional/ContentEntityExampleTest.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Drupal\content_entity_example\Tests;
3+
namespace Drupal\Tests\content_entity_example\Functional;
44

55
use Drupal\content_entity_example\Entity\Contact;
66
use Drupal\Tests\examples\Functional\ExamplesBrowserTestBase;
@@ -35,19 +35,19 @@ public function testContentEntityExample() {
3535
]);
3636

3737
// Anonymous User should not see the link to the listing.
38-
$assert->pageTextNotContains('Content Entity Example: Contacts Listing');
38+
$assert->pageTextNotContains('Content Entity Example: Contacts listing');
3939

4040
$this->drupalLogin($web_user);
4141

4242
// Web_user user has the right to view listing.
43-
$assert->linkExists('Content Entity Example: Contacts Listing');
43+
$assert->linkExists('Content Entity Example: Contacts listing');
4444

45-
$this->clickLink('Content Entity Example: Contacts Listing');
45+
$this->clickLink('Content Entity Example: Contacts listing');
4646

4747
// WebUser can add entity content.
48-
$assert->linkExists('Add Contact');
48+
$assert->linkExists('Add contact');
4949

50-
$this->clickLink(t('Add Contact'));
50+
$this->clickLink(t('Add contact'));
5151

5252
$assert->fieldValueEquals('name[0][value]', '');
5353
$assert->fieldValueEquals('name[0][value]', '');
@@ -77,7 +77,7 @@ public function testContentEntityExample() {
7777
$assert->pageTextContains('test first name');
7878
$assert->pageTextContains('administrator');
7979
$assert->pageTextContains('male');
80-
$assert->linkExists('Add Contact');
80+
$assert->linkExists('Add contact');
8181
$assert->linkExists('Edit');
8282
$assert->linkExists('Delete');
8383

@@ -109,14 +109,12 @@ public function testPaths() {
109109
$assert = $this->assertSession();
110110

111111
// Generate a contact so that we can test the paths against it.
112-
$contact = Contact::create(
113-
[
114-
'name' => 'somename',
115-
'first_name' => 'Joe',
116-
'gender' => 'female',
117-
'role' => 'administrator',
118-
]
119-
);
112+
$contact = Contact::create([
113+
'name' => 'somename',
114+
'first_name' => 'Joe',
115+
'gender' => 'female',
116+
'role' => 'administrator',
117+
]);
120118
$contact->save();
121119

122120
// Gather the test data.

dbtng_example/src/Controller/DbtngExampleController.php

+15-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ public function entryList() {
2121
];
2222

2323
$rows = [];
24-
$headers = [t('Id'), t('uid'), t('Name'), t('Surname'), t('Age')];
24+
$headers = [
25+
$this->t('Id'),
26+
$this->t('uid'),
27+
$this->t('Name'),
28+
$this->t('Surname'),
29+
$this->t('Age'),
30+
];
2531

2632
foreach ($entries = DbtngExampleStorage::load() as $entry) {
2733
// Sanitize each entry.
@@ -31,7 +37,7 @@ public function entryList() {
3137
'#type' => 'table',
3238
'#header' => $headers,
3339
'#rows' => $rows,
34-
'#empty' => t('No entries available.'),
40+
'#empty' => $this->t('No entries available.'),
3541
];
3642
// Don't cache this page.
3743
$content['#cache']['max-age'] = 0;
@@ -46,16 +52,15 @@ public function entryAdvancedList() {
4652
$content = [];
4753

4854
$content['message'] = [
49-
'#markup' => $this->t('A more complex list of entries in the database.') . ' ' .
50-
$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.'),
55+
'#markup' => $this->t('A more complex list of entries in the database. 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.'),
5156
];
5257

5358
$headers = [
54-
t('Id'),
55-
t('Created by'),
56-
t('Name'),
57-
t('Surname'),
58-
t('Age'),
59+
$this->t('Id'),
60+
$this->t('Created by'),
61+
$this->t('Name'),
62+
$this->t('Surname'),
63+
$this->t('Age'),
5964
];
6065

6166
$rows = [];
@@ -68,7 +73,7 @@ public function entryAdvancedList() {
6873
'#header' => $headers,
6974
'#rows' => $rows,
7075
'#attributes' => ['id' => 'dbtng-example-advanced-list'],
71-
'#empty' => t('No entries available.'),
76+
'#empty' => $this->t('No entries available.'),
7277
];
7378
// Don't cache this page.
7479
$content['#cache']['max-age'] = 0;

dbtng_example/src/Form/DbtngExampleUpdateForm.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function buildForm(array $form, FormStateInterface $form_state) {
3636
// Tell the user if there is nothing to display.
3737
if (empty($entries)) {
3838
$form['no_values'] = [
39-
'#value' => t('No entries exist in the table dbtng_example table.'),
39+
'#value' => $this->t('No entries exist in the table dbtng_example table.'),
4040
];
4141
return $form;
4242
}
4343

4444
$keyed_entries = [];
4545
foreach ($entries as $entry) {
46-
$options[$entry->pid] = t('@pid: @name @surname (@age)', [
46+
$options[$entry->pid] = $this->t('@pid: @name @surname (@age)', [
4747
'@pid' => $entry->pid,
4848
'@name' => $entry->name,
4949
'@surname' => $entry->surname,
@@ -64,7 +64,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
6464
$form['pid'] = [
6565
'#type' => 'select',
6666
'#options' => $options,
67-
'#title' => t('Choose entry to update'),
67+
'#title' => $this->t('Choose entry to update'),
6868
'#default_value' => $default_entry->pid,
6969
'#ajax' => [
7070
'wrapper' => 'updateform',
@@ -74,28 +74,28 @@ public function buildForm(array $form, FormStateInterface $form_state) {
7474

7575
$form['name'] = [
7676
'#type' => 'textfield',
77-
'#title' => t('Updated first name'),
77+
'#title' => $this->t('Updated first name'),
7878
'#size' => 15,
7979
'#default_value' => $default_entry->name,
8080
];
8181

8282
$form['surname'] = [
8383
'#type' => 'textfield',
84-
'#title' => t('Updated last name'),
84+
'#title' => $this->t('Updated last name'),
8585
'#size' => 15,
8686
'#default_value' => $default_entry->surname,
8787
];
8888
$form['age'] = [
8989
'#type' => 'textfield',
90-
'#title' => t('Updated age'),
90+
'#title' => $this->t('Updated age'),
9191
'#size' => 4,
9292
'#default_value' => $default_entry->age,
93-
'#description' => t('Values greater than 127 will cause an exception'),
93+
'#description' => $this->t('Values greater than 127 will cause an exception'),
9494
];
9595

9696
$form['submit'] = [
9797
'#type' => 'submit',
98-
'#value' => t('Update'),
98+
'#value' => $this->t('Update'),
9999
];
100100
return $form;
101101
}
@@ -125,7 +125,7 @@ public function updateCallback(array $form, FormStateInterface $form_state) {
125125
public function validateForm(array &$form, FormStateInterface $form_state) {
126126
// Confirm that age is numeric.
127127
if (!intval($form_state->getValue('age'))) {
128-
$form_state->setErrorByName('age', t('Age needs to be a number'));
128+
$form_state->setErrorByName('age', $this->t('Age needs to be a number'));
129129
}
130130
}
131131

@@ -144,7 +144,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
144144
'uid' => $account->id(),
145145
];
146146
$count = DbtngExampleStorage::update($entry);
147-
$this->messenger()->addMessage(t('Updated entry @entry (@count row updated)', [
147+
$this->messenger()->addMessage($this->t('Updated entry @entry (@count row updated)', [
148148
'@count' => $count,
149149
'@entry' => print_r($entry, TRUE),
150150
]));

0 commit comments

Comments
 (0)