user_preferences.module 5.85 KB
Newer Older
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?php
/**
 * @file
 * Lesson User Preferences module.
 */

use \Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\node\Entity\Node;

function user_preferences_page_attachments_alter(&$build){
	if(!\Drupal::service('router.admin_context')->isAdminRoute()){
		$build['#attached']['library'][] = 'user_preferences/user-preferences';
	}
}

/**
 * Implements hook_theme().
 */
function user_preferences_theme($existing, $type, $theme, $path) {
  return array(
    'user_preferences' => array(
      'variables' => array(),
    ),
  );
}

/**
 * Template preprocess function for User Preferences.
 *
 * @param array $variables
 *   An associative array containing:
 */
function user_template_preprocess_preferences(&$variables) {

}

/**
 * Implements hook_node_links_alter().
 */
function user_preferences_node_links_alter(array &$links, NodeInterface $node, array &$context) {
  if ($context['view_mode'] != 'rss') {
    $user = \Drupal::currentUser();
    $is_admin = in_array('administrator', $user->getRoles());
    $is_front = \Drupal::service('path.matcher')->isFrontPage();
48
    $is_book = isset($node->book['bid']);
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
49 50

    if(!$is_admin){
51
        $can_add_child_page = $user->hasPermission('Add content and child pages to books and manage their hierarchies');
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
52 53 54 55 56 57 58 59 60 61
    }

	if ($context['view_mode'] == 'full' && node_is_page($node)) {
    $nid = $node->id();

    $link['toggleSidebar'] = [
    'title' => t('<i class="fal fa-arrow-alt-from-right">&nbsp;</i>'),
    'url' => Url::fromUri('internal:#toggleSidebar'),
    'attributes' => [
        'id' => 'sidebar_collapse',
62
        'class' => 'd-none d-md-block',
ROBERT PATRICK CAREY III's avatar
111318  
ROBERT PATRICK CAREY III committed
63
        'data-tippy-content' => t('Toggle sidebar'),
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
64 65 66 67 68 69 70 71 72
        'data-toggle' => 'tooltip',
        'aria-label' => 'Toggle sidebar',
      ]
    ];
    if($is_admin){
        $link['accessibilityPanel'] = [
        'title' => t('<i class="fal fa-universal-access">&nbsp;</i>'),
        'url' => Url::fromUri('internal:#toggleA11y'),
        'attributes' => [
ROBERT PATRICK CAREY III's avatar
ROBERT PATRICK CAREY III committed
73 74 75 76 77
          'data-tippy-content' => t('Accessibility Tools'),
          'id' => 'toggle_a11y_tools',
          'onclick' => 'jQuery("#accessibilityPanel").modal("toggle")',
          'data-toggle' => 'tooltip',
          'aria-label' => 'Accessibility tools',
78 79 80
          ]
        ];
        $link['paragraphInspector'] = [
ROBERT PATRICK CAREY III's avatar
ROBERT PATRICK CAREY III committed
81
          'title' => t('<i class="fal fa-bullseye-pointer">&nbsp;</i>'),
82 83
          'url' => Url::fromUri('internal:#toggleInspector'),
          'attributes' => [
ROBERT PATRICK CAREY III's avatar
ROBERT PATRICK CAREY III committed
84 85 86 87 88
            'data-tippy-content' => t('Paragraph Inspector (disabled)'),
            'id' => 'toggle_inspector',
            'class' => 'toggle-inspector',
            'data-toggle' => 'tooltip',
            'aria-label' => 'Paragraph Inspector',
89
          ]
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
90 91 92 93 94 95
        ];
    }
    $link['keyboardShortcuts'] = [
      'title' => t('<i class="fal fa-keyboard">&nbsp;</i>'),
      'url' => Url::fromUri('internal:#toggleShortcuts'),
      'attributes' => [
ROBERT PATRICK CAREY III's avatar
111318  
ROBERT PATRICK CAREY III committed
96
        'data-tippy-content' => t('Keyboard Shortcuts'),
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
97 98 99 100 101 102 103 104 105 106 107 108
        'id' => 'toggle_help',
        'onclick' => 'jQuery("#keyboardShortcuts").modal("toggle")',
        'data-toggle' => 'tooltip',
        'aria-label' => 'Keyboard shortcuts',
      ]
    ];
    $link['userPreferences'] = [
      'title' => t('<i class="fal fa-sliders-h-square">&nbsp;</i>'),
      'url' => Url::fromUri('internal:#togglePreferences'),
      'attributes' => [
        'id' => 'toggle_preferences',
        'onclick' => 'jQuery("#userPreferences").modal("toggle")',
109
        'data-tippy-content' => t('Preferences'),
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
110 111 112 113 114 115 116 117 118 119 120
        'data-toggle' => 'tooltip',
        'aria-label' => 'User preferences',
      ]
    ];
    $link['fullScreen'] = [
      'title' => t('<i class="fal fa-expand">&nbsp;</i>'
        ),
      'url' => Url::fromUri('internal:#fullScreen'),
      'attributes' => [
        'id' => 'toggle_fullscreen',
        'class' => 'toggle-fullscreen',
ROBERT PATRICK CAREY III's avatar
111318  
ROBERT PATRICK CAREY III committed
121
        'data-tippy-content' => t('Enter Fullscreen'),
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
122 123 124 125
        'data-toggle' => 'tooltip',
        'aria-label' => 'Toggle fullscreen',
      ]
    ];
126
    if(($is_admin || $can_add_child_page) && $is_book){
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
127 128 129 130 131 132 133 134 135 136
        $link['addPage'] = [
        'title' => t('
            <span class="fa-layers">
                <i class="fal fa-book">&nbsp;</i>
                <i class="fas fa-plus-circle" data-fa-transform="shrink-8 up-9 right-8">&nbsp;</i>
            </span>
        '),
        'url' => Url::fromUri('internal:/node/add/lesson?parent='.$nid),
        'attributes' => [
            'id' => 'add_child_page',
ROBERT PATRICK CAREY III's avatar
111318  
ROBERT PATRICK CAREY III committed
137
            'data-tippy-content' => t('Add child page'),
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
138 139 140 141
            'data-toggle' => 'tooltip',
            'aria-label' => 'Add child page',
        ]
        ];
ROBERT PATRICK CAREY III's avatar
ROBERT PATRICK CAREY III committed
142
        /*
143 144 145 146 147 148 149 150 151
        $link['createCitation'] = [
          'title' => t('<i class="fal fa-quote-left">&nbsp;</i>'
            ),
          'url' => Url::fromUri('internal:#createCitation'),
          'attributes' => [
            'title' => t('Create Citation (not functional)'),
            'id' => 'create_citation',
            'class' => 'create-citation',
            'onclick' => 'createCitation()',
ROBERT PATRICK CAREY III's avatar
111318  
ROBERT PATRICK CAREY III committed
152
            'data-tippy-content' => t('Create Citation (not functional)'),
153 154 155 156
            'data-toggle' => 'tooltip',
            'aria-label' => 'Copy Citation',
          ]
        ];
ROBERT PATRICK CAREY III's avatar
ROBERT PATRICK CAREY III committed
157
        */
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
158
    }
159
    if(!$is_front && $is_book){
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
160 161 162 163 164 165
        $link['printerFriendly'] = [
        'title' => t('<i class="fal fa-print">&nbsp;</i>'),
        'url' => Url::fromUri('internal:/book/export/html/'.$nid),
        'attributes' => [
            'id' => 'printer_friendly',
            'data-toggle' => 'tooltip',
ROBERT PATRICK CAREY III's avatar
111318  
ROBERT PATRICK CAREY III committed
166
            'data-tippy-content' => t('Printer friendly version'),
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
167
            'aria-label' => 'Printer friendly version',
168
          ]
ROBERT PATRICK CAREY III's avatar
Init  
ROBERT PATRICK CAREY III committed
169 170 171 172 173 174 175 176 177 178 179 180 181
        ];
    }
    }

    if (!empty($link)) {
      $links['user_preferences'] = [
        '#theme' => 'links__user_preferences',
        '#links' => $link,
        '#attributes' => ['class' => ['links', 'inline']],
      ];
    }
  }
}