You are here

node_accessibility.module in Node Accessibility 8

Same filename and directory in other branches
  1. 7 node_accessibility.module

Module file for the node accessibility project.

File

node_accessibility.module
View source
<?php

/**
 * @file
 * Module file for the node accessibility project.
 */

/**
 * Implements hook_form_FORM_ID_alter() for the node type form.
 */
function node_accessibility_form_node_type_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $standards = \Drupal\quail_api\QuailApiSettings::get_standards_list();
  $severitys = \Drupal\quail_api\QuailApiSettings::get_severity_list();
  $methods = \Drupal\quail_api\QuailApiSettings::get_validation_methods();
  $methods_list = \Drupal\quail_api\QuailApiSettings::get_validation_methods_list();
  $filter_formats = filter_formats();
  $formats = [];
  $title_blocks = [];
  $options = [];
  $formats[NULL] = '<Quail API Default>';
  foreach ($filter_formats as $format_name => $format_interface) {
    $formats[$format_name] = $format_interface
      ->label();
  }
  unset($filter_formats);
  $title_blocks = [
    NULL => '<Quail API Default>',
    'h1' => 'Heading 1',
    'h2' => 'Heading 2',
    'h3' => 'Heading 3',
    'h4' => 'Heading 4',
    'h5' => 'Heading 5',
    'h6' => 'Heading 6',
    'div' => 'Divider',
    'span' => 'Spanner',
    'p' => 'Paragraph',
  ];
  $entity = $form_state
    ->getFormObject()
    ->getEntity();
  $node_type = $entity
    ->id();
  unset($entity);

  // node type is required, so if this entity does not have an id, then do nothing.
  if (empty($node_type)) {
    return;
  }
  $type_settings = \Drupal\node_accessibility\TypeSettingsStorage::load($node_type);
  $form['node_accessibility_validation'] = [
    '#type' => 'details',
    '#title' => t("Accessibility Validation"),
    '#description' => t("Provides options for enabled and disabled accessibility validation on text stored in this field."),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#group' => 'additional_settings',
  ];
  $form['node_accessibility_validation']['enabled'] = [
    '#type' => 'select',
    '#title' => t("Accessibility Validation"),
    '#default_value' => $type_settings
      ->getEnabled(),
    '#options' => [
      'disabled' => t("Disabled"),
      'optional' => t("Enabled (optional)"),
      'required' => t("Enabled (required)"),
    ],
    '#description' => t("Choose if accessibility validation should be enabled for this node type and if the node type is required to pass accessibility validation."),
  ];
  $form['node_accessibility_validation']['method'] = [
    '#type' => 'select',
    '#title' => t("Validation Method"),
    '#default_value' => $type_settings
      ->getMethod(),
    '#options' => $methods_list,
    '#description' => t("Choose the way in which validation is performed."),
    '#dependency' => [
      'edit-node-accessibility-validation-quail-enabled' => [
        'optional',
        'required',
      ],
    ],
  ];

  // add documentation for each option
  $form['node_accessibility_validation']['method']['#description'] .= '<ul>';
  foreach ($methods as $method => $method_settings) {
    $form['node_accessibility_validation']['method']['#description'] .= '<li>' . $method_settings['human_name'] . ': ' . $method_settings['description'] . '</li>';
  }
  $form['node_accessibility_validation']['method']['#description'] .= '</ul>';
  $form['node_accessibility_validation']['format_results'] = [
    '#type' => 'select',
    '#title' => t("Markup Format"),
    '#default_value' => $type_settings
      ->getFormatResults(),
    '#options' => $formats,
    '#description' => t("Choose the filter to use when presenting the results of validation checks. This is only used for the html markup associated with the problem or suggestion."),
    '#dependency' => [
      'edit-node-accessibility-validation-quail-enabled' => [
        'optional',
        'required',
      ],
    ],
  ];
  $form['node_accessibility_validation']['title_block'] = [
    '#type' => 'select',
    '#title' => t("Title Block"),
    '#default_value' => $type_settings
      ->getTitleBlock(),
    '#options' => $title_blocks,
    '#description' => t("Choose the title block to use when renderring the validation result titles."),
    '#dependency' => [
      'edit-node-accessibility-validation-quail-enabled' => [
        'optional',
        'required',
      ],
    ],
  ];
  $form['node_accessibility_validation']['standards'] = [
    '#type' => 'checkboxes',
    '#title' => t("Accessibility Standards"),
    '#default_value' => $type_settings
      ->getStandards(),
    '#options' => $standards,
    '#description' => t("Choose which accessibility standards to validate against. It is strongly suggested that only 1 standard should be used because many of the validation tests are performed by more than one standard."),
    '#dependency' => [
      'edit-node-accessibility-validation-quail-enabled' => [
        'optional',
        'required',
      ],
    ],
  ];
  $form['actions']['submit']['#submit'][] = 'node_accessibility_node_type_form_submit';
}

/**
 * Handles submitting the accessible content specific node type settings.
 *
 * @param array $form
 *   A form array
 * @param array $form_state
 *   A form state
 */
function node_accessibility_node_type_form_submit(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $state_values = $form_state
    ->getValues();
  if (isset($state_values['node_accessibility_validation'])) {
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    $state_values['node_accessibility_validation']['node_type'] = $entity
      ->id();
    $existing = \Drupal\node_accessibility\TypeSettingsStorage::merge($state_values['node_accessibility_validation']);
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function node_accessibility_node_insert(\Drupal\node\Entity\Node $node) {
  $node_type = $node
    ->getType();
  $type_settings = \Drupal\node_accessibility\TypeSettingsStorage::loadAsArray($node_type);
  $quail_methods = \Drupal\quail_api\QuailApiSettings::get_validation_methods();
  if (!empty($type_settings['method']) && isset($quail_methods[$type_settings['method']])) {
    $method = $quail_methods[$type_settings['method']];

    // only perform automated validation if both the database is enabled and automatic is enabled.
    if (isset($method['automatic']) && $method['automatic'] && isset($method['database']) && $method['database']) {
      \Drupal\node_accessibility\PerformValidation::nodes([
        $node,
      ]);
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function node_accessibility_node_update(\Drupal\node\Entity\Node $node) {
  $node_type = $node
    ->getType();
  $type_settings = \Drupal\node_accessibility\TypeSettingsStorage::loadAsArray($node_type);
  $quail_methods = \Drupal\quail_api\QuailApiSettings::get_validation_methods();
  if (!empty($type_settings['method']) && isset($quail_methods[$type_settings['method']])) {
    $method = $quail_methods[$type_settings['method']];

    // only perform automated validation if both the database is enabled and automatic is enabled.
    if (isset($method['automatic']) && $method['automatic'] && isset($method['database']) && $method['database']) {
      \Drupal\node_accessibility\PerformValidation::nodes([
        $node,
      ]);
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function node_accessibility_node_delete(\Drupal\node\Entity\Node $node) {
  \Drupal\node_accessibility\ProblemsStorage::delete_node_problems($node->nid->value, $node->vid->value);
}

Functions