views_ui.theme.inc 16.8 KB
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
<?php

/**
 * @file
 * Preprocessors and theme functions for the Views UI.
 */

use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Form\FormState;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;

/**
 * Prepares variables for Views UI display tab setting templates.
 *
 * Default template: views-ui-display-tab-setting.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - link: The setting's primary link.
 *   - settings_links: An array of links for this setting.
 *   - defaulted: A boolean indicating the setting is in its default state.
 *   - overridden: A boolean indicating the setting has been overridden from
 *     the default.
 *   - description: The setting's description.
 *   - description_separator: A boolean indicating a separator colon should be
 *     appended to the setting's description.
 */
function template_preprocess_views_ui_display_tab_setting(&$variables) {
  // Put the primary link to the left side.
  array_unshift($variables['settings_links'], $variables['link']);

  if (!empty($variables['overridden'])) {
    $variables['attributes']['title'][] = t('Overridden');
  }

  // Append a colon to the description, if requested.
  if ($variables['description'] && $variables['description_separator']) {
    $variables['description'] .= t(':');
  }
}

/**
 * Prepares variables for Views UI display tab bucket templates.
 *
 * Default template: views-ui-display-tab-bucket.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing the properties of the element.
 *     Properties used: #name, #overridden, #children, #title, #actions.
 */
function template_preprocess_views_ui_display_tab_bucket(&$variables) {
  $element = $variables['element'];

  if (!empty($element['#overridden'])) {
    $variables['attributes']['title'][] = t('Overridden');
  }

  $variables['name'] = isset($element['#name']) ? $element['#name'] : NULL;
  $variables['overridden'] = isset($element['#overridden']) ? $element['#overridden'] : NULL;
  $variables['content'] = $element['#children'];
  $variables['title'] = $element['#title'];
  $variables['actions'] = !empty($element['#actions']) ? $element['#actions'] : array();
}

/**
 * Prepares variables for Views UI build group filter form templates.
 *
 * Default template: views-ui-build-group-filter-form.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 */
function template_preprocess_views_ui_build_group_filter_form(&$variables) {
  $form = $variables['form'];

  // Prepare table of options.
  $header = array(
    t('Default'),
    t('Weight'),
    t('Label'),
    t('Operator'),
    t('Value'),
    t('Operations'),
  );

  // Prepare default selectors.
  $form_state = new FormState();
  $form['default_group'] = Element\Radios::processRadios($form['default_group'], $form_state, $form);
  $form['default_group_multiple'] = Element\Checkboxes::processCheckboxes($form['default_group_multiple'], $form_state, $form);
  $form['default_group']['All']['#title'] = '';

  $rows[] = array(
    ['data' => $form['default_group']['All']],
    '',
    array(
      'data' => \Drupal::config('views.settings')->get('ui.exposed_filter_any_label') == 'old_any' ? t('&lt;Any&gt;') : t('- Any -'),
      'colspan' => 4,
      'class' => array('class' => 'any-default-radios-row'),
    ),
  );
  // Remove the 'All' default_group form element because it's added to the
  // table row.
  unset($variables['form']['default_group']['All']);

  foreach (Element::children($form['group_items']) as $group_id) {
    $form['group_items'][$group_id]['value']['#title'] = '';
    $default = [
      $form['default_group'][$group_id],
      $form['default_group_multiple'][$group_id],
    ];
    // Remove these fields from the form since they are moved into the table.
    unset($variables['form']['default_group'][$group_id]);
    unset($variables['form']['default_group_multiple'][$group_id]);

    $link = [
      '#type' => 'link',
      '#url' => Url::fromRoute('<none>', [], [
        'attributes' => [
          'id' => 'views-remove-link-' . $group_id,
          'class' => [
            'views-hidden',
            'views-button-remove',
            'views-groups-remove-link',
            'views-remove-link',
          ],
          'alt' => t('Remove this item'),
          'title' => t('Remove this item'),
        ],
      ]),
      '#title' => SafeMarkup::format('<span>@text</span>', ['@text' => t('Remove')]),
    ];
    $remove = [$form['group_items'][$group_id]['remove'], $link];
    $data = array(
      'default' => ['data' => $default],
      'weight' => ['data' => $form['group_items'][$group_id]['weight']],
      'title' => ['data' => $form['group_items'][$group_id]['title']],
      'operator' => ['data' => $form['group_items'][$group_id]['operator']],
      'value' => ['data' => $form['group_items'][$group_id]['value']],
      'remove' => ['data' => $remove],
    );
    $rows[] = array('data' => $data, 'id' => 'views-row-' . $group_id, 'class' => array('draggable'));
  }
  $variables['table'] = array(
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array(
      'class' => array('views-filter-groups'),
      'id' => 'views-filter-groups',
    ),
    '#tabledrag' => array(
      array(
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'weight',
      )
    ),
  );

  // Hide fields used in table.
  unset($variables['form']['group_items']);
}

/**
 * Prepares variables for Views UI rearrange filter form templates.
 *
 * Default template: views-ui-rearrange-filter-form.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 */
function template_preprocess_views_ui_rearrange_filter_form(&$variables) {
  $form = &$variables['form'];
  $rows = $ungroupable_rows = array();
  // Enable grouping only if > 1 group.
  $variables['grouping'] = count(array_keys($form['#group_options'])) > 1;

  foreach ($form['#group_renders'] as $group_id => $contents) {
    // Header row for the group.
    if ($group_id !== 'ungroupable') {
      // Set up tabledrag so that it changes the group dropdown when rows are
      // dragged between groups.
      $options = array(
        'table_id' => 'views-rearrange-filters',
        'action' => 'match',
        'relationship' => 'sibling',
        'group' => 'views-group-select',
        'subgroup' => 'views-group-select-' . $group_id,
      );
      drupal_attach_tabledrag($form['override'], $options);

      // Title row, spanning all columns.
      $row = array();
      // Add a cell to the first row, containing the group operator.
      $row[] = array(
        'class' => array('group', 'group-operator', 'container-inline'),
        'data' => $form['filter_groups']['groups'][$group_id],
        'rowspan' => max(array(2, count($contents) + 1)),
      );
      // Title.
      $row[] = array(
        'class' => array('group', 'group-title'),
        'data' => array(
          '#prefix' => '<span>',
          '#markup' => $form['#group_options'][$group_id],
          '#suffix' => '</span>',
        ),
        'colspan' => 4,
      );
      $rows[] = array(
        'class' => array('views-group-title'),
        'data' => $row,
        'id' => 'views-group-title-' . $group_id,
      );

      // Row which will only appear if the group has nothing in it.
      $row = array();
      $class = 'group-' . (count($contents) ? 'populated' : 'empty');
      $instructions = '<span>' . t('No filters have been added.') . '</span> <span class="js-only">' . t('Drag to add filters.') . '</span>';
      // When JavaScript is enabled, the button for removing the group (if it's
      // present) should be hidden, since it will be replaced by a link on the
      // client side.
      if (!empty($form['remove_groups'][$group_id]['#type']) && $form['remove_groups'][$group_id]['#type'] == 'submit') {
        $form['remove_groups'][$group_id]['#attributes']['class'][] = 'js-hide';
      }
      $row[] = array(
        'colspan' => 5,
        'data' => array(
          array('#markup' => $instructions),
          $form['remove_groups'][$group_id],
        ),
      );
      $rows[] = array(
        'class' => array(
          'group-message',
          'group-' . $group_id . '-message',
          $class,
        ),
        'data' => $row,
        'id' => 'views-group-' . $group_id,
      );
    }

    foreach ($contents as $id) {
      if (isset($form['filters'][$id]['name'])) {
        $row = array();
        $row[]['data'] = $form['filters'][$id]['name'];
        $form['filters'][$id]['weight']['#attributes']['class'] = array('weight');
        $row[]['data'] = $form['filters'][$id]['weight'];
        $form['filters'][$id]['group']['#attributes']['class'] = array('views-group-select views-group-select-' . $group_id);
        $row[]['data'] = $form['filters'][$id]['group'];
        $form['filters'][$id]['removed']['#attributes']['class'][] = 'js-hide';

        $remove_link = array(
          '#type' => 'link',
          '#url' => Url::fromRoute('<none>'),
          '#title' => SafeMarkup::format('<span>@text</span>', array('@text' => t('Remove'))),
          '#weight' => '1',
          '#options' => array(
            'attributes' => array(
              'id' => 'views-remove-link-' . $id,
              'class' => array(
                'views-hidden',
                'views-button-remove',
                'views-groups-remove-link',
                'views-remove-link',
              ),
              'alt' => t('Remove this item'),
              'title' => t('Remove this item'),
            ),
          ),
        );
        $row[]['data'] = array(
          $form['filters'][$id]['removed'],
          $remove_link,
        );

        $row = array(
          'data' => $row,
          'class' => array('draggable'),
          'id' => 'views-row-' . $id,
        );

        if ($group_id !== 'ungroupable') {
          $rows[] = $row;
        }
        else {
          $ungroupable_rows[] = $row;
        }
      }
    }
  }

  if (!$variables['grouping']) {
    $form['filter_groups']['groups'][0]['#title'] = t('Operator');
  }

  if (!empty($ungroupable_rows)) {
    $header = array(
      t('Ungroupable filters'),
      t('Weight'),
      array(
        'data' => t('Group'),
        'class' => array('views-hide-label'),
      ),
      array(
        'data' => t('Remove'),
        'class' => array('views-hide-label'),
      ),
    );
    $variables['ungroupable_table'] = array(
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $ungroupable_rows,
      '#attributes' => array(
        'id' => 'views-rearrange-filters-ungroupable',
        'class' => array('arrange'),
      ),
      '#tabledrag' => array(
        array(
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'weight',
        )
      ),
    );
  }

  if (empty($rows)) {
    $rows[] = array(array('data' => t('No fields available.'), 'colspan' => '2'));
  }

  // Set up tabledrag so that the weights are changed when rows are dragged.
  $variables['table'] = array(
    '#type' => 'table',
    '#rows' => $rows,
    '#attributes' => array(
      'id' => 'views-rearrange-filters',
      'class' => array('arrange'),
    ),
    '#tabledrag' => array(
      array(
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'weight',
      ),
    ),
  );

  // When JavaScript is enabled, the button for adding a new group should be
  // hidden, since it will be replaced by a link on the client side.
  $form['actions']['add_group']['#attributes']['class'][] = 'js-hide';

}

/**
 * Prepares variables for style plugin table templates.
 *
 * Default template: views-ui-style-plugin-table.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 */
function template_preprocess_views_ui_style_plugin_table(&$variables) {
  $form = $variables['form'];

  $header = array(
    t('Field'),
    t('Column'),
    t('Align'),
    t('Separator'),
    array(
      'data' => t('Sortable'),
      'align' => 'center',
    ),
    array(
      'data' => t('Default order'),
      'align' => 'center',
    ),
    array(
      'data' => t('Default sort'),
      'align' => 'center',
    ),
    array(
      'data' => t('Hide empty column'),
      'align' => 'center',
    ),
    array(
      'data' => t('Responsive'),
      'align' => 'center',
    ),
  );
  $rows = array();
  foreach (Element::children($form['columns']) as $id) {
    $row = array();
    $row[]['data'] = $form['info'][$id]['name'];
    $row[]['data'] = $form['columns'][$id];
    $row[]['data'] = $form['info'][$id]['align'];
    $row[]['data'] = $form['info'][$id]['separator'];

    if (!empty($form['info'][$id]['sortable'])) {
      $row[] = array(
        'data' => $form['info'][$id]['sortable'],
        'align' => 'center',
      );
      $row[] = array(
        'data' => $form['info'][$id]['default_sort_order'],
        'align' => 'center',
      );
      $row[] = array(
        'data' => $form['default'][$id],
        'align' => 'center',
      );
    }
    else {
      $row[] = '';
      $row[] = '';
      $row[] = '';
    }
    $row[] = array(
      'data' => $form['info'][$id]['empty_column'],
      'align' => 'center',
    );
    $row[] = array(
      'data' => $form['info'][$id]['responsive'],
      'align' => 'center',
    );
    $rows[] = $row;
  }

  // Add the special 'None' row.
  $rows[] = array(array('data' => t('None'), 'colspan' => 6), array('align' => 'center', 'data' => $form['default'][-1]), array('colspan' => 2));

  // Unset elements from the form array that are used to build the table so that
  // they are not rendered twice.
  unset($form['default']);
  unset($form['info']);
  unset($form['columns']);

  $variables['table'] = array(
    '#type' => 'table',
    '#theme' => 'table__views_ui_style_plugin_table',
    '#header' => $header,
    '#rows' => $rows,
  );
  $variables['form'] = $form;
}

/**
 * Prepares variables for views UI view preview section templates.
 *
 * Default template: views-ui-view-preview-section.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - view: The view object.
 *   - section: The section name of a View (e.g. title, rows or pager).
 */
function template_preprocess_views_ui_view_preview_section(&$variables) {
  switch ($variables['section']) {
    case 'title':
      $variables['title'] = t('Title');
      $links = views_ui_view_preview_section_display_category_links($variables['view'], 'title', $variables['title']);
      break;
    case 'header':
      $variables['title'] = t('Header');
      $links = views_ui_view_preview_section_handler_links($variables['view'], $variables['section']);
      break;
    case 'empty':
      $variables['title'] = t('No results behavior');
      $links = views_ui_view_preview_section_handler_links($variables['view'], $variables['section']);
      break;
    case 'exposed':
      // @todo Sorts can be exposed too, so we may need a better title.
      $variables['title'] = t('Exposed Filters');
      $links = views_ui_view_preview_section_display_category_links($variables['view'], 'exposed_form_options', $variables['title']);
      break;
    case 'rows':
      // @todo The title needs to depend on what is being viewed.
      $variables['title'] = t('Content');
      $links = views_ui_view_preview_section_rows_links($variables['view']);
      break;
    case 'pager':
      $variables['title'] = t('Pager');
      $links = views_ui_view_preview_section_display_category_links($variables['view'], 'pager_options', $variables['title']);
      break;
    case 'more':
      $variables['title'] = t('More');
      $links = views_ui_view_preview_section_display_category_links($variables['view'], 'use_more', $variables['title']);
      break;
    case 'footer':
      $variables['title'] = t('Footer');
      $links = views_ui_view_preview_section_handler_links($variables['view'], $variables['section']);
      break;
    case 'attachment_before':
      // @todo: Add links to the attachment configuration page.
      $variables['title'] = t('Attachment before');
      break;
    case 'attachment_after':
      // @todo: Add links to the attachment configuration page.
      $variables['title'] = t('Attachment after');
      break;
  }

  if (isset($links)) {
    $build = array(
      '#theme' => 'links__contextual',
      '#links' => $links,
      '#attributes' => array('class' => array('contextual-links')),
      '#attached' => array(
        'library' => array('contextual/drupal.contextual-links'),
      ),
    );
    $variables['links'] = $build;
  }
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function views_ui_theme_suggestions_views_ui_view_preview_section(array $variables) {
  return array('views_ui_view_preview_section__' . $variables['section']);
}