keyboard_shortcuts-1.0.0/keyboard_shortcuts.module

keyboard_shortcuts.module
<?php

/**
 * Implements hook_help().
 *
 * @param $path
 * @param $arg
 *
 * @return void
 */

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function keyboard_shortcuts_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.keyboard_shortcuts':
      $output=<<<QQ
        <h3>About</h3>
        <p>Keyboard shortcuts allows you to create keyboard shortcuts on your website. Shortcuts can be configured to:</p>
        <ul>
            <li>perform an action (click) on an element (using css selectors)</li>
            <li>redirect to a url</li>
            <li>open a url in a modal</li>
            <li>call a custom javascript callback</li>
        </ul>
        <p>For more information and OS/browser limitations see the website of the js library <a href=":library_url">http://craig.is/killing/mice</a>.</p>

        <h3>Default shortcuts</h3>
        <p>After installing the module, 3 shortcuts will be available by default.</p>
        <ul>
            <li>ctrl+n or command+n : opens /node/add in a modal.</li>
            <li>ctrl+s or command+s : saves a form (#edit-submit buttons).</li>
            <li>ctrl+e or command+e : opens the edit form of the current node (if available).</li>
        </ul>
        <p>Note that some browsers don't allow to overwrite system/browser shortcuts. Safari for example prevents command+n, so use the alternative ctrl+n.</p>

        <h3>Configuring Keyboard shortcuts')</h3>
        <p>All settings for this module are on the Keyboard shortcuts configuration page, which can be found at Administration &gt; Configuration &gt; User inferface &gt; <a href=":config_url">Keyboard shortcuts.</a>. A detailed description of the possibilities is given in the configuration form.</p>
QQ;

      return t($output, [
        ':library_url' => 'http://craig.is/killing/mice',
        ':config_url' => '/admin/config/user-interface/keyboard-shortcuts'
      ]);
  }
}

/**
 * Implements hook_page_attachments().
 *
 * @param $page
 */
function keyboard_shortcuts_page_attachments(&$page) {

  $user = \Drupal::currentUser();

  if ($user->hasPermission('use keyboard shortcuts')) {
    $page['#attached']['library'][] = 'keyboard_shortcuts/keyboard_shortcuts';
    $config = \Drupal::config('keyboard_shortcuts.settings');
    $shortcuts = $config->get('shortcuts');

    // Remove shortcuts we are not allowed to use.
    if ( !empty($keystroke['roles'])) {
      $shortcuts = array_filter($shortcuts, function ($shortcut) use ($user) {
        return !empty(array_intersect($user->getRoles(), $shortcut['roles']));
      });
    }

    // Token replace the arguments.
    $node = \Drupal::routeMatch()->getParameter('node');

    array_walk($shortcuts, function(&$keystroke) use ($user, $node) {

      if (!empty($keystroke['arguments']['url'])) {
        // Make sure urls are expanded.
        if ($keystroke['arguments']['url'][0] == '/') {
          $url = \Drupal\Core\Url::fromUserInput($keystroke['arguments']['url']);
          $keystroke['arguments']['url'] = $url->setAbsolute(true)->toString();
        }
      }

      array_walk($keystroke['arguments'], function(&$argument) use ($user, $node) {

        $token_service = \Drupal::token();

        $argument = $token_service->replace($argument, [
          'node' => $node,
          'user' => $user,
        ], []);

      });

    });

    $page['#attached']['drupalSettings']['keyboard_shortcuts']['shortcuts'] = $shortcuts;
  }

}

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

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