views_ui.module 26.9 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Provide structure for the administrative interface to Views.
 */

8
use Drupal\views\ViewExecutable;
9
use Drupal\views\ViewUI;
10
use Drupal\Core\Entity\EntityInterface;
11

merlinofchaos's avatar
merlinofchaos committed
12 13 14 15 16 17 18 19 20 21 22 23
/**
 * Implements hook_menu().
 */
function views_ui_menu() {
  $items = array();

  // Minor code reduction technique.
  $base = array(
    'access callback' => 'user_access',
    'access arguments' => array('administer views'),
    'file' => 'includes/admin.inc',
  );
24 25 26 27 28 29
  // Set up the base for AJAX callbacks.
  $ajax_base = array(
    'page callback' => 'views_ui_ajax_callback',
    'page arguments' => array(4, 5),
    'type' => MENU_CALLBACK,
  ) + $base;
merlinofchaos's avatar
merlinofchaos committed
30 31 32 33 34 35 36 37

  // Top-level Views module pages (not tied to a particular View).
  $items['admin/structure/views/add'] = array(
    'title' => 'Add new view',
    'page callback' => 'views_ui_add_page',
    'type' => MENU_LOCAL_ACTION,
  ) + $base;

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  $items['admin/structure/views'] = array(
    'title' => 'Views',
    'description' => 'Manage customized lists of content.',
    'page callback' => 'views_ui_list_page',
  ) + $base;

  $items['admin/structure/views/list'] = array(
    'title' => 'List',
    'weight' => -10,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  ) + $base;

  $items['admin/structure/views/view/%views_ui/enable'] = array(
    'title' => 'Enable a view',
  ) + $ajax_base;

  $items['admin/structure/views/view/%views_ui/disable'] = array(
    'title' => 'Disable a view',
  ) + $ajax_base;

58
  /*
merlinofchaos's avatar
merlinofchaos committed
59 60 61 62 63 64 65
  // Top-level Views module pages (not tied to a particular View).
  $items['admin/structure/views/add-template'] = array(
    'title' => 'Add view from template',
    'page callback' => 'views_ui_add_template_page',
    // Don't show a local action link if there aren't any templates.
    'type' => views_get_all_templates() ? MENU_LOCAL_ACTION : MENU_VISIBLE_IN_BREADCRUMB,
  ) + $base;
66
  */
merlinofchaos's avatar
merlinofchaos committed
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

  $items['admin/structure/views/import'] = array(
    'title' => 'Import',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('views_ui_import_page'),
    'access callback' => 'views_import_access',
    'type' => MENU_LOCAL_ACTION,
  ) + $base;

  $items['admin/structure/views/settings'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('views_ui_admin_settings_basic'),
    'type' => MENU_LOCAL_TASK,
  ) + $base;
  $items['admin/structure/views/settings/basic'] = array(
    'title' => 'Basic',
    'page arguments' => array('views_ui_admin_settings_basic'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
  ) + $base;
  $items['admin/structure/views/settings/advanced'] = array(
    'title' => 'Advanced',
    'page arguments' => array('views_ui_admin_settings_advanced'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  ) + $base;

  // The primary Edit View page. Secondary tabs for each Display are added in
  // views_ui_menu_local_tasks_alter().
  $items['admin/structure/views/view/%views_ui_cache'] = array(
    'title callback' => 'views_ui_edit_page_title',
    'title arguments' => array(4),
    'page callback' => 'views_ui_edit_page',
    'page arguments' => array(4),
  ) + $base;
  $items['admin/structure/views/view/%views_ui_cache/edit'] = array(
    'title' => 'Edit view',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'weight' => -10,
    'theme callback' => 'ajax_base_page_theme',
  ) + $base;
  $items['admin/structure/views/view/%views_ui_cache/edit/%/ajax'] = array(
    'page callback' => 'views_ui_ajax_get_form',
    'page arguments' => array('views_ui_edit_form', 4, 6),
    'delivery callback' => 'ajax_deliver',
    'theme callback' => 'ajax_base_page_theme',
    'type' => MENU_CALLBACK,
  ) + $base;
  $items['admin/structure/views/view/%views_ui_cache/preview/%'] = array(
    'page callback' => 'views_ui_build_preview',
    'page arguments' => array(4, 6),
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  ) + $base;
  $items['admin/structure/views/view/%views_ui_cache/preview/%/ajax'] = array(
    'page callback' => 'views_ui_build_preview',
    'page arguments' => array(4, 6),
    'delivery callback' => 'ajax_deliver',
    'theme callback' => 'ajax_base_page_theme',
    'type' => MENU_CALLBACK,
  ) + $base;

  // Additional pages for acting on a View.

  $items['admin/structure/views/view/%views_ui_cache/break-lock'] = array(
    'title' => 'Break lock',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('views_ui_break_lock_confirm', 4),
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  ) + $base;

  // NoJS/AJAX callbacks that can use the default Views AJAX form system.
  $items['admin/structure/views/nojs/%/%views_ui_cache'] = array(
    'page callback' => 'views_ui_ajax_form',
    'page arguments' => array(FALSE, 4, 5),
    'type' => MENU_CALLBACK,
  ) + $base;
  $items['admin/structure/views/ajax/%/%views_ui_cache'] = array(
    'page callback' => 'views_ui_ajax_form',
    'page arguments' => array(TRUE, 4, 5),
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  ) + $base;

  // NoJS/AJAX callbacks that require custom page callbacks.
  $ajax_callbacks = array(
    'preview' => 'views_ui_preview',
  );
  foreach ($ajax_callbacks as $menu => $menu_callback) {
    $items['admin/structure/views/nojs/' . $menu . '/%views_ui_cache/%'] = array(
      'page callback' => $menu_callback,
      'page arguments' => array(5, 6),
    ) + $base;
    $items['admin/structure/views/ajax/' . $menu . '/%views_ui_cache/%'] = array(
      'page callback' => $menu_callback,
      'page arguments' => array(5, 6),
      'delivery callback' => 'ajax_deliver',
    ) + $base;
  }

  // Autocomplete callback for tagging a View.
  // Views module uses admin/views/... instead of admin/structure/views/... for
  // autocomplete paths, so be consistent with that.
  // @todo Change to admin/structure/views/... when the change can be made to
  //   Views module as well.
  $items['admin/views/ajax/autocomplete/tag'] = array(
    'page callback' => 'views_ui_autocomplete_tag',
    'type' => MENU_CALLBACK,
  ) + $base;

  // A page in the Reports section to show usage of fields in all views
179 180 181 182 183 184 185
  $items['admin/reports/fields/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/reports/fields/views-fields'] = array(
    'title' => 'Used in views',
merlinofchaos's avatar
merlinofchaos committed
186 187
    'description' => 'Overview of fields used in all views.',
    'page callback' => 'views_ui_field_list',
188 189
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
merlinofchaos's avatar
merlinofchaos committed
190 191
  ) + $base;

192 193 194 195 196 197 198
  // A page in the Reports section to show usage of plugins in all views.
  $items['admin/reports/views-plugins'] = array(
    'title' => 'Views plugins',
    'description' => 'Overview of plugins used in all views.',
    'page callback' => 'views_ui_plugin_list',
  ) + $base;

merlinofchaos's avatar
merlinofchaos committed
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
  return $items;
}

/**
 * Implements hook_theme().
 */
function views_ui_theme() {
  $path = drupal_get_path('module', 'views');
  require_once DRUPAL_ROOT . "/$path/includes/admin.inc";

  return array(
    // edit a view
    'views_ui_display_tab_setting' => array(
      'variables' => array('description' => '', 'link' => '', 'settings_links' => array(), 'overridden' => FALSE, 'defaulted' => FALSE, 'description_separator' => TRUE, 'class' => array()),
      'template' => 'views-ui-display-tab-setting',
      'path' => "$path/theme",
    ),
    'views_ui_display_tab_bucket' => array(
      'render element' => 'element',
      'template' => 'views-ui-display-tab-bucket',
      'path' => "$path/theme",
    ),
    'views_ui_edit_item' => array(
      'variables' => array('type' => NULL, 'view' => NULL, 'display' => NULL, 'no_fields' => FALSE),
      'template' => 'views-ui-edit-item',
      'path' => "$path/theme",
    ),
    'views_ui_rearrange_form' => array(
      'render element' => 'form',
    ),
    'views_ui_rearrange_filter_form' => array(
      'render element' => 'form',
      'file' => 'includes/admin.inc',
    ),
    'views_ui_expose_filter_form' => array(
      'render element' => 'form',
      'file' => 'includes/admin.inc',
    ),

    // list views
    'views_ui_view_info' => array(
      'variables' => array('view' => NULL, 'base' => NULL),
      'file' => "includes/admin.inc",
    ),

244 245 246 247 248 249
    // Group of filters.
    'views_ui_build_group_filter_form' => array(
      'render element' => 'form',
      'file' => 'includes/admin.inc',
    ),

merlinofchaos's avatar
merlinofchaos committed
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
    // tab themes
    'views_tabset' => array(
      'variables' => array('tabs' => NULL),
    ),
    'views_tab' => array(
      'variables' => array('body' => NULL),
    ),
    'views_ui_reorder_displays_form' => array(
      'render element' => 'form',
      'file' => 'includes/admin.inc',
    ),

    // On behalf of a plugin
    'views_ui_style_plugin_table' => array(
      'render element' => 'form',
    ),

    // When previewing a view.
    'views_ui_view_preview_section' => array(
      'variables' => array('view' => NULL, 'section' => NULL, 'content' => NULL, 'links' => ''),
    ),

    // Generic container wrapper, to use instead of theme_container when an id
    // is not desired.
    'views_container' => array(
      'render element' => 'element',
      'file' => 'theme/theme.inc',
    ),
  );
}

/**
 * Impements hook_custom_theme()
 */
function views_ui_custom_theme() {
285
  $theme = config('views.settings')->get('ui.custom_theme');
merlinofchaos's avatar
merlinofchaos committed
286 287 288 289 290 291 292 293 294 295 296 297 298

  if ($theme != '_default') {
    $available = list_themes();

    if (isset($available[$theme]) && $available[$theme]->status && preg_match('/^admin\/structure\/views/', current_path())) {
      return $theme;
    }
  }
}

/**
 * Page title callback for the Edit View page.
 */
299
function views_ui_edit_page_title(ViewUI $view) {
merlinofchaos's avatar
merlinofchaos committed
300 301
  module_load_include('inc', 'views_ui', 'includes/admin');
  $bases = views_fetch_base_tables();
302 303 304
  $name = $view->storage->getHumanName();
  if (isset($bases[$view->storage->base_table])) {
    $name .= ' (' . $bases[$view->storage->base_table]['title'] . ')';
merlinofchaos's avatar
merlinofchaos committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  }

  return $name;
}

/**
 * Specialized menu callback to load a view and check its locked status.
 *
 * @param $name
 *   The machine name of the view.
 *
 * @return
 *   The view object, with a "locked" property indicating whether or not
 *   someone else is already editing the view.
 */
function views_ui_cache_load($name) {
321 322
  $views_temp_store = views_temp_store();
  $view = $views_temp_store->get($name);
323 324
  $storage = entity_load('view', $name);
  $original_view = $storage ? new ViewUI($storage) : NULL;
merlinofchaos's avatar
merlinofchaos committed
325 326 327 328 329

  if (empty($view)) {
    $view = $original_view;
    if (!empty($view)) {
      // Check to see if someone else is already editing this view.
330
      $view->locked = $views_temp_store->isLocked($view->storage->name);
merlinofchaos's avatar
merlinofchaos committed
331 332 333 334 335 336 337 338 339
      // Set a flag to indicate that this view is being edited.
      // This flag will be used e.g. to determine whether strings
      // should be localized.
      $view->editing = TRUE;
    }
  }
  else {
    // Keep disabled/enabled status real.
    if ($original_view) {
340
      $view->storage->disabled = $original_view->storage->disabled;
merlinofchaos's avatar
merlinofchaos committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    }
  }

  if (empty($view)) {
    return FALSE;
  }

  else {
    return $view;
  }
}

/**
 * Specialized cache function to add a flag to our view, include an appropriate
 * include, and cache more easily.
 */
357
function views_ui_cache_set(ViewUI $view) {
merlinofchaos's avatar
merlinofchaos committed
358 359 360 361
  if (!empty($view->locked)) {
    drupal_set_message(t('Changes cannot be made to a locked view.'), 'error');
    return;
  }
362

merlinofchaos's avatar
merlinofchaos committed
363 364 365 366 367 368 369 370 371 372 373 374
  $view->changed = TRUE; // let any future object know that this view has changed.

  if (isset($view->current_display)) {
    // Add the knowledge of the changed display, too.
    $view->changed_display[$view->current_display] = TRUE;
    unset($view->current_display);
  }

  // Unset handlers; we don't want to write these into the cache
  unset($view->display_handler);
  unset($view->default_display);
  $view->query = NULL;
375 376
  $view->displayHandlers = array();
  views_temp_store()->set($view->storage->name, $view);
merlinofchaos's avatar
merlinofchaos committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
}

/**
 * Theme preprocess for views-view.tpl.php.
 */
function views_ui_preprocess_views_view(&$vars) {
  $view = $vars['view'];
  if (!empty($view->views_ui_context) && module_exists('contextual')) {
    $view->hide_admin_links = TRUE;
    foreach (array('title', 'header', 'exposed', 'rows', 'pager', 'more', 'footer', 'empty', 'attachment_after', 'attachment_before') as $section) {
      if (!empty($vars[$section])) {
        $vars[$section] = array(
          '#theme' => 'views_ui_view_preview_section',
          '#view' => $view,
          '#section' => $section,
          '#content' => is_array($vars[$section]) ? drupal_render($vars[$section]) : $vars[$section],
          '#theme_wrappers' => array('views_container'),
394
          '#attributes' => array('class' => 'contextual-region'),
merlinofchaos's avatar
merlinofchaos committed
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
        );
        $vars[$section] = drupal_render($vars[$section]);
      }
    }
  }
}

/**
 * Theme preprocess for theme_views_ui_view_preview_section().
 *
 * @TODO
 *    Perhaps move this to includes/admin.inc or theme/theme.inc
 */
function template_preprocess_views_ui_view_preview_section(&$vars) {
  switch ($vars['section']) {
    case 'title':
      $vars['title'] = t('Title');
      $links = views_ui_view_preview_section_display_category_links($vars['view'], 'title', $vars['title']);
      break;
    case 'header':
      $vars['title'] = t('Header');
      $links = views_ui_view_preview_section_handler_links($vars['view'], $vars['section']);
      break;
    case 'empty':
      $vars['title'] = t('No results behavior');
      $links = views_ui_view_preview_section_handler_links($vars['view'], $vars['section']);
      break;
    case 'exposed':
      // @todo Sorts can be exposed too, so we may need a better title.
      $vars['title'] = t('Exposed Filters');
      $links = views_ui_view_preview_section_display_category_links($vars['view'], 'exposed_form_options', $vars['title']);
      break;
    case 'rows':
      // @todo The title needs to depend on what is being viewed.
      $vars['title'] = t('Content');
      $links = views_ui_view_preview_section_rows_links($vars['view']);
      break;
    case 'pager':
      $vars['title'] = t('Pager');
      $links = views_ui_view_preview_section_display_category_links($vars['view'], 'pager_options', $vars['title']);
      break;
    case 'more':
      $vars['title'] = t('More');
      $links = views_ui_view_preview_section_display_category_links($vars['view'], 'use_more', $vars['title']);
      break;
    case 'footer':
      $vars['title'] = t('Footer');
      $links = views_ui_view_preview_section_handler_links($vars['view'], $vars['section']);
      break;
    case 'attachment_before':
      // @todo: Add links to the attachment configuration page.
      $vars['title'] = t('Attachment before');
      break;
    case 'attachment_after':
      // @todo: Add links to the attachment configuration page.
      $vars['title'] = t('Attachment after');
      break;
  }

  if (isset($links)) {
    $build = array(
456
      '#prefix' => '<div class="contextual">',
merlinofchaos's avatar
merlinofchaos committed
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
      '#suffix' => '</div>',
      '#theme' => 'links__contextual',
      '#links' => $links,
      '#attributes' => array('class' => array('contextual-links')),
      '#attached' => array(
        'library' => array(array('contextual', 'contextual-links')),
      ),
    );
    $vars['links'] = drupal_render($build);
  }
  $vars['theme_hook_suggestions'][] = 'views_ui_view_preview_section__' . $vars['section'];
}

/**
 * Returns the HTML for a section of a View being previewed within the Views UI.
 */
function theme_views_ui_view_preview_section($vars) {
  return '<h1 class="section-title">' . $vars['title'] . '</h1>'
  . $vars['links']
  . '<div class="preview-section">'. $vars['content'] . '</div>';
}

/**
 * Returns contextual links for each handler of a certain section.
 *
 * @TODO
 *   Bring in relationships
 *   Refactor this function to use much stuff of views_ui_edit_form_get_bucket.
 *
 * @param $title
 *   Add a bolded title of this section.
 */
489
function views_ui_view_preview_section_handler_links(ViewUI $view, $type, $title = FALSE) {
merlinofchaos's avatar
merlinofchaos committed
490
  $display = $view->display_handler->display;
491
  $handlers = $view->display_handler->getHandlers($type);
merlinofchaos's avatar
merlinofchaos committed
492 493
  $links = array();

494
  $types = ViewExecutable::viewsHandlerTypes();
merlinofchaos's avatar
merlinofchaos committed
495 496 497 498 499 500 501
  if ($title) {
    $links[$type . '-title'] = array(
      'title' => $types[$type]['title'],
    );
  }

  foreach ($handlers as $id => $handler) {
502
    $field_name = $handler->adminLabel(TRUE);
merlinofchaos's avatar
merlinofchaos committed
503 504
    $links[$type . '-edit-' . $id] = array(
      'title' => t('Edit @section', array('@section' => $field_name)),
505
      'href' => "admin/structure/views/nojs/config-item/{$view->storage->name}/{$display['id']}/$type/$id",
merlinofchaos's avatar
merlinofchaos committed
506 507 508 509 510
      'attributes' => array('class' => array('views-ajax-link')),
    );
  }
  $links[$type . '-add'] = array(
    'title' => t('Add new'),
511
    'href' => "admin/structure/views/nojs/add-item/{$view->storage->name}/{$display['id']}/$type",
merlinofchaos's avatar
merlinofchaos committed
512 513 514 515 516 517 518 519 520
    'attributes' => array('class' => array('views-ajax-link')),
  );

  return $links;
}

/**
 * Returns a link to editing a certain display setting.
 */
521
function views_ui_view_preview_section_display_category_links(ViewUI $view, $type, $title) {
merlinofchaos's avatar
merlinofchaos committed
522 523 524 525
  $display = $view->display_handler->display;
  $links = array(
    $type . '-edit' => array(
      'title' => t('Edit @section', array('@section' => $title)),
526
      'href' => "admin/structure/views/nojs/display/{$view->storage->name}/{$display['id']}/$type",
merlinofchaos's avatar
merlinofchaos committed
527 528 529 530 531 532 533 534 535 536
      'attributes' => array('class' => array('views-ajax-link')),
    ),
  );

  return $links;
}

/**
 * Returns all contextual links for the main content part of the view.
 */
537
function views_ui_view_preview_section_rows_links(ViewUI $view) {
merlinofchaos's avatar
merlinofchaos committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
  $display = $view->display_handler->display;
  $links = array();
  $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'filter', TRUE));
  $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'field', TRUE));
  $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'sort', TRUE));
  $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'argument', TRUE));
  $links = array_merge($links, views_ui_view_preview_section_handler_links($view, 'relationship', TRUE));

  return $links;
}

/**
 * Fetch metadata on a specific views ui wizard plugin.
 *
 * @param $wizard_type
 *   Name of a wizard, or name of a base table.
 *
 * @return
 *   An array with information about the requested wizard type.
 */
function views_ui_get_wizard($wizard_type) {
559
  $wizard = views_get_plugin_definition('wizard', $wizard_type);
merlinofchaos's avatar
merlinofchaos committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
  // @todo - handle this via an alter hook instead.
  if (!$wizard) {
    // Must be a base table using the default wizard plugin.
    $base_tables = views_fetch_base_tables();
    if (!empty($base_tables[$wizard_type])) {
      $wizard = views_ui_views_wizard_defaults();
      $wizard['base_table'] = $wizard_type;
      $wizard['title'] = $base_tables[$wizard_type]['title'];
    }
  }
  return $wizard;
}

/**
 * Fetch metadata for all content_type plugins.
 *
 * @return
 *   An array of arrays with information about all available views wizards.
 */
function views_ui_get_wizards() {
580
  $wizard_plugins = views_get_plugin_definitions('wizard');
merlinofchaos's avatar
merlinofchaos committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  $wizard_tables = array();
  foreach ($wizard_plugins as $name => $info) {
    $wizard_tables[$info['base_table']] = TRUE;
  }
  $base_tables = views_fetch_base_tables();
  $default_wizard = views_ui_views_wizard_defaults();
  // Find base tables with no wizard.
  // @todo - handle this via an alter hook for plugins?
  foreach ($base_tables as $table => $info) {
    if (!isset($wizard_tables[$table])) {
      $wizard = $default_wizard;
      $wizard['title'] = $info['title'];
      $wizard['base_table'] = $table;
      $wizard_plugins[$table] = $wizard;
    }
  }
  return $wizard_plugins;
}

/**
 * Helper function to define the default values for a Views wizard plugin.
 *
 * @return
 *   An array of defaults for a views wizard.
 */
function views_ui_views_wizard_defaults() {
  return array(
    // The children may, for example, be a different variant for each node type.
    'get children' => NULL,
    'get child' => NULL,
    // title and base table must be populated.  They are empty here just
    // so they are documented.
    'title' => '',
    'base_table' => NULL,
  );
}

function views_ui_get_form_wizard_instance($wizard) {
619
  return views_get_plugin('wizard', $wizard['name']);
merlinofchaos's avatar
merlinofchaos committed
620 621 622 623 624 625
}

/**
 * Implements hook_views_plugins_alter().
 */
function views_ui_views_plugins_alter(&$plugins) {
626
  // @todo This currently does not work, investigate annotation alters.
merlinofchaos's avatar
merlinofchaos committed
627
  // Attach contextual links to each display plugin. The links will point to
628
  // paths underneath "admin/structure/views/view/{$view->storage->name}" (i.e., paths
merlinofchaos's avatar
merlinofchaos committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
  // for editing and performing other contextual actions on the view).
  foreach ($plugins['display'] as &$display) {
    $display['contextual links']['views_ui'] = array(
      'parent path' => 'admin/structure/views/view',
      'argument properties' => array('name'),
    );
  }
}

/**
 * Implements hook_contextual_links_view_alter().
 */
function views_ui_contextual_links_view_alter(&$element, $items) {
  // Remove contextual links from being rendered, when so desired, such as
  // within a View preview.
  if (views_ui_contextual_links_suppress()) {
    $element['#links'] = array();
  }
  // Append the display ID to the Views UI edit links, so that clicking on the
  // contextual link takes you directly to the correct display tab on the edit
  // screen.
  elseif (!empty($element['#links']['views-ui-edit']) && !empty($element['#element']['#views_contextual_links_info']['views_ui']['view_display_id'])) {
    $display_id = $element['#element']['#views_contextual_links_info']['views_ui']['view_display_id'];
    $element['#links']['views-ui-edit']['href'] .= '/' . $display_id;
  }
}

/**
 * Sets a static variable for controlling whether contextual links are rendered.
 *
 * @see views_ui_contextual_links_view_alter()
 */
function views_ui_contextual_links_suppress($set = NULL) {
  $suppress = &drupal_static(__FUNCTION__);
  if (isset($set)) {
    $suppress = $set;
  }
  return $suppress;
}

/**
 * Increments the views_ui_contextual_links_suppress() static variable.
 *
 * When this function is added to the #pre_render of an element, and
 * 'views_ui_contextual_links_suppress_pop' is added to the #post_render of the
 * same element, then all contextual links within the element and its
 * descendants are suppressed from being rendered. This is used, for example,
 * during a View preview, when it is not desired for nodes in the Views result
 * to have contextual links.
 *
 * @see views_ui_contextual_links_suppress_pop()
 */
function views_ui_contextual_links_suppress_push() {
  views_ui_contextual_links_suppress(((int) views_ui_contextual_links_suppress())+1);
}

/**
 * Decrements the views_ui_contextual_links_suppress() static variable.
 *
 * @see views_ui_contextual_links_suppress_push()
 */
function views_ui_contextual_links_suppress_pop() {
  views_ui_contextual_links_suppress(((int) views_ui_contextual_links_suppress())-1);
}

/**
 * Menu callback; handles AJAX form submissions similar to ajax_form_callback(), but can be used for uncached forms.
 *
 * ajax_form_callback(), the menu callback for the system/ajax path, requires
 * the form to be retrievable from the form cache, because it lacks a trusted
 * $form_id argument with which to call drupal_retrieve_form(). When AJAX is
 * wanted on a non-cacheable form, #ajax['path'] can be set to a path whose
 * menu router item's 'page callback' is this function, and whose
 * 'page arguments' is the form id, optionally followed by additional build
 * arguments, as expected by drupal_get_form().
 *
 * The same caution must be used when defining a hook_menu() entry with this
 * page callback as is used when defining a hook_menu() entry with the
 * 'drupal_get_form' page callback: a 'page arguments' must be specified with a
 * literal value as the first argument, because $form_id determines which form
 * builder function gets called, so must be safe from user tampering.
 *
 * @see drupal_get_form()
 * @see ajax_form_callback()
 * @see http://drupal.org/node/774876
 */
function views_ui_ajax_get_form($form_id) {
  // @see ajax_get_form()
  $form_state = array(
    'no_redirect' => TRUE,
  );
  $form_state['rebuild_info']['copy']['#build_id'] = TRUE;
  $form_state['rebuild_info']['copy']['#action'] = TRUE;

  // @see drupal_get_form()
  $args = func_get_args();
  array_shift($args);
  $form_state['build_info']['args'] = $args;
  $form = drupal_build_form($form_id, $form_state);

  // @see ajax_form_callback()
  if (!empty($form_state['triggering_element'])) {
    $callback = $form_state['triggering_element']['#ajax']['callback'];
  }
  if (!empty($callback) && function_exists($callback)) {
    return $callback($form, $form_state);
  }
}

/**
 * This is part of a patch to address a jQueryUI bug.  The bug is responsible
 * for the inability to scroll a page when a modal dialog is active. If the content
 * of the dialog extends beyond the bottom of the viewport, the user is only able
 * to scroll with a mousewheel or up/down keyboard keys.
 *
 * @see http://bugs.jqueryui.com/ticket/4671
 * @see https://bugs.webkit.org/show_bug.cgi?id=19033
 * @see /js/jquery.ui.dialog.patch.js
 * @see /js/jquery.ui.dialog.min.js
 *
 * The javascript patch overwrites the $.ui.dialog.overlay.events object to remove
 * the mousedown, mouseup and click events from the list of events that are bound
 * in $.ui.dialog.overlay.create.
 */

function views_ui_library_alter(&$libraries, $module) {
755 756 757
  if ($module == 'system' && isset($libraries['jquery.ui.dialog'])) {
    if (version_compare($libraries['jquery.ui.dialog']['version'], '1.7.2', '>=')) {
      $libraries['jquery.ui.dialog']['js'][drupal_get_path('module', 'views') . '/js/jquery.ui.dialog.patch.js'] = array();
merlinofchaos's avatar
merlinofchaos committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    }
  }
}

/**
 * Truncate strings to a set length and provide a ... if they truncated.
 *
 * This is often used in the UI to ensure long strings fit.
 */
function views_ui_truncate($string, $length) {
  if (drupal_strlen($string) > $length) {
    $string = drupal_substr($string, 0, $length);
    $string .= '...';
  }

  return $string;
}
775 776 777 778

/**
 * Magic load function. Wrapper to load a view.
 */
779
function views_ui_load($name) {
780 781
  return views_get_view($name);
}
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

/**
 * Page callback: Calls a method on a view and reloads the listing page.
 *
 * @param Drupal\views\ViewExectuable $view
 *   The config entity being acted upon.
 * @param string $op
 *   The operation to perform, e.g., 'enable' or 'disable'.
 *
 * @return mixed
 *   Either returns the listing page as JSON, or calls drupal_goto() to
 *   redirect back to the listing page.
 */
function views_ui_ajax_callback(ViewExecutable $view, $op) {
  $controller = views_ui_entity_list_controller('view');
  // Perform the operation.
  $view->storage->$op();

  // If the request is via AJAX, return the rendered list as JSON.
  if (drupal_container()->get('request')->request->get('js')) {
    return $controller->renderListAJAX();
  }
  // Otherwise, redirect back to the page.
  else {
    drupal_goto($controller->getPath());
  }
}

/**
 * Page callback: Lists all of the views.
 *
 * @return array
 *   A render array for a page containing a list of views.
 *
 * @see views_ui_menu()
 */
function views_ui_list_page() {
  $controller = views_ui_entity_list_controller('view');
  return $controller->render();
}