/** * Tests that views cacheability metadata updated properly. */ public function testUpdateHookN() { $this->runUpdates(); foreach (Views::getAllViews() as $view) { $displays = $view->get('display'); foreach (array_keys($displays) as $display_id) { $display = $view->getDisplay($display_id); $this->assertFalse(isset($display['cache_metadata']['cacheable'])); $this->assertTrue(isset($display['cache_metadata']['contexts'])); $this->assertTrue(isset($display['cache_metadata']['max-age'])); $this->assertTrue(isset($display['cache_metadata']['tags'])); } } }
/** * {@inheritdoc} */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); // Get the sorts that apply to our base. $sorts = Views::viewsDataHelper()->fetchFields($this->definition['base'], 'sort'); $sort_options = array(); foreach ($sorts as $sort_id => $sort) { $sort_options[$sort_id] = "{$sort['group']}: {$sort['title']}"; } $base_table_data = Views::viewsData()->get($this->definition['base']); // Extends the relationship's basic options, allowing the user to pick a // sort and an order for it. $form['subquery_sort'] = array('#type' => 'select', '#title' => $this->t('Representative sort criteria'), '#default_value' => !empty($this->options['subquery_sort']) ? $this->options['subquery_sort'] : $this->definition['base'] . '.' . $base_table_data['table']['base']['field'], '#options' => $sort_options, '#description' => $this->t("The sort criteria is applied to the data brought in by the relationship to determine how a representative item is obtained for each row. For example, to show the most recent node for each user, pick 'Content: Updated date'.")); $form['subquery_order'] = array('#type' => 'radios', '#title' => $this->t('Representative sort order'), '#description' => $this->t("The ordering to use for the sort criteria selected above."), '#options' => array('ASC' => $this->t('Ascending'), 'DESC' => $this->t('Descending')), '#default_value' => $this->options['subquery_order']); $form['subquery_namespace'] = array('#type' => 'textfield', '#title' => $this->t('Subquery namespace'), '#description' => $this->t('Advanced. Enter a namespace for the subquery used by this relationship.'), '#default_value' => $this->options['subquery_namespace']); // WIP: This stuff doesn't work yet: namespacing issues. // A list of suitable views to pick one as the subview. $views = array('' => '- None -'); foreach (Views::getAllViews() as $view) { // Only get views that are suitable: // - base must the base that our relationship joins towards // - must have fields. if ($view->get('base_table') == $this->definition['base'] && !empty($view->getDisplay('default')['display_options']['fields'])) { // TODO: check the field is the correct sort? // or let users hang themselves at this stage and check later? $views[$view->id()] = $view->id(); } } $form['subquery_view'] = array('#type' => 'select', '#title' => $this->t('Representative view'), '#default_value' => $this->options['subquery_view'], '#options' => $views, '#description' => $this->t('Advanced. Use another view to generate the relationship subquery. This allows you to use filtering and more than one sort. If you pick a view here, the sort options above are ignored. Your view must have the ID of its base as its only field, and should have some kind of sorting.')); $form['subquery_regenerate'] = array('#type' => 'checkbox', '#title' => $this->t('Generate subquery each time view is run'), '#default_value' => $this->options['subquery_regenerate'], '#description' => $this->t('Will re-generate the subquery for this relationship every time the view is run, instead of only when these options are saved. Use for testing if you are making changes elsewhere. WARNING: seriously impairs performance.')); }
/** * Tests the load wrapper/helper functions. */ public function testLoadFunctions() { $this->enableModules(array('field', 'text', 'node')); $this->installConfig(array('node')); $storage = $this->container->get('entity.manager')->getStorage('view'); // Test views_view_is_enabled/disabled. $archive = $storage->load('archive'); $this->assertTrue(views_view_is_disabled($archive), 'views_view_is_disabled works as expected.'); // Enable the view and check this. $archive->enable(); $this->assertTrue(views_view_is_enabled($archive), ' views_view_is_enabled works as expected.'); // We can store this now, as we have enabled/disabled above. $all_views = $storage->loadMultiple(); // Test Views::getAllViews(). $this->assertIdentical(array_keys($all_views), array_keys(Views::getAllViews()), 'Views::getAllViews works as expected.'); // Test Views::getEnabledViews(). $expected_enabled = array_filter($all_views, function ($view) { return views_view_is_enabled($view); }); $this->assertIdentical(array_keys($expected_enabled), array_keys(Views::getEnabledViews()), 'Expected enabled views returned.'); // Test Views::getDisabledViews(). $expected_disabled = array_filter($all_views, function ($view) { return views_view_is_disabled($view); }); $this->assertIdentical(array_keys($expected_disabled), array_keys(Views::getDisabledViews()), 'Expected disabled views returned.'); // Test Views::getViewsAsOptions(). // Test the $views_only parameter. $this->assertIdentical(array_keys($all_views), array_keys(Views::getViewsAsOptions(TRUE)), 'Expected option keys for all views were returned.'); $expected_options = array(); foreach ($all_views as $id => $view) { $expected_options[$id] = $view->label(); } $this->assertIdentical($expected_options, $this->castSafeStrings(Views::getViewsAsOptions(TRUE)), 'Expected options array was returned.'); // Test the default. $this->assertIdentical($this->formatViewOptions($all_views), $this->castSafeStrings(Views::getViewsAsOptions()), 'Expected options array for all views was returned.'); // Test enabled views. $this->assertIdentical($this->formatViewOptions($expected_enabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'enabled')), 'Expected enabled options array was returned.'); // Test disabled views. $this->assertIdentical($this->formatViewOptions($expected_disabled), $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'disabled')), 'Expected disabled options array was returned.'); // Test the sort parameter. $all_views_sorted = $all_views; ksort($all_views_sorted); $this->assertIdentical(array_keys($all_views_sorted), array_keys(Views::getViewsAsOptions(TRUE, 'all', NULL, FALSE, TRUE)), 'All view id keys returned in expected sort order'); // Test $exclude_view parameter. $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive')), 'View excluded from options based on name'); $this->assertFalse(array_key_exists('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default')), 'View display excluded from options based on name'); $this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', $archive->getExecutable())), 'View excluded from options based on object'); // Test the $opt_group parameter. $expected_opt_groups = array(); foreach ($all_views as $view) { foreach ($view->get('display') as $display) { $expected_opt_groups[$view->id()][$view->id() . ':' . $display['id']] = (string) t('@view : @display', array('@view' => $view->id(), '@display' => $display['id'])); } } $this->assertIdentical($expected_opt_groups, $this->castSafeStrings(Views::getViewsAsOptions(FALSE, 'all', NULL, TRUE)), 'Expected option array for an option group returned.'); }
/** * Helper to get display ids for a particular View */ protected function getViewDisplayIds($entity_id) { $views = \Drupal\views\Views::getAllViews(); $options = array(); foreach ($views as $view) { if ($view->get('id') == $entity_id) { foreach ($view->get('display') as $display) { $options[$display['id']] = $display['display_title']; } } } return $options; }