Commit 82ea3675 authored by TravisCarden's avatar TravisCarden

By TravisCarden: Updated traditional array literal syntax.

parent 537c8a1d
......@@ -70,27 +70,27 @@
* @see hook_checklistapi_checklist_info_alter()
*/
function hook_checklistapi_checklist_info() {
$definitions = array();
$definitions['example_checklist'] = array(
$definitions = [];
$definitions['example_checklist'] = [
'#title' => t('Example checklist'),
'#path' => 'example-checklist',
'#description' => t('An example checklist.'),
'#help' => t('<p>This is an example checklist.</p>'),
'example_group' => array(
'example_group' => [
'#title' => t('Example group'),
'#description' => t('<p>Here are some example items.</p>'),
'example_item_1' => array(
'example_item_1' => [
'#title' => t('Example item 1'),
'example_link' => array(
'example_link' => [
'#text' => t('Example.com'),
'#url' => \Drupal\Core\Url::fromUri('http://www.example.com/'),
),
),
'example_item_2' => array(
],
],
'example_item_2' => [
'#title' => t('Example item 2'),
),
),
);
],
],
];
return $definitions;
}
......@@ -112,13 +112,13 @@ function hook_checklistapi_checklist_info() {
*/
function hook_checklistapi_checklist_info_alter(array &$definitions) {
// Add an item.
$definitions['example_checklist']['example_group']['new_item'] = array(
$definitions['example_checklist']['example_group']['new_item'] = [
'title' => t('New item'),
);
];
// Add a group.
$definitions['example_checklist']['new_group'] = array(
$definitions['example_checklist']['new_group'] = [
'#title' => t('New group'),
);
];
// Move an item.
$definitions['example_checklist']['new_group']['example_item_1'] = $definitions['example_checklist']['example_group']['example_item_1'];
unset($definitions['example_checklist']['example_group']['example_item_1']);
......
......@@ -12,20 +12,20 @@ use \Drupal\user\Entity\User;
* Implements hook_drush_command().
*/
function checklistapi_drush_command() {
$items = array();
$items = [];
$items['checklistapi-list'] = array(
'aliases' => array('capi-list', 'capil'),
$items['checklistapi-list'] = [
'aliases' => ['capi-list', 'capil'],
'description' => 'Get an overview of your installed checklists with progress details.',
);
$items['checklistapi-info'] = array(
'aliases' => array('capi-info', 'capii'),
];
$items['checklistapi-info'] = [
'aliases' => ['capi-info', 'capii'],
'description' => 'Show detailed info for a given checklist.',
'arguments' => array(
'arguments' => [
'checklist' => 'The checklist machine name, e.g., "example_checklist".',
),
],
'required-arguments' => TRUE,
);
];
return $items;
}
......@@ -51,26 +51,26 @@ function drush_checklistapi_list() {
}
// Build table rows.
$rows = array();
$rows = [];
// The first row is the table header.
$rows[] = array(
$rows[] = [
dt('Checklist'),
dt('Progress'),
dt('Last updated'),
dt('Last updated by'),
);
];
foreach ($definitions as $id => $definition) {
$checklist = checklistapi_checklist_load($id);
$row = array();
$row[] = dt('!title (!id)', array(
$row = [];
$row[] = dt('!title (!id)', [
'!title' => strip_tags($checklist->title),
'!id' => $id,
));
$row[] = dt('@completed of @total (@percent%)', array(
]);
$row[] = dt('@completed of @total (@percent%)', [
'@completed' => $checklist->getNumberCompleted(),
'@total' => $checklist->getNumberOfItems(),
'@percent' => round($checklist->getPercentComplete()),
));
]);
$row[] = $checklist->getLastUpdatedDate();
$row[] = strip_tags($checklist->getLastUpdatedUser());
$rows[] = $row;
......@@ -91,9 +91,9 @@ function drush_checklistapi_info_validate() {
// Make sure the given checklist exists.
if (!$checklist) {
drush_set_error('CHECKLISTAPI_INVALID_ARGUMENT', dt('No such checklist "!id".', array(
drush_set_error('CHECKLISTAPI_INVALID_ARGUMENT', dt('No such checklist "!id".', [
'!id' => $id,
)));
]));
}
drush_set_context('checklist', $checklist);
......@@ -113,15 +113,15 @@ function drush_checklistapi_info() {
// Print last updated and progress details.
if ($checklist->hasSavedProgress()) {
drush_print('');
drush_print(dt('Last updated @date by !user', array(
drush_print(dt('Last updated @date by !user', [
'@date' => $checklist->getLastUpdatedDate(),
'!user' => strip_tags($checklist->getLastUpdatedUser()),
)));
drush_print(dt('@completed of @total (@percent%) complete', array(
]));
drush_print(dt('@completed of @total (@percent%) complete', [
'@completed' => $checklist->getNumberCompleted(),
'@total' => $checklist->getNumberOfItems(),
'@percent' => round($checklist->getPercentComplete()),
)));
]));
}
// Loop through groups.
......@@ -142,16 +142,16 @@ function drush_checklistapi_info() {
if ($saved_item) {
// Append completion details.
$user = User::load($saved_item['#uid']);
$title .= dt(' - Completed @time by !user', array(
$title .= dt(' - Completed @time by !user', [
'@time' => format_date($saved_item['#completed'], 'short'),
'!user' => $user->getUsername(),
));
]);
}
// Print the list item.
drush_print(dt(' [!x] !title', array(
drush_print(dt(' [!x] !title', [
'!x' => ($saved_item) ? 'x' : ' ',
'!title' => $title,
)));
]));
}
}
}
......@@ -30,7 +30,7 @@ use Drupal\Core\Routing\RouteMatchInterface;
* Throws an exception if an unsupported operation is supplied.
*/
function checklistapi_checklist_access($id, $operation = 'any') {
$all_operations = array('view', 'edit', 'any');
$all_operations = ['view', 'edit', 'any'];
if (!in_array($operation, $all_operations)) {
throw new \InvalidArgumentException(sprintf('No such operation "%s"', $operation));
}
......@@ -93,7 +93,7 @@ function checklistapi_get_checklist_info($id = NULL) {
$definitions = checklistapi_sort_array($definitions);
// Inject checklist IDs.
foreach ($definitions as $key => $value) {
$definitions[$key] = array('#id' => $key) + $definitions[$key];
$definitions[$key] = ['#id' => $key] + $definitions[$key];
}
}
if (!empty($id)) {
......@@ -143,7 +143,7 @@ function checklistapi_sort_array(array $array) {
}
$incrementer = 0;
$children = array();
$children = [];
foreach ($child_keys as $key) {
// Move child to a temporary array for sorting.
$children[$key] = $array[$key];
......@@ -160,7 +160,7 @@ function checklistapi_sort_array(array $array) {
$children[$key] = checklistapi_sort_array($children[$key]);
}
// Sort by weight.
uasort($children, array('Drupal\Component\Utility\SortArray', 'sortByWeightProperty'));
uasort($children, ['Drupal\Component\Utility\SortArray', 'sortByWeightProperty']);
// Remove incremental weight hack.
foreach ($children as $key => $child) {
$children[$key]['#weight'] = floor($children[$key]['#weight']);
......@@ -192,16 +192,16 @@ function checklistapi_strtolowercamel($string) {
* Implements hook_theme().
*/
function checklistapi_theme() {
return array(
'checklistapi_progress_bar' => array(
return [
'checklistapi_progress_bar' => [
'path' => drupal_get_path('module', 'checklistapi') . '/templates',
'template' => 'checklistapi-progress-bar',
'variables' => array(
'variables' => [
'message' => '',
'number_complete' => 0,
'number_of_items' => 0,
'percent_complete' => 0,
),
),
);
],
],
];
}
......@@ -82,7 +82,7 @@ class ChecklistapiChecklist {
*
* @var array
*/
public $items = array();
public $items = [];
/**
* The saved progress data.
......@@ -128,9 +128,9 @@ class ChecklistapiChecklist {
public function clearSavedProgress() {
$this->config->delete();
drupal_set_message(t('%title saved progress has been cleared.', array(
drupal_set_message(t('%title saved progress has been cleared.', [
'%title' => $this->title,
)));
]));
}
/**
......@@ -162,10 +162,10 @@ class ChecklistapiChecklist {
*/
public function getLastUpdatedUser() {
if (isset($this->savedProgress['#changed_by'])) {
$username = array(
$username = [
'#theme' => 'username',
'#account' => user_load($this->savedProgress['#changed_by']),
);
];
return \Drupal::service('renderer')->render($username);
}
else {
......@@ -240,11 +240,11 @@ class ChecklistapiChecklist {
$time = time();
$num_changed_items = 0;
$progress = array(
$progress = [
'#changed' => $time,
'#changed_by' => $user->id(),
'#completed_items' => 0,
);
];
// Loop through groups.
foreach ($values as $group_key => $group) {
......@@ -269,10 +269,10 @@ class ChecklistapiChecklist {
}
else {
// Item is newly checked. Set new value.
$new_item = array(
$new_item = [
'#completed' => $time,
'#uid' => $user->id(),
);
];
$num_changed_items++;
}
}
......@@ -299,7 +299,7 @@ class ChecklistapiChecklist {
$num_changed_items,
'%title progress has been saved. 1 item changed.',
'%title progress has been saved. @count items changed.',
array('%title' => $this->title)
['%title' => $this->title]
));
}
......
......@@ -29,19 +29,19 @@ class ChecklistapiPermissions {
* An array of permission details.
*/
public function universalPermissions() {
$perms['view checklistapi checklists report'] = array(
'title' => t('View the !name report', array(
$perms['view checklistapi checklists report'] = [
'title' => t('View the !name report', [
'!name' => (\Drupal::currentUser()->hasPermission('view checklistapi checklists report')) ? \Drupal::l(t('Checklists'), Url::fromRoute('checklistapi.report')) : drupal_placeholder('Checklists'),
)),
);
$perms['view any checklistapi checklist'] = array(
]),
];
$perms['view any checklistapi checklist'] = [
'title' => t('View any checklist'),
'description' => $this->viewPermissionDescription,
);
$perms['edit any checklistapi checklist'] = array(
];
$perms['edit any checklistapi checklist'] = [
'title' => t('Edit any checklist'),
'description' => $this->editPermissionDescription,
);
];
return $perms;
}
......@@ -52,7 +52,7 @@ class ChecklistapiPermissions {
* An array of permission details.
*/
public function perChecklistPermissions() {
$perms = array();
$perms = [];
// Per checklist permissions.
foreach (checklistapi_get_checklist_info() as $id => $definition) {
......@@ -68,14 +68,14 @@ class ChecklistapiPermissions {
$checklist_name = \Drupal::l($checklist->title, Url::fromRoute($checklist->getRouteName()));
}
$perms["view {$id} checklistapi checklist"] = array(
'title' => t('View the !name checklist', array('!name' => $checklist_name)),
$perms["view {$id} checklistapi checklist"] = [
'title' => t('View the !name checklist', ['!name' => $checklist_name]),
'description' => $this->viewPermissionDescription,
);
$perms["edit {$id} checklistapi checklist"] = array(
'title' => t('Edit the !name checklist', array('!name' => $checklist_name)),
];
$perms["edit {$id} checklistapi checklist"] = [
'title' => t('Edit the !name checklist', ['!name' => $checklist_name]),
'description' => $this->editPermissionDescription,
);
];
}
return $perms;
......
......@@ -23,54 +23,54 @@ class ChecklistapiController extends ControllerBase {
*/
public function report() {
// Define table header.
$header = array(
array('data' => t('Checklist')),
array(
$header = [
['data' => t('Checklist')],
[
'data' => t('Progress'),
'class' => array(RESPONSIVE_PRIORITY_MEDIUM),
),
array(
'class' => [RESPONSIVE_PRIORITY_MEDIUM],
],
[
'data' => t('Last updated'),
'class' => array(RESPONSIVE_PRIORITY_MEDIUM),
),
array(
'class' => [RESPONSIVE_PRIORITY_MEDIUM],
],
[
'data' => t('Last updated by'),
'class' => array(RESPONSIVE_PRIORITY_LOW),
),
array('data' => t('Operations')),
);
'class' => [RESPONSIVE_PRIORITY_LOW],
],
['data' => t('Operations')],
];
// Build table rows.
$rows = array();
$rows = [];
$definitions = checklistapi_get_checklist_info();
foreach ($definitions as $id => $definition) {
$checklist = checklistapi_checklist_load($id);
$row = array();
$row[] = array(
$row = [];
$row[] = [
'data' => ($checklist->userHasAccess()) ? \Drupal::l($checklist->title, $checklist->getUrl()) : drupal_placeholder($checklist->title),
'title' => (!empty($checklist->description)) ? $checklist->description : '',
);
$row[] = t('@completed of @total (@percent%)', array(
];
$row[] = t('@completed of @total (@percent%)', [
'@completed' => $checklist->getNumberCompleted(),
'@total' => $checklist->getNumberOfItems(),
'@percent' => round($checklist->getPercentComplete()),
));
]);
$row[] = $checklist->getLastUpdatedDate();
$row[] = $checklist->getLastUpdatedUser();
if ($checklist->userHasAccess('edit') && $checklist->hasSavedProgress()) {
$row[] = array(
'data' => array(
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => array(
'clear' => array(
'#links' => [
'clear' => [
'title' => t('Clear'),
'url' => Url::fromRoute($checklist->getRouteName() . '.clear', array(), array(
'url' => Url::fromRoute($checklist->getRouteName() . '.clear', [], [
'query' => $this->getDestinationArray(),
)),
),
),
),
);
]),
],
],
],
];
}
else {
$row[] = '';
......@@ -79,12 +79,12 @@ class ChecklistapiController extends ControllerBase {
}
// Compile output.
$output['table'] = array(
$output['table'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No checklists available.'),
);
];
return $output;
}
......
......@@ -47,9 +47,9 @@ class ChecklistapiChecklistClearForm extends ConfirmFormBase {
* {@inheritdoc}
*/
public function getDescription() {
return t('All progress details for %checklist will be erased. This action cannot be undone.', array(
return t('All progress details for %checklist will be erased. This action cannot be undone.', [
'%checklist' => $this->checklist->title,
));
]);
}
/**
......
......@@ -34,49 +34,49 @@ class ChecklistapiChecklistForm implements FormInterface {
$renderer = \Drupal::service('renderer');
// Progress bar.
$progress_bar = array(
$progress_bar = [
'#theme' => 'checklistapi_progress_bar',
'#message' => ($checklist->hasSavedProgress()) ? t('Last updated @date by !user', array(
'#message' => ($checklist->hasSavedProgress()) ? t('Last updated @date by !user', [
'@date' => $checklist->getLastUpdatedDate(),
'!user' => $checklist->getLastUpdatedUser(),
)) : '',
]) : '',
'#number_complete' => $checklist->getNumberCompleted(),
'#number_of_items' => $checklist->getNumberOfItems(),
'#percent_complete' => (int) round($checklist->getPercentComplete()),
);
$form['progress_bar'] = array(
];
$form['progress_bar'] = [
'#type' => 'markup',
'#markup' => $renderer->render($progress_bar),
);
];
// Compact mode.
if (checklistapi_compact_mode_is_on()) {
$form['#attributes']['class'] = array('compact-mode');
$form['#attributes']['class'] = ['compact-mode'];
}
$compact_link = array('#markup' => '<div class="compact-link"></div>');
$form['compact_mode_link'] = array(
$compact_link = ['#markup' => '<div class="compact-link"></div>'];
$form['compact_mode_link'] = [
'#markup' => $renderer->render($compact_link),
);
];
// General properties.
$form['checklistapi'] = array(
'#attached' => array(
'library' => array('checklistapi/checklistapi'),
),
$form['checklistapi'] = [
'#attached' => [
'library' => ['checklistapi/checklistapi'],
],
'#tree' => TRUE,
'#type' => 'vertical_tabs',
);
];
// Loop through groups.
$num_autochecked_items = 0;
$groups = $checklist->items;
foreach (Element::children($groups) as $group_key) {
$group = &$groups[$group_key];
$form[$group_key] = array(
$form[$group_key] = [
'#title' => Xss::filter($group['#title']),
'#type' => 'details',
'#group' => 'checklistapi',
);
];
if (!empty($group['#description'])) {
$form[$group_key]['#description'] = Xss::filterAdmin($group['#description']);
}
......@@ -89,16 +89,16 @@ class ChecklistapiChecklistForm implements FormInterface {
$title = Xss::filter($item['#title']);
if ($saved_item) {
// Append completion details.
$user = array(
$user = [
'#theme' => 'username',
'#account' => user_load($saved_item['#uid']),
);
];
$title .= t(
'<span class="completion-details"> - Completed @time by !user</a>',
array(
[
'@time' => format_date($saved_item['#completed'], 'short'),
'!user' => \Drupal::service('renderer')->render($user),
)
]
);
}
// Set default value.
......@@ -114,7 +114,7 @@ class ChecklistapiChecklistForm implements FormInterface {
// Get description.
$description = (isset($item['#description'])) ? '<p>' . Xss::filterAdmin($item['#description']) . '</p>' : '';
// Append links.
$links = array();
$links = [];
foreach (Element::children($item) as $link_key) {
$link = &$item[$link_key];
$links[] = \Drupal::l($link['#text'], $link['#url']);
......@@ -123,47 +123,47 @@ class ChecklistapiChecklistForm implements FormInterface {
$description .= '<div class="links">' . implode(' | ', $links) . '</div>';
}
// Compile the list item.
$form[$group_key][$item_key] = array(
'#attributes' => array('class' => array('checklistapi-item')),
$form[$group_key][$item_key] = [
'#attributes' => ['class' => ['checklistapi-item']],
'#default_value' => $default_value,
'#description' => Xss::filterAdmin($description),
'#disabled' => !($user_has_edit_access),
'#title' => Xss::filterAdmin($title),
'#type' => 'checkbox',
'#group' => $group_key,
'#parents' => array('checklistapi', $group_key, $item_key),
);
'#parents' => ['checklistapi', $group_key, $item_key],
];
}
}
// Actions.
$form['actions'] = array(
$form['actions'] = [
'#access' => $user_has_edit_access,
'#type' => 'actions',
'#weight' => 100,
'save' => array(
'save' => [
'#button_type' => 'primary',
'#type' => 'submit',
'#value' => t('Save'),
),
'clear' => array(
],
'clear' => [
'#access' => $checklist->hasSavedProgress(),
'#button_type' => 'danger',
'#attributes' => array('class' => array('clear-saved-progress')),
'#submit' => array(array($this, 'clear')),
'#attributes' => ['class' => ['clear-saved-progress']],
'#submit' => [[$this, 'clear']],
'#type' => 'submit',
'#value' => t('Clear saved progress'),
),
);
],
];
// Alert the user of autochecked items. Only set the message on GET requests
// to prevent it from reappearing after saving the form. (Testing the
// request method may not be the "correct" way to accomplish this.)
if ($num_autochecked_items && $_SERVER['REQUEST_METHOD'] == 'GET') {
$args = array(
$args = [
'%checklist' => $checklist->title,
'@num' => $num_autochecked_items,
);
];
$message = \Drupal::translation()->formatPlural(
$num_autochecked_items,
t('%checklist found 1 unchecked item that was already completed and checked it for you. Save the form to record the change.', $args),
......
......@@ -23,30 +23,30 @@ class ChecklistapiRouteSubscriber extends RouteSubscriberBase {
* An array of route objects.
*/
public function routes() {
$routes = array();
$routes = [];
foreach (checklistapi_get_checklist_info() as $id => $definition) {
// Ignore incomplete definitions.
if (empty($definition['#path']) || empty($definition['#title'])) {
continue;
}
$requirements = array('_checklistapi_access' => 'TRUE');
$requirements = ['_checklistapi_access' => 'TRUE'];
// View/edit checklist.
$routes["checklistapi.checklists.{$id}"] = new Route($definition['#path'], array(
$routes["checklistapi.checklists.{$id}"] = new Route($definition['#path'], [
'_title' => $definition['#title'],
'_form' => '\Drupal\checklistapi\Form\ChecklistapiChecklistForm',
'checklist_id' => $id,
'op' => 'any',
), $requirements);
], $requirements);
// Clear saved progress.
$routes["checklistapi.checklists.{$id}.clear"] = new Route("{$definition['#path']}/clear", array(
$routes["checklistapi.checklists.{$id}.clear"] = new Route("{$definition['#path']}/clear", [
'_title' => 'Clear',
'_form' => '\Drupal\checklistapi\Form\ChecklistapiChecklistClearForm',
'checklist_id' => $id,
'op' => 'edit',
), $requirements);
], $requirements);
return $routes;
}
......
......@@ -23,12 +23,12 @@ class ChecklistapiTest extends WebTestBase {
/**
* {@inheritdoc}
*/
public static $modules = array(
public static $modules = [
'checklistapi',
'checklistapiexample',