editoria11y-1.0.0-alpha8/modules/editoria11y_csa/src/Form/Editoria11yAuditorSettings.php

modules/editoria11y_csa/src/Form/Editoria11yAuditorSettings.php
<?php

namespace Drupal\editoria11y_csa\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\editoria11y\TestNames;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;

/**
 * Class to define all settings of the module.
 */
class Editoria11yAuditorSettings extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'editoria11y_csa_form_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames(): array {
    return [
      'editoria11y_csa.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $config = $this->config('editoria11y.settings');
    $csaConfig = $this->config('editoria11y_csa.settings');

    $form['getting_started'] = [
      '#markup' => '<h2>' .
      $this->t('Editoria11y CSF') . '</h2>' .
        '<p>' . $this->t("This is generally used to run additional tests for developers, designers and testers, but you can set all roles to use this configuration if you want all users to see the additional checks.") . '</p>' .
      '<blockquote>NOTE FOR ALPHA TESTERS THIS CONFIGURATION IS NOT CONNECTED YET. SETTING VALUES ON THIS PAGE WILL HAVE NO EFFECT.</blockquote>',
    ];

    /*
    $form['setup'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Content Author Configuration'),
      '#markup' => '<p>' . $this->t("Make sure") . ' ' .
      $linkToPermissions . ' ' .
      $this->t('have the "View Editoria11y checker" permission. Assign "mark OK" as appropriate.') . ' ' .
      $linkToDashboard . $this->t(', users need "Manage Editoria11y results."') . '</p>',
    ];

    $form['setup']['assertiveness'] = [
      '#title' => $this->t("Show tips automatically when issues are detected"),
      '#type' => 'radios',
      '#options' => [
        'assertive' => $this->t('Always'),
        'smart' => $this->t('On first detection'),
        'polite' => $this->t('Never'),
      ],
      '#default_value' => $config->get('assertiveness'),
    ];

    $form['setup']['content_root'] = [
      '#title' => $this->t("Check content in these containers"),
      '#type' => 'textarea',
      '#rows' => 1,
      '#description' => $this->t('To limit checks to user-editable containers, provide a list of <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors">CSS selectors</a>.<br>E.g.: <code><em>main, #footer-editable-content</em></code><br>Default: <code>main</code>, falling back to <code>body</code>. If elements are specified here and none are found on the page, Editoria11y will not find anything!'),
      '#default_value' => $config->get('content_root'),
    ];

    $form['setup']['ignore_elements'] = [
      '#title' => $this->t("Skip over these elements"),
      '#type' => 'textarea',
      '#rows' => 1,
      '#description' => $this->t('Provide a comma-separated list of selectors for elements to ignore. These should target specific elements (use an asterisk to indicate "all children of this element"). <br>E.g.: <code><em>#sidebar-menu a, .card img, .slide [aria-hidden="true"], .feed *</em></code>.'),
      '#default_value' => $config->get('ignore_elements'),
    ];

    $form['setup']['tests'] = [
      '#type' => 'details',
      '#title' => $this->t('Modify tests'),
    ];

    $form['setup']['tests']['ignore_tests'] = [
      '#title' => $this->t("Turn off specific tests"),
      '#type' => 'checkboxes',
      '#options' => (new TestNames())->coreNames(),
      '#default_value' => $config->get('ignore_tests') ?? [],
    ];
    */

    //$roles = Role::loadMultiple();
    //unset($roles[RoleInterface::ANONYMOUS_ID]);
    //$roles = array_filter($roles, fn(RoleInterface $role) => $role->hasPermission('module_test perm'));
    //...    $names =  array_map(fn(RoleInterface $role) => $role->label(), array_filter(Role::loadMultiple()));
    $permission = 'view editoria11y checker';
    $roles = array_filter(Role::loadMultiple(), function (RoleInterface $role) use ($permission) {
      return $role->hasPermission($permission);
    });
    $names =  array_map(fn(RoleInterface $role) => $role->label(), $roles);

    $form['developer_roles'] = [
      '#title' => $this->t('Roles that will use the developer configuration'),
      '#type' => 'checkboxes',
      '#options' => $names,
      '#default_value' => $config->get('developer_roles') ?? [],
    ];

    $form['developer_assertiveness'] = [
      '#title' => $this->t("Show tips for these roles automatically when issues are detected"),
      '#type' => 'radios',
      '#options' => [
        'assertive' => $this->t('Always'),
        'smart' => $this->t('On first detection'),
        'polite' => $this->t('Never'),
      ],
      '#default_value' => $config->get('developer_assertiveness'),
    ];

    $form['developer_content_root'] = [
      '#title' => $this->t("Check in these containers"),
      '#type' => 'textarea',
      '#rows' => 1,
      '#description' => $this->t('Make sure this includes the content areas from the previous tab, as the tests shown to end-users will be filtered from what is found here. E.g., show "main" to content editors and "header, main, footer" to developers.<br>Content editors are viewing results matching: ') . '<code><em>'.  $config->get('content_root') . '</em></code>', // @todo handle empty config.
      '#default_value' => $config->get('developer_content_root'),
    ];

    $form['developer_template_regions'] = [
      '#title' => $this->t("Define template regions shown on every page"),
      '#type' => 'textarea',
      '#rows' => 1,
      '#description' => $this->t('Provide CSS selectors for non-content sections that repeat throughout the site, e.g. "header, footer." Dashboard data for alerts and dismissals in these regions will be stored as a single value on a site-wide basis rather than a per-page basis, and findable via template filters.'), // @todo template filters and storage.
      '#default_value' => $config->get('developer_template_regions'),
    ];

    $form['developer_ignore_elements'] = [
      '#title' => $this->t("Skip over these elements"),
      '#type' => 'textarea',
      '#rows' => 1,
      '#description' => $this->t('Provide a comma-separated list of selectors for elements to ignore for all users. Additional elements can be ignored for content editors.<br>Content editors are ignoring: ') . $config->get('ignore_elements'),
      '#default_value' => $config->get('developer_ignore_elements'),
    ];

    $tests = (new TestNames())->devTestNames();
    /*$form['developer_ignore_tests'] = [
      '#title' => $this->t("Turn off tests for developer roles"),
      '#description' => $this->t("Content editors have a separate list."),
      '#type' => 'checkboxes',
      '#options' => $tests,
      '#default_value' => $config->get('developer_ignore_tests') ?? (new TestNames())->offByDefault(), // @Todo only store if it isn't exactly this.
    ];*/

    $form['additional_tests'] = [
      '#title' => $this->t('Manage who receives alerts for advanced & developer tests'),
      '#markup' => '<p><em>' . $this->t('Defaults are marked with a *')  . '</em></p>',
      '#type' => 'details',
    ];

    $groupLabels = [
      'HEADING_MISSING_ONE' => $this->t('Content & Layout'),
      'BTN_EMPTY' => $this->t('Links & Labels'),
      'CONTRAST_ERROR' => $this->t('Contrast'),
      'HIDDEN_FOCUSABLE' => $this->t('Code'),
    ];
    $activeGroup = '';
    foreach($tests as $key => $value) {

      if (array_key_exists($key, $groupLabels)) {
        $activeGroup = $key;
        $form['additional_tests'][$activeGroup] = [
          '#type' => 'fieldset',
          '#title' => $groupLabels[$key],
        ];
      }

      $options = [
        'content' => $this->t('Content editors'),
        'developers' => $this->t('Developers'),
        'both' => $this->t('Both'),
        'neither' => $this->t('Neither'),
      ];
      $default = in_array($key, $config->get('developer_ignore_tests') ?? []) ? $config->get('developer_ignore_tests')->$key : FALSE;
      if (!$default) {
        $default = in_array($key, (new TestNames())->offByDefault()) ? 'neither' : 'developers';
      }
      $options[$default] .= '*';
      $form['additional_tests'][$activeGroup][$key] = [
        '#title' => $value,
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => $default, // @Todo only store if it isn't exactly this.
      ];
    }



    return parent::buildForm($form, $form_state);

  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    /*$this->config('editoria11y.settings')
      ->set('assertiveness', $form_state->getValue('assertiveness'))
      ->set('ignore_elements', $form_state->getValue('ignore_elements'))
      ->set('content_root', $form_state->getValue('content_root'))
      ->set('ignore_tests', $form_state->getValue('ignore_tests'))
      ->save();*/
    // @todo create on for getValue for each in testNames.
    $this->config('editoria11y_developer.settings')
      ->set('developer_assertiveness', $form_state->getValue('developer_assertiveness'))
      ->set('developer_ignore_elements', $form_state->getValue('developer_ignore_elements'))
      ->set('developer_content_root', $form_state->getValue('developer_content_root'))
      ->set('developer_ignore_tests', $form_state->getValue('developer_ignore_tests'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc