accessibility.module

  1. cis7 sites/all/modules/ulmus/accessibility/accessibility.module
  2. cle7 sites/all/modules/ulmus/accessibility/accessibility.module
  3. ecd7 sites/all/modules/ulmus/accessibility/accessibility.module
  4. elmsmedia7 sites/all/modules/ulmus/accessibility/accessibility.module
  5. harmony7 sites/all/modules/ulmus/accessibility/accessibility.module
  6. icor7 sites/all/modules/ulmus/accessibility/accessibility.module
  7. meedjum_blog7 sites/all/modules/ulmus/accessibility/accessibility.module
  8. mooc7 sites/all/modules/ulmus/accessibility/accessibility.module

Functions

Namesort descending Description
accessibility_accessibility_get_test_translation Implements hook_accessibility_get_test_translation().
accessibility_accessibility_guidelines Implements hook_accessibility_guidelines().
accessibility_accessibility_tests Implements hook_accessibility_tests().
accessibility_entity_info Implements hook_entity_info().
accessibility_features_api Implements hook_features_api().
accessibility_field_extra_fields Implements hook_field_extra_fields().
accessibility_get_active_tests Helper function to return all active accessibiliy test entities.
accessibility_get_test_by_machine_name Returns the test ID for a given machine name.
accessibility_help Implements hook_help().
accessibility_load Helper function to load the QUAIL library.
accessibility_menu Implements hook_menu().
accessibility_permission Implements hook_permission().
accessibility_test_access Access callback for accessibility test entities.
accessibility_test_add New accessibility test form.
accessibility_test_add_form Form for saving accessibiliy test entities.
accessibility_test_add_form_submit Submit callback for saving accessibility test entities.
accessibility_test_add_form_validate Validate callback for saving accessibility test entities.
accessibility_test_delete Deletes an accessibility test.
accessibility_test_delete_form Form callback: confirmation form for deleting a model.
accessibility_test_delete_form_submit Submit callback for model_delete_form
accessibility_test_delete_multiple Delete multiple accessibility tests.
accessibility_test_edit_form Edit form for accessibility tests.
accessibility_test_edit_form_submit Form API submit callback for the model form.
accessibility_test_edit_form_validate
accessibility_test_form_submit_delete Form API submit callback for the delete button.
accessibility_test_load Loads a single accessibility test.
accessibility_test_load_multiple Loads multiple accessibility test entities.
accessibility_test_page_title Returns the title of the accessibility test entity page.
accessibility_test_page_view View callback for accessibility test entities.
accessibility_test_page_view_json JSON render callback for accessibility test entities.
accessibility_test_render
accessibility_test_save Saves an accessibility test entity.
accessibility_test_uri Callback to define the default URI for accessibility test entities.
accessibility_views_api Implements hook_views_api().

Constants

Classes

Namesort descending Description
AccessibilityTestController Accessibility test entity controller.

File

sites/all/modules/ulmus/accessibility/accessibility.module
View source
  1. <?php
  2. define('ACCESSIBILITY_TEST_SEVERE', 'severe');
  3. define('ACCESSIBILITY_TEST_MODERATE', 'moderate');
  4. define('ACCESSIBILITY_TEST_SUGGESTION', 'suggestion');
  5. define('ACCESSIBILITY_QUAIL_VERSION', '2.0.3');
  6. /**
  7. * Implements hook_help().
  8. */
  9. function accessibility_help($path, $arg) {
  10. switch($path) {
  11. case 'admin/config/accessibility':
  12. return '<p>' . t('The accessibility module enables tests for content, themes, and modules. You can <a target="_blank" href="https://drupal.org/node/2056945">read more defailed documentation on Drupal.org</a>.') .'</p>';
  13. case 'admin/config/accessibility/tests':
  14. return '<p>'. t('This is a list of your existing accessibility tests. To create new accessibiilty tests, <a href="!link">visit the Import Tests</a> page. <a href="https://drupal.org/node/2056945#tests" target="_blank">Learn more about accessibility tests</a>.', array('!link' => url('admin/config/accessibility/tests/import'))) .'</p>';
  15. case 'admin/config/accessibility/tests/import':
  16. return '<p>'. t('Accessibility tests check for a single error in your site. You can import tests by selecting them, or click a guideline below to enable all tests associated with that guideline.') .'</p>';
  17. case 'admin/help#accessibility':
  18. $output = '';
  19. $output .= '<h3>' . t('About') . '</h3>';
  20. $output .= '<p>' . t('The Accessibility module allows you to check content, themes, or modules against pre-defined accessibility tests. For example, enabling a test looking for images missing an "alt" attribute would help give content authors feedback on how to ensure people using assistive technologies (i.e. blind users using a screen reader) can use the site. You can add and edit tests under the <a href="!link">Accessibility administration page</a>. For more infromation, see the online handbook entry for the <a href="https://drupal.org/node/2056945">Accessibility module</a>.', array('!link' => url('admin/config/accessibility'))) .'</p>';
  21. $output .= '<h3>' . t('Uses') . '</h3>';
  22. $output .= '<dl>';
  23. $output .= '<dt>' . t('Giving feedback to content authors') . '</dt>';
  24. $output .= '<dd>' . t('To help content authors create accessible content, enable the Content Accessibility module. This will place a toggle on every page for users with the "Check content for accessibility" permission when viewing fields that have had testing enabled.') . '</dd>';
  25. $output .= '<dt>' . t('Checking content in WYSIWYG editors') . '</dt>';
  26. $output .= '<dd>' . t('Enabling the Accessibility WYSIWYG module, in combination with either the WYSIWYG or CKEditor modules, will place a checkbox in the toolbar of any TinyMCE or CKEditor (version 3 or 4) for users to check content against your enabled accessibility tests.') . '</dd>';
  27. $output .= '<dt>' . t('Report on accessibility errors') . '</dt>';
  28. $output .= '<dd>' . t('The Accessibility Reporting module checks content for accessibility errors in a user\'s browser and posts back to the site any errors. This data is stored in your drupal site, and is available as additional fields for the Views module. While this module installs a default view, you can clone that view to start buidlding your own accessibility reports.') . '</dd>';
  29. $output .= '<dt>' . t('Checking themes for accessibility') . '</dt>';
  30. $output .= '<dd>' . t('Enabling the Theme Accessibility module will give users with the "Access accessibility theming" permission a toggle to turn testing on for the entire page, not just parts of the page that are just content like the Content Accessibility module. This can lead to more false-positives, but helps users when building custom themes to ensure they are as accessible as possible. This module should not be enabled for production websites, as it alters the normal way a page is rendered.') . '</dd>';
  31. $output .= '</dl>';
  32. return $output;
  33. }
  34. }
  35. /**
  36. * Helper function to load the QUAIL library.
  37. */
  38. function accessibility_load() {
  39. $is_loaded = drupal_static(__FUNCTION__);
  40. if ($is_loaded) {
  41. return;
  42. }
  43. $is_loaded = TRUE;
  44. $library_path = libraries_get_path('quail');
  45. $module_path = drupal_get_path('module', 'accessibility');
  46. drupal_add_js($library_path . '/src/quail.js');
  47. drupal_add_js($module_path . '/js/accessibility.js');
  48. drupal_add_css($module_path . '/css/accessibility.css');
  49. drupal_add_js(array('accessibility' => array('icon_path' => drupal_get_path('module', 'accessibility') .'/css/img/', 'quail_path' => $library_path . '/src/resources')), 'setting');
  50. module_invoke_all('accessibility_load');
  51. }
  52. /**
  53. * Implements hook_entity_info().
  54. */
  55. function accessibility_entity_info() {
  56. $entities['accessibility_test'] = array(
  57. 'label' => t('Accessibility Test'),
  58. 'controller class' => 'AccessibilityTestController',
  59. 'base table' => 'accessibility_test',
  60. 'uri callback' => 'accessibility_test_uri',
  61. 'fieldable' => TRUE,
  62. 'entity keys' => array(
  63. 'id' => 'test_id',
  64. ),
  65. 'static cache' => TRUE,
  66. 'bundles' => array(
  67. 'accessibility_test' => array(
  68. 'label' => 'accessibility_test',
  69. 'admin' => array(
  70. 'path' => 'admin/config/accessibility/tests',
  71. 'access arguments' => array('administer accessibility tests'),
  72. ),
  73. ),
  74. ),
  75. 'view modes' => array(
  76. 'full' => array(
  77. 'label' => t('Full test'),
  78. 'custom settings' => FALSE,
  79. ),
  80. 'popup' => array(
  81. 'label' => t('Test message'),
  82. 'custom settings' => FALSE,
  83. ),
  84. )
  85. );
  86. return $entities;
  87. }
  88. /**
  89. * Accessibility test entity controller.
  90. */
  91. class AccessibilityTestController extends EntityAPIController {
  92. public function save($test) {
  93. if ((property_exists($test, 'is_new') && $test->is_new) || !isset($test->test_id)) {
  94. $test->changed = $test->created = time();
  95. }
  96. else {
  97. $test->changed = time();
  98. }
  99. parent::save($test);
  100. if (property_exists($test, 'is_new') && $test->is_new) {
  101. drupal_write_record('accessibility_test', $test);
  102. }
  103. else {
  104. db_update('accessibility_test')
  105. ->fields(array('name' => $test->name,
  106. 'severity' => $test->severity,
  107. 'created' => $test->created,
  108. 'changed' => $test->changed,
  109. 'quail_name' => $test->quail_name,
  110. 'data' => serialize($test->data)))
  111. ->condition('test_id', $test->test_id)
  112. ->execute();
  113. }
  114. cache_clear_all('accessibility_tests_json', 'cache');
  115. }
  116. public function create(array $values = array()) {
  117. // Add values that are specific to our Model
  118. $values += array(
  119. 'test_id' => '',
  120. 'is_new' => TRUE,
  121. 'name' => '',
  122. 'created' => time(),
  123. 'changed' => time(),
  124. 'quail_name' => '',
  125. 'severity' => '',
  126. 'status' => 1,
  127. 'data' => array()
  128. );
  129. $test = parent::create($values);
  130. return $test;
  131. }
  132. };
  133. /**
  134. * Callback to define the default URI for accessibility test entities.
  135. */
  136. function accessibility_test_uri($test) {
  137. return array(
  138. 'path' => 'accessibility-test/' . $test->test_id,
  139. );
  140. }
  141. /**
  142. * Loads a single accessibility test.
  143. */
  144. function accessibility_test_load($test_id = NULL, $reset = FALSE) {
  145. $ids = (isset($test_id) ? array($test_id) : array());
  146. $test = accessibility_test_load_multiple($ids, $reset);
  147. return $test ? reset($test) : FALSE;
  148. }
  149. /**
  150. * Access callback for accessibility test entities.
  151. */
  152. function accessibility_test_access($op, $test) {
  153. if ($op == 'edit' || $op == 'delete') {
  154. return user_access('administer accessibility tests');
  155. }
  156. return user_access('view accessibility tests');
  157. }
  158. /**
  159. * Loads multiple accessibility test entities.
  160. */
  161. function accessibility_test_load_multiple($ids = array(), $conditions = array(), $reset = FALSE) {
  162. $tests = entity_load('accessibility_test', $ids, $conditions, $reset);
  163. if ($tests && is_array($tests)) {
  164. foreach ($tests as &$test) {
  165. $test->data = ($test->data) ? unserialize($test->data) : array();
  166. }
  167. }
  168. return $tests;
  169. }
  170. /**
  171. * Implements hook_menu().
  172. */
  173. function accessibility_menu() {
  174. $items['admin/config/accessibility'] = array(
  175. 'title' => 'Accessibility',
  176. 'description' => 'Configures how page content and themes are checked for accessibility.',
  177. 'position' => 'right',
  178. 'weight' => -20,
  179. 'page callback' => 'system_admin_menu_block_page',
  180. 'access arguments' => array('access administration pages'),
  181. 'file' => 'system.admin.inc',
  182. 'file path' => drupal_get_path('module', 'system'),
  183. );
  184. $items['admin/config/accessibility/tests'] = array(
  185. 'title' => 'Accessibility tests',
  186. 'description' => 'View existing and create new accessibility tests.',
  187. 'page callback' => 'accessibility_admin_existing_tests',
  188. 'file' => 'accessibility.admin.inc',
  189. 'access arguments' => array('administer accessibility tests')
  190. );
  191. $items['admin/config/accessibility/tests/tests'] = array(
  192. 'title' => 'Tests',
  193. 'page callback' => 'accessibility_admin_existing_tests',
  194. 'file' => 'accessibility.admin.inc',
  195. 'access arguments' => array('administer accessibility tests'),
  196. 'weight' => -20,
  197. 'type' => MENU_DEFAULT_LOCAL_TASK
  198. );
  199. $items['admin/config/accessibility/tests/import'] = array(
  200. 'title' => 'Import tests',
  201. 'description' => 'Import and create new accessibility tests.',
  202. 'page callback' => 'drupal_get_form',
  203. 'page arguments' => array('accessibility_tests_list'),
  204. 'file' => 'accessibility.admin.inc',
  205. 'access arguments' => array('administer accessibility tests'),
  206. 'type' => MENU_LOCAL_TASK
  207. );
  208. $items['accessibility-test/%accessibility_test/json'] = array(
  209. 'title callback' => 'accessibility_test_page_title',
  210. 'title arguments' => array(1),
  211. 'page callback' => 'accessibility_test_page_view_json',
  212. 'page arguments' => array(1),
  213. 'access arguments' => array('view accessibility tests'),
  214. 'type' => MENU_CALLBACK
  215. );
  216. $items['accessibility-test/%accessibility_test'] = array(
  217. 'title callback' => 'accessibility_test_page_title',
  218. 'title arguments' => array(1),
  219. 'page callback' => 'accessibility_test_page_view',
  220. 'page arguments' => array(1),
  221. 'access arguments' => array('view accessibility tests'),
  222. );
  223. $items['accessibility-test/%accessibility_test/view'] = array(
  224. 'title' => 'View',
  225. 'page callback' => 'accessibility_test_page_view',
  226. 'page arguments' => array(1),
  227. 'access arguments' => array('view accessibility tests'),
  228. 'type' => MENU_DEFAULT_LOCAL_TASK,
  229. 'weight' => -10
  230. );
  231. $items['accessibility-test/%accessibility_test/edit'] = array(
  232. 'title' => 'Edit',
  233. 'page callback' => 'drupal_get_form',
  234. 'page arguments' => array('accessibility_test_edit_form', 1),
  235. 'access arguments' => array('administer accessibility tests'),
  236. 'type' => MENU_LOCAL_TASK,
  237. );
  238. $items['accessibility-test/%accessibility_test/delete'] = array(
  239. 'page callback' => 'drupal_get_form',
  240. 'page arguments' => array('accessibility_test_delete_form', 1),
  241. 'access arguments' => array('administer accessibility tests'),
  242. );
  243. $items['accessibility-test/add'] = array(
  244. 'title' => 'Add accessibility test',
  245. 'page callback' => 'accessibility_test_add',
  246. 'access arguments' => array('administer accessibility tests'),
  247. );
  248. $items['js/accessibility/tests.json'] = array(
  249. 'title' => 'Accessibility tests',
  250. 'page callback' => 'accessibility_tests_json',
  251. 'access arguments' => array('view accessibility tests'),
  252. 'file' => 'accessibility.pages.inc',
  253. 'type' => MENU_CALLBACK
  254. );
  255. return $items;
  256. }
  257. /**
  258. * New accessibility test form.
  259. */
  260. function accessibility_test_add() {
  261. $test = (object) array(
  262. 'test_id' => '',
  263. 'quail_name' => '',
  264. );
  265. return drupal_get_form('accessibility_test_add_form', $test);
  266. }
  267. /**
  268. * Edit form for accessibility tests.
  269. */
  270. function accessibility_test_edit_form($form, &$form_state, $test) {
  271. // Add the default field elements.
  272. $form['name'] = array(
  273. '#type' => 'textfield',
  274. '#title' => t('Test name'),
  275. '#default_value' => isset($test->name) ? $test->name : '',
  276. '#maxlength' => 255,
  277. '#required' => TRUE,
  278. '#weight' => -5,
  279. );
  280. $form['quail_name'] = array(
  281. '#type' => 'textfield',
  282. '#title' => t('Machine name'),
  283. '#default_value' => isset($test->quail_name) ? $test->quail_name : '',
  284. '#maxlength' => 255,
  285. '#required' => TRUE,
  286. '#weight' => -5,
  287. );
  288. $form['severity'] = array(
  289. '#type' => 'select',
  290. '#title' => t('Severity'),
  291. '#required' => TRUE,
  292. '#default_value' => isset($test->severity) ? $test->severity : '',
  293. '#options' => array(
  294. ACCESSIBILITY_TEST_SEVERE => t('Severe'),
  295. ACCESSIBILITY_TEST_MODERATE => t('Moderate'),
  296. ACCESSIBILITY_TEST_SUGGESTION => t('Suggestion'),
  297. ),
  298. );
  299. $form['status'] = array(
  300. '#type' => 'checkbox',
  301. '#title' => t('Test is active'),
  302. '#default_value' => isset($test->status) ? $test->status : 0,
  303. );
  304. $form_state['accessibility_test'] = $test;
  305. field_attach_form('accessibility_test', $test, $form, $form_state);
  306. $form['actions'] = array(
  307. '#type' => 'container',
  308. '#attributes' => array('class' => array('form-actions')),
  309. '#weight' => 400,
  310. );
  311. // We add the form's #submit array to this button along with the actual submit
  312. // handler to preserve any submit handlers added by a form callback_wrapper.
  313. $submit = array();
  314. if (!empty($form['#submit'])) {
  315. $submit += $form['#submit'];
  316. }
  317. $form['actions']['submit'] = array(
  318. '#type' => 'submit',
  319. '#value' => t('Save test'),
  320. '#submit' => $submit + array('accessibility_test_edit_form_submit'),
  321. );
  322. if (!empty($test->name)) {
  323. $form['actions']['delete'] = array(
  324. '#type' => 'submit',
  325. '#value' => t('Delete test'),
  326. '#suffix' => l(t('Cancel'), 'admin/config/accessibility/tests/existing'),
  327. '#submit' => $submit + array('accessibility_test_form_submit_delete'),
  328. '#weight' => 45,
  329. );
  330. }
  331. // We append the validate handler to #validate in case a form callback_wrapper
  332. // is used to add validate handlers earlier.
  333. $form['#validate'][] = 'accessibility_test_edit_form_validate';
  334. return $form;
  335. }
  336. function accessibility_test_edit_form_validate(&$form, &$form_state) {
  337. $test = $form_state['accessibility_test'];
  338. // Notify field widgets to validate their data.
  339. field_attach_form_validate('accessibility_test', $test, $form, $form_state);
  340. }
  341. /**
  342. * Form API submit callback for the model form.
  343. *
  344. * @todo remove hard-coded link
  345. */
  346. function accessibility_test_edit_form_submit(&$form, &$form_state) {
  347. $test = $form_state['accessibility_test'];
  348. $test->changed = time();
  349. field_attach_submit('accessibility_test', $test, $form, $form_state);
  350. $test->name = $form_state['values']['name'];
  351. $test->quail_name = $form_state['values']['quail_name'];
  352. $test->severity = $form_state['values']['severity'];
  353. $test->status = $form_state['values']['status'];
  354. accessibility_test_save($test);
  355. $form_state['redirect'] = 'accessibility-test/' . $test->test_id;
  356. }
  357. /**
  358. * Form API submit callback for the delete button.
  359. *
  360. * @todo Remove hard-coded path
  361. */
  362. function accessibility_test_form_submit_delete(&$form, &$form_state) {
  363. $form_state['redirect'] = 'accessibility-test/' . $form_state['accessibility_test']->test_id . '/delete';
  364. }
  365. /**
  366. * Form callback: confirmation form for deleting a model.
  367. *
  368. * @param $test
  369. * The model to delete
  370. *
  371. * @see confirm_form()
  372. */
  373. function accessibility_test_delete_form($form, &$form_state, $test) {
  374. $form_state['accessibility_test'] = $test;
  375. $form['#submit'][] = 'accessibility_test_delete_form_submit';
  376. $form = confirm_form($form,
  377. t('Are you sure you want to delete test %name?', array('%name' => $test->name)),
  378. 'accessibility-test/' . $test->test_id,
  379. '<p>' . t('This action cannot be undone.') . '</p>',
  380. t('Delete'),
  381. t('Cancel'),
  382. 'confirm'
  383. );
  384. return $form;
  385. }
  386. /**
  387. * Submit callback for model_delete_form
  388. */
  389. function accessibility_test_delete_form_submit($form, &$form_state) {
  390. $test = $form_state['accessibility_test'];
  391. $test = accessibility_test_load($test->test_id);
  392. accessibility_test_delete($test);
  393. drupal_set_message(t('The test %name has been deleted.', array('%name' => $test->name)));
  394. watchdog('accessibility_test', 'Deleted test %name.', array('%name' => $test->name));
  395. $form_state['redirect'] = 'admin/config/accessibility/tests/existing';
  396. }
  397. /**
  398. * Form for saving accessibiliy test entities.
  399. */
  400. function accessibility_test_add_form($form, &$form_state, $test) {
  401. $form['name'] = array(
  402. '#type' => 'textfield',
  403. '#title' => t('Test Name'),
  404. '#required' => TRUE,
  405. );
  406. $form['quail_name'] = array(
  407. '#type' => 'textfield',
  408. '#title' => t('QUAIL Name'),
  409. '#required' => TRUE,
  410. );
  411. $form['severity'] = array(
  412. '#type' => 'select',
  413. '#title' => t('Severity'),
  414. '#required' => TRUE,
  415. '#options' => array(
  416. ACCESSIBILITY_TEST_SEVERE => t('Severe'),
  417. ACCESSIBILITY_TEST_MODERATE => t('Moderate'),
  418. ACCESSIBILITY_TEST_SUGGESTION => t('Suggestion'),
  419. ),
  420. );
  421. $form['status'] = array(
  422. '#type' => 'checkbox',
  423. '#title' => t('Test is active'),
  424. );
  425. $form['submit'] = array(
  426. '#type' => 'submit',
  427. '#value' => t('Save'),
  428. );
  429. $test = new stdClass();
  430. $test->type = 'accessibility_test';
  431. field_attach_form('accessibility_test', $test, $form, $form_state);
  432. return $form;
  433. }
  434. /**
  435. * Validate callback for saving accessibility test entities.
  436. */
  437. function accessibility_test_add_form_validate($form, &$form_state) {
  438. $test_submission = (object) $form_state['values'];
  439. $test_submission->type = 'accessibility_test';
  440. field_attach_form_validate('accessibility_test', $test_submission, $form, $form_state);
  441. }
  442. /**
  443. * Submit callback for saving accessibility test entities.
  444. */
  445. function accessibility_test_add_form_submit($form, &$form_state) {
  446. $test_submission = (object) $form_state['values'];
  447. $test_submission->type = 'accessibility_test';
  448. field_attach_submit('accessibility_test', $test_submission, $form, $form_state);
  449. $test = accessibility_test_save($test_submission);
  450. $form_state['redirect'] = "accessibility-test/$test->test_id";
  451. }
  452. /**
  453. * Saves an accessibility test entity.
  454. */
  455. function accessibility_test_save(&$test) {
  456. return entity_get_controller('accessibility_test')->save($test);
  457. }
  458. /**
  459. * Implements hook_permission().
  460. */
  461. function accessibility_permission() {
  462. return array(
  463. 'administer accessibility tests' => array(
  464. 'title' => t('Administer accessibility tests'),
  465. 'restrict access' => TRUE,
  466. ),
  467. 'view accessibility tests' => array(
  468. 'title' => t('View accessibility tests'),
  469. )
  470. );
  471. }
  472. /**
  473. * Deletes an accessibility test.
  474. */
  475. function accessibility_test_delete($test) {
  476. accessibility_test_delete_multiple(array($test->test_id));
  477. }
  478. /**
  479. * Delete multiple accessibility tests.
  480. */
  481. function accessibility_test_delete_multiple(array $test_ids) {
  482. $tests = accessibility_test_load_multiple($test_ids);
  483. foreach ($tests as $test) {
  484. module_invoke_all('accessibility_test_delete', $test);
  485. field_attach_delete('accessibility_test', $test);
  486. db_delete('accessibility_test')
  487. ->condition('test_id', $test->test_id)
  488. ->execute();
  489. }
  490. }
  491. /**
  492. * Returns the title of the accessibility test entity page.
  493. */
  494. function accessibility_test_page_title($test) {
  495. return $test->name;
  496. }
  497. /**
  498. * View callback for accessibility test entities.
  499. */
  500. function accessibility_test_page_view($test, $view_mode = 'default') {
  501. accessibility_test_render($test, $view_mode);
  502. return $test->content;
  503. }
  504. /**
  505. * JSON render callback for accessibility test entities.
  506. */
  507. function accessibility_test_page_view_json($test, $view_mode = 'popup') {
  508. accessibility_test_render($test, $view_mode);
  509. $test->content['#prefix'] = '<div class="accessibility-test">';
  510. $test->content['#suffix'] = '</div>';
  511. $json = array('content' => drupal_render($test->content),
  512. 'title' => $test->name
  513. );
  514. drupal_json_output($json);
  515. }
  516. function accessibility_test_render($test, $view_mode = 'default') {
  517. $test->content = array();
  518. field_attach_prepare_view('accessibility_test', array($test->test_id => $test), $view_mode);
  519. entity_prepare_view('accessibility_test', array($test->test_id => $test));
  520. $test->content += field_attach_view('accessibility_test', $test, $view_mode);
  521. $extra_fields = field_info_extra_fields('accessibility_test', 'accessibility_test', 'display');
  522. if($extra_fields['quail_name']['display'][$view_mode]['visible']) {
  523. $test->content['quail_name'] = array(
  524. '#type' => 'item',
  525. '#title' => $extra_fields['quail_name']['label'],
  526. '#markup' => check_plain($test->quail_name),
  527. '#weight' => $extra_fields['quail_name']['display'][$view_mode],
  528. );
  529. }
  530. if($extra_fields['severity']['display'][$view_mode]['visible']) {
  531. $test->content['severity'] = array(
  532. '#type' => 'item',
  533. '#title' => $extra_fields['severity']['label'],
  534. '#markup' => $test->severity,
  535. '#weight' => $extra_fields['quail_name']['display'][$view_mode],
  536. );
  537. }
  538. if($extra_fields['status']['display'][$view_mode]['visible']) {
  539. $test->content['status'] = array(
  540. '#type' => 'item',
  541. '#title' => $extra_fields['status']['label'],
  542. '#markup' => ($test->status) ? t('Active') : t('Inactive'),
  543. '#weight' => $extra_fields['quail_name']['display'][$view_mode],
  544. );
  545. }
  546. module_invoke_all('entity_view', $test, 'accessibility_test', $view_mode);
  547. }
  548. /**
  549. * Implements hook_field_extra_fields().
  550. */
  551. function accessibility_field_extra_fields() {
  552. $return = array();
  553. $extras = array(
  554. 'quail_name' => array(
  555. 'label' => t('Machine Name'),
  556. 'weight' => 0,
  557. 'display' => array(
  558. 'popup' => array(
  559. 'visible' => FALSE,
  560. ),
  561. ),
  562. ),
  563. 'severity' => array(
  564. 'label' => t('Severity level'),
  565. 'weight' => 0,
  566. 'display' => array(
  567. 'popup' => array(
  568. 'visible' => FALSE,
  569. ),
  570. ),
  571. ),
  572. 'status' => array(
  573. 'label' => t('Status'),
  574. 'weight' => 0,
  575. 'display' => array(
  576. 'popup' => array(
  577. 'visible' => FALSE,
  578. ),
  579. ),
  580. )
  581. );
  582. $return['accessibility_test']['accessibility_test'] = array(
  583. 'form' => $extras,
  584. 'display' => $extras,
  585. );
  586. return $return;
  587. }
  588. /**
  589. * Implements hook_accessibility_tests().
  590. */
  591. function accessibility_accessibility_tests($get_translation = FALSE) {
  592. $suffix = ($get_translation) ? 'translated' : '';
  593. if ($cache = cache_get('accessibility_quail_tests_' . $suffix)) {
  594. return $cache->data;
  595. }
  596. $library_path = libraries_get_path('quail');
  597. $tests = (array)json_decode(file_get_contents($library_path . '/src/resources/tests.json'));
  598. $test_translation = array('0' => ACCESSIBILITY_TEST_SUGGESTION,
  599. '.5' => ACCESSIBILITY_TEST_MODERATE,
  600. '1' => ACCESSIBILITY_TEST_SEVERE);
  601. foreach ($tests as &$test) {
  602. $test = (array)$test;
  603. if (isset($test['testability'])) {
  604. $test['severity'] = $test_translation[$test['testability']];
  605. }
  606. }
  607. if ($get_translation) {
  608. foreach ($tests as $testname => &$test) {
  609. $translation = module_invoke_all('accessibility_get_test_translation', $testname);
  610. $test['title'] = $translation['title'];
  611. $test['description'] = $translation['body'];
  612. }
  613. }
  614. cache_set('accessibility_quail_tests_' . $suffix, $tests);
  615. return $tests;
  616. }
  617. /**
  618. * Implements hook_accessibility_get_test_translation().
  619. */
  620. function accessibility_accessibility_get_test_translation($test) {
  621. $strings = &drupal_static('_locale_import_one_string:strings', array());
  622. $language = language_default();
  623. if (!count($strings)) {
  624. require_once('includes/locale.inc');
  625. $file = new stdClass();
  626. $file->uri = drupal_get_path('module', 'accessibility') . '/po/' . $language->language . '.po';
  627. _locale_import_read_po('mem-store', $file);
  628. }
  629. return isset($strings[$test][$test]) ? $strings[$test][$test] : array('title' => $test, 'body' => '');
  630. }
  631. /**
  632. * Implements hook_accessibility_guidelines().
  633. */
  634. function accessibility_accessibility_guidelines($tests = FALSE) {
  635. $guidelines = array('508' => array('title' => 'Section 508'),
  636. 'wcag1a' => array('title' => 'WCAG 1.0 A'),
  637. 'wcag1aa' => array('title' => 'WCAG 1.0 AA'),
  638. 'wcag1aaa' => array('title' => 'WCAG 1.0 AAA'),
  639. 'wcag2a' => array('title' => 'WCAG 2.0 A'),
  640. 'wcag2aa' => array('title' => 'WCAG 2.0 AA'),
  641. 'wcag2aaa' => array('title' => 'WCAG 2.0 AAA'),
  642. );
  643. if(!$tests) {
  644. return $guidelines;
  645. }
  646. if($cache = cache_get('accessibility_guidelines')) {
  647. return $cache->data;
  648. }
  649. $library_path = libraries_get_path('quail');
  650. foreach($guidelines as $key => &$guideline) {
  651. $guideline['tests'] = json_decode(file_get_contents($library_path .'/src/resources/guidelines/'. $key .'.json'));
  652. }
  653. cache_set('accessibility_guidelines', $guidelines);
  654. return $guidelines;
  655. }
  656. /**
  657. * Implements hook_views_api().
  658. */
  659. function accessibility_views_api() {
  660. return array(
  661. 'api' => 3,
  662. 'path' => drupal_get_path('module', 'accessibility') . '/views',
  663. );
  664. }
  665. /**
  666. * Implements hook_features_api().
  667. */
  668. function accessibility_features_api() {
  669. return array(
  670. 'accessibility_test' => array(
  671. 'default_hook' => 'accessibility_test_default',
  672. 'default_file' => FEATURES_DEFAULTS_CUSTOM,
  673. 'feature_source' => TRUE,
  674. 'default_filename' => 'accessibility_test',
  675. 'file' => drupal_get_path('module', 'accessibility') . '/accessibility.features.inc',
  676. ),
  677. );
  678. }
  679. /**
  680. * Helper function to return all active accessibiliy test entities.
  681. */
  682. function accessibility_get_active_tests($load = TRUE) {
  683. $tests = db_select('accessibility_test', 't')
  684. ->fields('t', array('test_id'))
  685. ->condition('status', 1)
  686. ->execute()
  687. ->fetchAllKeyed(0, 0);
  688. if ($load) {
  689. foreach ($tests as &$test) {
  690. $test = accessibility_test_load($test);
  691. }
  692. }
  693. return $tests;
  694. }
  695. /**
  696. * Returns the test ID for a given machine name.
  697. */
  698. function accessibility_get_test_by_machine_name($name) {
  699. $id = db_select('accessibility_test', 'a')
  700. ->fields('a', array('test_id'))
  701. ->condition('quail_name', $name)
  702. ->execute()
  703. ->fetchField();
  704. return (int)$id;
  705. }