/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $types = ViewExecutable::getHandlerTypes(); $view = $form_state->get('view'); $display =& $view->getExecutable()->displayHandlers->get($form_state->get('display_id')); $remember_groups = array(); if (!empty($view->form_cache)) { $old_fields = $view->form_cache['handlers']; } else { $old_fields = $display->getOption($types['filter']['plural']); } $groups = $form_state->getValue('filter_groups'); // Whatever button was clicked, re-calculate field information. $new_fields = $order = array(); // Make an array with the weights foreach ($form_state->getValue('filters') as $field => $info) { // add each value that is a field with a weight to our list, but only if // it has had its 'removed' checkbox checked. if (is_array($info) && empty($info['removed'])) { if (isset($info['weight'])) { $order[$field] = $info['weight']; } if (isset($info['group'])) { $old_fields[$field]['group'] = $info['group']; $remember_groups[$info['group']][] = $field; } } } // Sort the array asort($order); // Create a new list of fields in the new order. foreach (array_keys($order) as $field) { $new_fields[$field] = $old_fields[$field]; } // If the #group property is set on the clicked button, that means we are // either adding or removing a group, not actually updating the filters. $triggering_element = $form_state->getTriggeringElement(); if (!empty($triggering_element['#group'])) { if ($triggering_element['#group'] == 'add') { // Add a new group $groups['groups'][] = 'AND'; } else { // Renumber groups above the removed one down. foreach (array_keys($groups['groups']) as $group_id) { if ($group_id >= $triggering_element['#group']) { $old_group = $group_id + 1; if (isset($groups['groups'][$old_group])) { $groups['groups'][$group_id] = $groups['groups'][$old_group]; if (isset($remember_groups[$old_group])) { foreach ($remember_groups[$old_group] as $id) { $new_fields[$id]['group'] = $group_id; } } } else { // If this is the last one, just unset it. unset($groups['groups'][$group_id]); } } } } // Update our cache with values so that cancel still works the way // people expect. $view->form_cache = ['key' => 'rearrange-filter', 'groups' => $groups, 'handlers' => $new_fields]; // Return to this form except on actual Update. $view->addFormToStack('rearrange-filter', $form_state->get('display_id'), 'filter'); } else { // The actual update button was clicked. Remove the empty groups, and // renumber them sequentially. ksort($remember_groups); $groups['groups'] = static::arrayKeyPlus(array_values(array_intersect_key($groups['groups'], $remember_groups))); // Change the 'group' key on each field to match. Here, $mapping is an // array whose keys are the old group numbers and whose values are the new // (sequentially numbered) ones. $mapping = array_flip(static::arrayKeyPlus(array_keys($remember_groups))); foreach ($new_fields as &$new_field) { $new_field['group'] = $mapping[$new_field['group']]; } // Write the changed handler values. $display->setOption($types['filter']['plural'], $new_fields); $display->setOption('filter_groups', $groups); if (isset($view->form_cache)) { unset($view->form_cache); } } // Store in cache. $view->cacheSet(); }
/** * Tests the mapping of fields. * * @param \Drupal\views\ViewExecutable $view * The view to test. * * @return string * The view rendered as HTML. */ protected function mappedOutputHelper($view) { $output = $view->preview(); $rendered_output = \Drupal::service('renderer')->renderRoot($output); $this->storeViewPreview($rendered_output); $rows = $this->elements->body->div->div->div; $data_set = $this->dataSet(); $count = 0; foreach ($rows as $row) { $attributes = $row->attributes(); $class = (string) $attributes['class'][0]; $this->assertTrue(strpos($class, 'views-row-mapping-test') !== FALSE, 'Make sure that each row has the correct CSS class.'); foreach ($row->div as $field) { // Split up the field-level class, the first part is the mapping name // and the second is the field ID. $field_attributes = $field->attributes(); $name = strtok((string) $field_attributes['class'][0], '-'); $field_id = strtok('-'); // The expected result is the mapping name and the field value, // separated by ':'. $expected_result = $name . ':' . $data_set[$count][$field_id]; $actual_result = (string) $field; $this->assertIdentical($expected_result, $actual_result, format_string('The fields were mapped successfully: %name => %field_id', array('%name' => $name, '%field_id' => $field_id))); } $count++; } return $rendered_output; }
/** * Sets up the testing view with random field data. * * @param \Drupal\views\ViewExecutable $view * The view to add field data to. */ protected function prepareView(ViewExecutable $view) { $view->initDisplay(); foreach ($this->fieldStorages as $field_storage) { $field_name = $field_storage->getName(); $view->display_handler->options['fields'][$field_name]['id'] = $field_name; $view->display_handler->options['fields'][$field_name]['table'] = 'node__' . $field_name; $view->display_handler->options['fields'][$field_name]['field'] = $field_name; } }
/** * Returns whether the base table is of a translatable entity type. * * @return bool * TRUE if the base table is of a translatable entity type, FALSE otherwise. */ protected function isBaseTableTranslatable() { if ($entity_type = $this->view->getBaseEntityType()) { return $entity_type->isTranslatable(); } return FALSE; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $view = $form_state['view']; $display_id = $form_state['display_id']; $type = $form_state['type']; $id = $form_state['id']; $form = array('options' => array('#tree' => true, '#theme_wrappers' => array('container'), '#attributes' => array('class' => array('scroll'), 'data-drupal-views-scroll' => TRUE))); $executable = $view->getExecutable(); $executable->setDisplay($display_id); $item = $executable->getHandler($display_id, $type, $id); if ($item) { $handler = $executable->display_handler->getHandler($type, $id); if (empty($handler)) { $form['markup'] = array('#markup' => $this->t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field']))); } else { $handler->init($executable, $executable->display_handler, $item); $types = ViewExecutable::getHandlerTypes(); $form['#title'] = $this->t('Configure extra settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->adminLabel())); $form['#section'] = $display_id . '-' . $type . '-' . $id; // Get form from the handler. $handler->buildExtraOptionsForm($form['options'], $form_state); $form_state['handler'] = $handler; } $view->getStandardButtons($form, $form_state, 'views_ui_config_item_extra_form'); } return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $view = $form_state->get('view'); $display_id = $form_state->get('display_id'); $type = $form_state->get('type'); $id = $form_state->get('id'); $form = array('options' => array('#tree' => TRUE, '#theme_wrappers' => array('container'), '#attributes' => array('class' => array('scroll'), 'data-drupal-views-scroll' => TRUE))); $executable = $view->getExecutable(); if (!$executable->setDisplay($display_id)) { $form['markup'] = array('#markup' => $this->t('Invalid display id @display', array('@display' => $display_id))); return $form; } $executable->initQuery(); $item = $executable->getHandler($display_id, $type, $id); if ($item) { $handler = $executable->display_handler->getHandler($type, $id); if (empty($handler)) { $form['markup'] = array('#markup' => $this->t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field']))); } else { $handler->init($executable, $executable->display_handler, $item); $types = ViewExecutable::getHandlerTypes(); $form['#title'] = $this->t('Configure aggregation settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->adminLabel())); $handler->buildGroupByForm($form['options'], $form_state); $form_state->set('handler', $handler); } $view->getStandardButtons($form, $form_state, 'views_ui_config_item_group_form'); } return $form; }
protected function setUp() { parent::setUp(); // Ensure that the plugin definitions are cleared. foreach (ViewExecutable::getPluginTypes() as $plugin_type) { $this->container->get("plugin.manager.views.{$plugin_type}")->clearCachedDefinitions(); } ViewTestData::createTestViews(get_class($this), array('ds_test')); }
/** * Asserts a view's result & output cache items' cache tags. * * @param \Drupal\views\ViewExecutable $view * The view to test, must have caching enabled. * @param null|string[] $expected_results_cache * NULL when expecting no results cache item, a set of cache tags expected * to be set on the results cache item otherwise. * @param bool $views_caching_is_enabled * Whether to expect an output cache item. If TRUE, the cache tags must * match those in $expected_render_array_cache_tags. * @param string[] $expected_render_array_cache_tags * A set of cache tags expected to be set on the built view's render array. * * @return array * The render array */ protected function assertViewsCacheTags(ViewExecutable $view, $expected_results_cache, $views_caching_is_enabled, array $expected_render_array_cache_tags) { $build = $view->preview(); // Ensure the current request is a GET request so that render caching is // active for direct rendering of views, just like for actual requests. /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */ $request_stack = \Drupal::service('request_stack'); $request_stack->push(new Request()); \Drupal::service('renderer')->renderRoot($build); $request_stack->pop(); // Render array cache tags. $this->pass('Checking render array cache tags.'); sort($expected_render_array_cache_tags); $this->assertEqual($build['#cache']['tags'], $expected_render_array_cache_tags); if ($views_caching_is_enabled) { $this->pass('Checking Views results cache item cache tags.'); /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache_plugin */ $cache_plugin = $view->display_handler->getPlugin('cache'); // Results cache. $results_cache_item = \Drupal::cache('data')->get($cache_plugin->generateResultsKey()); if (is_array($expected_results_cache)) { $this->assertTrue($results_cache_item, 'Results cache item found.'); if ($results_cache_item) { sort($expected_results_cache); $this->assertEqual($results_cache_item->tags, $expected_results_cache); } } else { $this->assertFalse($results_cache_item, 'Results cache item not found.'); } // Output cache. $this->pass('Checking Views output cache item cache tags.'); $output_cache_item = \Drupal::cache('render')->get($cache_plugin->generateOutputKey()); if ($views_caching_is_enabled === TRUE) { $this->assertTrue($output_cache_item, 'Output cache item found.'); if ($output_cache_item) { $this->assertEqual($output_cache_item->tags, Cache::mergeTags($expected_render_array_cache_tags, ['rendered'])); } } else { $this->assertFalse($output_cache_item, 'Output cache item not found.'); } } $view->destroy(); return $build; }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->entityManager = $this->getMock('Drupal\\Core\\Entity\\EntityManagerInterface'); $this->entityStorage = $this->getMock('Drupal\\Core\\Entity\\EntityStorageInterface'); $this->entityViewBuilder = $this->getMock('Drupal\\Core\\Entity\\EntityViewBuilderInterface'); $this->executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')->disableOriginalConstructor()->getMock(); $this->display = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')->disableOriginalConstructor()->getMock(); $this->stylePlugin = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\style\\StylePluginBase')->disableOriginalConstructor()->getMock(); $this->executable->style_plugin = $this->stylePlugin; $this->entityHandler = new Entity(array(), 'entity', array('entity_type' => 'entity_test'), $this->entityManager); $this->display->expects($this->any())->method('getPlugin')->with('style')->willReturn($this->stylePlugin); $this->executable->expects($this->any())->method('getStyle')->willReturn($this->stylePlugin); $token = $this->getMockBuilder('Drupal\\Core\\Utility\\Token')->disableOriginalConstructor()->getMock(); $token->expects($this->any())->method('replace')->willReturnArgument(0); $container = new ContainerBuilder(); $container->set('token', $token); \Drupal::setContainer($container); }
/** * Tests the buildThemeFunctions() method. */ public function testBuildThemeFunctions() { $config = array('id' => 'test_view', 'tag' => 'OnE, TWO, and three', 'display' => array('default' => array('id' => 'default', 'display_plugin' => 'default', 'display_title' => 'Default'))); $storage = new View($config, 'view'); $user = $this->getMock('Drupal\\Core\\Session\\AccountInterface'); $view = new ViewExecutable($storage, $user); $expected = array('test_hook__test_view', 'test_hook'); $this->assertEquals($expected, $view->buildThemeFunctions('test_hook')); // Add a mock display. $display = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')->disableOriginalConstructor()->getMock(); $display->display = $config['display']['default']; $view->display_handler = $display; $expected = array('test_hook__test_view__default', 'test_hook__default', 'test_hook__one', 'test_hook__two', 'test_hook__and_three', 'test_hook__test_view', 'test_hook'); $this->assertEquals($expected, $view->buildThemeFunctions('test_hook')); //Change the name of the display plugin and make sure that is in the array. $view->display_handler->display['display_plugin'] = 'default2'; $expected = array('test_hook__test_view__default', 'test_hook__default', 'test_hook__one', 'test_hook__two', 'test_hook__and_three', 'test_hook__test_view__default2', 'test_hook__default2', 'test_hook__test_view', 'test_hook'); $this->assertEquals($expected, $view->buildThemeFunctions('test_hook')); }
/** * Tests the build method with a failed execution. * * @see \Drupal\views\Plugin\block\ViewsBlock::build() */ public function testBuildFailed() { $output = FALSE; $this->executable->expects($this->once())->method('buildRenderable')->with('block_1', [])->willReturn($output); $block_id = 'views_block:test_view-block_1'; $config = array(); $definition = array(); $definition['provider'] = 'views'; $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account); $this->assertEquals(array(), $plugin->build()); }
/** * Changes the tid filter to given term and depth. * * @param integer $tid * The term ID to filter on. * @param integer $depth * The depth to search. * @param array $expected * The expected views result. */ protected function assertTermWithDepthResult($tid, $depth, array $expected) { $this->view->destroy(); $this->view->initDisplay(); $filters = $this->view->displayHandlers->get('default')->getOption('filters'); $filters['tid_depth']['depth'] = $depth; $filters['tid_depth']['value'] = [$tid]; $this->view->displayHandlers->get('default')->setOption('filters', $filters); $this->executeView($this->view); $this->assertIdenticalResultsetHelper($this->view, $expected, ['nid' => 'nid'], 'assertIdentical'); }
/** * Tests most of the handlers. */ public function testHandlers() { $this->drupalCreateContentType(array('type' => 'article')); $this->addDefaultCommentField('node', 'article'); $object_types = array_keys(ViewExecutable::getHandlerTypes()); foreach ($this->container->get('views.views_data')->get() as $base_table => $info) { if (!isset($info['table']['base'])) { continue; } $view = entity_create('view', array('base_table' => $base_table)); $view = $view->getExecutable(); // @todo The groupwise relationship is currently broken. $exclude[] = 'taxonomy_term_field_data:tid_representative'; $exclude[] = 'users_field_data:uid_representative'; // Go through all fields and there through all handler types. foreach ($info as $field => $field_info) { // Table is a reserved key for the metainformation. if ($field != 'table' && !in_array("{$base_table}:{$field}", $exclude)) { $item = array('table' => $base_table, 'field' => $field); foreach ($object_types as $type) { if (isset($field_info[$type]['id'])) { $options = array(); if ($type == 'filter') { $handler = $this->container->get("plugin.manager.views.{$type}")->getHandler($item); // Set the value to use for the filter based on the filter type. if ($handler instanceof InOperator) { $options['value'] = array(1); } else { $options['value'] = 1; } } $view->addHandler('default', $type, $base_table, $field, $options); } } } } // Go through each step individually to see whether some parts are // failing. $view->build(); $view->preExecute(); $view->execute(); $view->render(); // Make sure all handlers extend the HandlerBase. foreach ($object_types as $type) { if (isset($view->{$type})) { foreach ($view->{$type} as $handler) { $this->assertTrue($handler instanceof HandlerBase, format_string('@type handler of class %class is an instance of HandlerBase', array('@type' => $type, '%class' => get_class($handler)))); } } } } }
/** * Tests the build method with a failed execution. * * @see \Drupal\views\Plugin\block\ViewsBlock::build() */ public function testBuildFailed() { $output = FALSE; $this->executable->expects($this->once())->method('executeDisplay')->with($this->equalTo('block_1'))->will($this->returnValue($output)); $block_id = 'views_block:test_view-block_1'; $config = array(); $definition = array(); $definition['provider'] = 'views'; $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account); $reflector = new \ReflectionClass($plugin); $property = $reflector->getProperty('conditionPluginManager'); $property->setAccessible(TRUE); $property->setValue($plugin, $this->getMock('Drupal\\Core\\Executable\\ExecutableManagerInterface')); $this->assertEquals(array(), $plugin->build()); }
/** * {@inheritdoc} */ protected function viewsData() { $data = parent::viewsData(); // Override the name handler to be able to call placeholder() from outside. $data['views_test_data']['name']['field']['id'] = 'test_field'; // Setup one field with an access callback and one with an access callback // and arguments. $data['views_test_data']['access_callback'] = $data['views_test_data']['id']; $data['views_test_data']['access_callback_arguments'] = $data['views_test_data']['id']; foreach (ViewExecutable::getHandlerTypes() as $type => $info) { if (isset($data['views_test_data']['access_callback'][$type]['id'])) { $data['views_test_data']['access_callback'][$type]['access callback'] = 'views_test_data_handler_test_access_callback'; $data['views_test_data']['access_callback_arguments'][$type]['access callback'] = 'views_test_data_handler_test_access_callback_argument'; $data['views_test_data']['access_callback_arguments'][$type]['access arguments'] = array(TRUE); } } return $data; }
/** * Converts Views block content to a renderable array with contextual links. * * @param string|array $output * An string|array representing the block. This will be modified to be a * renderable array, containing the optional '#contextual_links' property (if * there are any contextual links associated with the block). * @param string $block_type * The type of the block. If it's 'block' it's a regular views display, * but 'exposed_filter' exist as well. */ protected function addContextualLinks(&$output, $block_type = 'block') { // Do not add contextual links to an empty block. if (!empty($output)) { // Contextual links only work on blocks whose content is a renderable // array, so if the block contains a string of already-rendered markup, // convert it to an array. if (is_string($output)) { $output = array('#markup' => $output); } // views_add_contextual_links() needs the following information in // order to be attached to the view. $output['#view_id'] = $this->view->storage->id(); $output['#view_display_show_admin_links'] = $this->view->getShowAdminLinks(); $output['#view_display_plugin_id'] = $this->view->display_handler->getPluginId(); views_add_contextual_links($output, $block_type, $this->displayID); } }
/** * Lists all instances of fields on any views. * * @return array * The Views fields report page. */ public function reportFields() { $views = $this->entityManager()->getStorage('view')->loadMultiple(); // Fetch all fieldapi fields which are used in views // Therefore search in all views, displays and handler-types. $fields = array(); $handler_types = ViewExecutable::getHandlerTypes(); foreach ($views as $view) { $executable = $view->getExecutable(); $executable->initDisplay(); foreach ($executable->displayHandlers as $display_id => $display) { if ($executable->setDisplay($display_id)) { foreach ($handler_types as $type => $info) { foreach ($executable->getHandlers($type, $display_id) as $item) { $table_data = $this->viewsData->get($item['table']); if (isset($table_data[$item['field']]) && isset($table_data[$item['field']][$type]) && ($field_data = $table_data[$item['field']][$type])) { // The final check that we have a fieldapi field now. if (isset($field_data['field_name'])) { $fields[$field_data['field_name']][$view->id()] = $view->id(); } } } } } } } $header = array(t('Field name'), t('Used in')); $rows = array(); foreach ($fields as $field_name => $views) { $rows[$field_name]['data'][0]['data']['#plain_text'] = $field_name; foreach ($views as $view) { $rows[$field_name]['data'][1][] = $this->l($view, new Url('entity.view.edit_form', array('view' => $view))); } $item_list = ['#theme' => 'item_list', '#items' => $rows[$field_name]['data'][1], '#context' => ['list_style' => 'comma-list']]; $rows[$field_name]['data'][1] = ['data' => $item_list]; } // Sort rows by field name. ksort($rows); $output = array('#type' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => t('No fields have been used in views yet.')); return $output; }
/** * Lists all instances of fields on any views. * * @return array * The Views fields report page. */ public function reportFields() { $views = $this->entityManager()->getStorage('view')->loadMultiple(); // Fetch all fieldapi fields which are used in views // Therefore search in all views, displays and handler-types. $fields = array(); $handler_types = ViewExecutable::getHandlerTypes(); foreach ($views as $view) { $executable = $view->getExecutable(); $executable->initDisplay(); foreach ($executable->displayHandlers as $display_id => $display) { if ($executable->setDisplay($display_id)) { foreach ($handler_types as $type => $info) { foreach ($executable->getHandlers($type, $display_id) as $item) { $table_data = $this->viewsData->get($item['table']); if (isset($table_data[$item['field']]) && isset($table_data[$item['field']][$type]) && ($field_data = $table_data[$item['field']][$type])) { // The final check that we have a fieldapi field now. if (isset($field_data['field_name'])) { $fields[$field_data['field_name']][$view->id()] = $view->id(); } } } } } } } $header = array(t('Field name'), t('Used in')); $rows = array(); foreach ($fields as $field_name => $views) { $rows[$field_name]['data'][0] = String::checkPlain($field_name); foreach ($views as $view) { $rows[$field_name]['data'][1][] = $this->l($view, 'views_ui.edit', array('view' => $view)); } $rows[$field_name]['data'][1] = SafeMarkup::set(implode(', ', $rows[$field_name]['data'][1])); } // Sort rows by field name. ksort($rows); $output = array('#type' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => t('No fields have been used in views yet.')); return $output; }
/** * Generates a grid and asserts that it is displaying correctly. * * @param \Drupal\views\ViewExecutable $view * The executable to prepare. * @param string $alignment * The alignment of the grid to test. * @param int $columns * The number of columns in the grid to test. */ protected function assertGrid(ViewExecutable $view, $alignment, $columns) { $view->setDisplay('default'); $view->initStyle(); $view->initHandlers(); $view->initQuery(); $view->style_plugin->options['alignment'] = $alignment; $view->style_plugin->options['columns'] = $columns; $this->executeView($view); $output = $view->preview(); $output = drupal_render($output); $this->setRawContent($output); if (!in_array($alignment, $this->alignmentsTested)) { $result = $this->xpath('//div[contains(@class, "views-view-grid") and contains(@class, :alignment) and contains(@class, :columns)]', array(':alignment' => $alignment, ':columns' => 'cols-' . $columns)); $this->assertTrue(count($result), ucfirst($alignment) . " grid markup detected."); $this->alignmentsTested[] = $alignment; } $width = '0'; switch ($columns) { case 5: $width = '20'; break; case 4: $width = '25'; break; case 3: $width = '33.3333'; break; case 2: $width = '50'; break; case 1: $width = '100'; break; } // Ensure last column exists. $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns) and starts-with(@style, :width)]', array(':columns' => 'col-' . $columns, ':width' => 'width: ' . $width)); $this->assertTrue(count($result), ucfirst($alignment) . " {$columns} column grid: last column exists and automatic width calculated correctly."); // Ensure no extra columns were generated. $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns)]', array(':columns' => 'col-' . ($columns + 1))); $this->assertFalse(count($result), ucfirst($alignment) . " {$columns} column grid: no extraneous columns exist."); // Ensure tokens are being replaced in custom row/column classes. $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, "name-John")]'); $this->assertTrue(count($result), ucfirst($alignment) . " {$columns} column grid: Token replacement verified in custom column classes."); $result = $this->xpath('//div[contains(@class, "views-row") and contains(@class, "age-25")]'); $this->assertTrue(count($result), ucfirst($alignment) . " {$columns} column grid: Token replacement verified in custom row classes."); }
/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); // Set the default label to '' so the views internal title is used. $form['label']['#default_value'] = ''; $form['label']['#access'] = FALSE; // Unset the machine_name provided by BlockForm. unset($form['id']['#machine_name']['source']); // Prevent users from changing the auto-generated block machine_name. $form['id']['#access'] = FALSE; $form['#pre_render'][] = '\\Drupal\\views\\Plugin\\views\\PluginBase::preRenderAddFieldsetMarkup'; // Allow to override the label on the actual page. $form['views_label_checkbox'] = array('#type' => 'checkbox', '#title' => $this->t('Override title'), '#default_value' => !empty($this->configuration['views_label'])); $form['views_label_fieldset'] = array('#type' => 'fieldset', '#states' => array('visible' => array(array(':input[name="settings[views_label_checkbox]"]' => array('checked' => TRUE))))); $form['views_label'] = array('#title' => $this->t('Title'), '#type' => 'textfield', '#default_value' => $this->configuration['views_label'] ?: $this->view->getTitle(), '#states' => array('visible' => array(array(':input[name="settings[views_label_checkbox]"]' => array('checked' => TRUE)))), '#fieldset' => 'views_label_fieldset'); if ($this->view->storage->access('edit') && \Drupal::moduleHandler()->moduleExists('views_ui')) { $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore. (Try changing it directly in <a href="@url">@name</a>.)', array('@url' => \Drupal::url('entity.view.edit_display_form', array('view' => $this->view->storage->id(), 'display_id' => $this->displayID)), '@name' => $this->view->storage->label())); } else { $form['views_label']['#description'] = $this->t('Changing the title here means it cannot be dynamically altered anymore.'); } return $form; }
/** * Asserts a view's result & render cache items' cache tags. * * This method starts with a pre bubbling basic render array. * * @param \Drupal\views\ViewExecutable $view * The view. * @param string[] $expected_render_array_cache_tags * The expected render cache tags. * @param bool $views_caching_is_enabled * Defines whether views output / render caching is enabled. * * @return array * The render array. */ protected function assertViewsCacheTagsFromStaticRenderArray(ViewExecutable $view, array $expected_render_array_cache_tags, $views_caching_is_enabled) { $original = $build = DisplayPluginBase::buildBasicRenderable($view->id(), $view->current_display ?: 'default', $view->args); /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */ $render_cache = \Drupal::service('render_cache'); // Ensure the current request is a GET request so that render caching is // active for direct rendering of views, just like for actual requests. /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */ $request_stack = \Drupal::service('request_stack'); $request = new Request(); $request->server->set('REQUEST_TIME', REQUEST_TIME); $request_stack->push($request); $renderer->renderRoot($build); // Render array cache tags. $this->pass('Checking render array cache tags.'); sort($expected_render_array_cache_tags); $this->assertEqual($build['#cache']['tags'], $expected_render_array_cache_tags); $this->debugCacheTags($build['#cache']['tags'], $expected_render_array_cache_tags); $this->pass('Checking Views render cache item cache tags.'); $original['#cache'] += ['contexts' => []]; $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container->getParameter('renderer.config')['required_cache_contexts']); $render_cache_item = $render_cache->get($original); if ($views_caching_is_enabled) { $this->assertTrue(!empty($render_cache_item), 'Render cache item found.'); if ($render_cache_item) { $this->assertEqual($render_cache_item['#cache']['tags'], $expected_render_array_cache_tags); $this->debugCacheTags($render_cache_item['#cache']['tags'], $expected_render_array_cache_tags); } } else { $this->assertFalse($render_cache_item, 'Render cache item not found.'); } return $build; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $view = $form_state->get('view'); $display_id = $form_state->get('display_id'); $type = $form_state->get('type'); $form = array('options' => array('#theme_wrappers' => array('container'), '#attributes' => array('class' => array('scroll'), 'data-drupal-views-scroll' => TRUE))); $executable = $view->getExecutable(); $executable->setDisplay($display_id); $display =& $executable->displayHandlers->get($display_id); $types = ViewExecutable::getHandlerTypes(); $ltitle = $types[$type]['ltitle']; $section = $types[$type]['plural']; if (!empty($types[$type]['type'])) { $type = $types[$type]['type']; } $form['#title'] = $this->t('Add @type', array('@type' => $ltitle)); $form['#section'] = $display_id . 'add-handler'; // Add the display override dropdown. views_ui_standard_display_dropdown($form, $form_state, $section); // Figure out all the base tables allowed based upon what the relationships provide. $base_tables = $executable->getBaseTables(); $options = Views::viewsDataHelper()->fetchFields(array_keys($base_tables), $type, $display->useGroupBy(), $form_state->get('type')); if (!empty($options)) { $form['override']['controls'] = array('#theme_wrappers' => array('container'), '#id' => 'views-filterable-options-controls', '#attributes' => array('class' => array('container-inline'))); $form['override']['controls']['options_search'] = array('#type' => 'textfield', '#title' => $this->t('Search')); $groups = array('all' => $this->t('- All -')); $form['override']['controls']['group'] = array('#type' => 'select', '#title' => $this->t('Type'), '#options' => array()); $form['options']['name'] = array('#prefix' => '<div class="views-radio-box form-checkboxes views-filterable-options">', '#suffix' => '</div>', '#tree' => TRUE, '#default_value' => 'all'); // Group options first to simplify the usage of #states. $grouped_options = array(); foreach ($options as $key => $option) { $group = preg_replace('/[^a-z0-9]/', '-', strtolower($option['group'])); $groups[$group] = $option['group']; $grouped_options[$group][$key] = $option; if (!empty($option['aliases']) && is_array($option['aliases'])) { foreach ($option['aliases'] as $id => $alias) { if (empty($alias['base']) || !empty($base_tables[$alias['base']])) { $copy = $option; $copy['group'] = $alias['group']; $copy['title'] = $alias['title']; if (isset($alias['help'])) { $copy['help'] = $alias['help']; } $group = preg_replace('/[^a-z0-9]/', '-', strtolower($copy['group'])); $groups[$group] = $copy['group']; $grouped_options[$group][$key . '$' . $id] = $copy; } } } } foreach ($grouped_options as $group => $group_options) { foreach ($group_options as $key => $option) { $form['options']['name'][$key] = array('#type' => 'checkbox', '#title' => $this->t('!group: !field', array('!group' => $option['group'], '!field' => $option['title'])), '#description' => $option['help'], '#return_value' => $key, '#prefix' => "<div class='filterable-option'>", '#suffix' => '</div>', '#states' => array('visible' => array(array(':input[name="override[controls][group]"]' => array('value' => 'all')), array(':input[name="override[controls][group]"]' => array('value' => $group))))); } } $form['override']['controls']['group']['#options'] = $groups; } else { $form['options']['markup'] = array('#markup' => '<div class="form-item">' . $this->t('There are no @types available to add.', array('@types' => $ltitle)) . '</div>'); } // Add a div to show the selected items $form['selected'] = array('#type' => 'item', '#markup' => '<span class="views-ui-view-title">' . $this->t('Selected:') . '</span> ' . '<div class="views-selected-options"></div>', '#theme_wrappers' => array('form_element', 'views_ui_container'), '#attributes' => array('class' => array('container-inline', 'views-add-form-selected'), 'data-drupal-views-offset' => 'bottom')); $view->getStandardButtons($form, $form_state, 'views_ui_add_handler_form', $this->t('Add and configure @types', array('@types' => $ltitle))); // Remove the default submit function. $form['actions']['submit']['#submit'] = array_filter($form['actions']['submit']['#submit'], function ($var) { return !(is_array($var) && isset($var[1]) && $var[1] == 'standardSubmit'); }); $form['actions']['submit']['#submit'][] = array($view, 'submitItemAdd'); return $form; }
/** * Merges handlers default values. * * @param string $type * The name of the handler type option. */ protected function mergeHandler($type) { $types = ViewExecutable::getHandlerTypes(); $options = $this->getOption($types[$type]['plural']); foreach ($this->getHandlers($type) as $id => $handler) { if (isset($options[$id])) { $options[$id] = $options[$id] + $handler->options; } } $this->setOption($types[$type]['plural'], $options); }
/** * A submit handler that is used for storing temporary items when using * multi-step changes, such as ajax requests. */ public function submitTemporaryForm($form, FormStateInterface $form_state) { // Run it through the handler's submit function. $this->submitOptionsForm($form['options'], $form_state); $item = $this->options; $types = ViewExecutable::getHandlerTypes(); // For footer/header $handler_type is area but $type is footer/header. // For all other handle types it's the same. $handler_type = $type = $form_state->get('type'); if (!empty($types[$type]['type'])) { $handler_type = $types[$type]['type']; } $override = NULL; $view = $form_state->get('view'); $executable = $view->getExecutable(); if ($executable->display_handler->useGroupBy() && !empty($item['group_type'])) { if (empty($executable->query)) { $executable->initQuery(); } $aggregate = $executable->query->getAggregationInfo(); if (!empty($aggregate[$item['group_type']]['handler'][$type])) { $override = $aggregate[$item['group_type']]['handler'][$type]; } } // Create a new handler and unpack the options from the form onto it. We // can use that for storage. $handler = Views::handlerManager($handler_type)->getHandler($item, $override); $handler->init($executable, $executable->display_handler, $item); // Add the incoming options to existing options because items using // the extra form may not have everything in the form here. $options = $form_state->getValue('options') + $this->options; // This unpacks only options that are in the definition, ensuring random // extra stuff on the form is not sent through. $handler->unpackOptions($handler->options, $options, NULL, FALSE); // Store the item back on the view. $executable = $view->getExecutable(); $executable->temporary_options[$type][$form_state->get('id')] = $handler->options; // @todo Decide if \Drupal\views_ui\Form\Ajax\ViewsFormBase::getForm() is // perhaps the better place to fix the issue. // \Drupal\views_ui\Form\Ajax\ViewsFormBase::getForm() drops the current // form from the stack, even if it's an #ajax. So add the item back to the top // of the stack. $view->addFormToStack($form_state->get('form_key'), $form_state->get('display_id'), $type, $item['id'], TRUE); $form_state->get('rerender', TRUE); $form_state->setRebuild(); // Write to cache $view->cacheSet(); }
/** * Builds the necessary info to execute the query. */ public function build(ViewExecutable $view) { // Make the query distinct if the option was set. if (!empty($this->options['distinct'])) { $this->setDistinct(TRUE); } // Store the view in the object to be able to use it later. $this->view = $view; $view->initPager(); // Let the pager modify the query to add limits. $view->pager->query(); $view->build_info['query'] = $this->query(); $view->build_info['count_query'] = $this->query(TRUE); }
/** * Gets the render cache for a given view. * * @param \Drupal\views\ViewExecutable $view * The view. * * @return array|FALSE * The render cache result or FALSE if not existent. */ protected function getRenderCache(ViewExecutable $view) { /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */ $render_cache = \Drupal::service('render_cache'); $view->element = ['#cache' => []]; $build = $view->buildRenderable(); $build['#cache']['contexts'] = Cache::mergeContexts($build['#cache']['contexts'], $this->container->getParameter('renderer.config')['required_cache_contexts']); return $render_cache->get($build); }
/** * Tests the plugins list is correct. */ public function testPluginList() { $plugin_list = array('access', 'area', 'argument', 'argument_default', 'argument_validator', 'cache', 'display_extender', 'display', 'exposed_form', 'field', 'filter', 'join', 'pager', 'query', 'relationship', 'row', 'sort', 'style', 'wizard'); $diff = array_diff($plugin_list, ViewExecutable::getPluginTypes()); $this->assertTrue(empty($diff), 'The plugin list is correct'); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, ViewExecutable $view = NULL, $output = []) { if (!($step = $form_state->get('step'))) { $step = 'views_form_views_form'; $form_state->set('step', $step); } $form_state->set(['step_controller', 'views_form_views_form'], 'Drupal\\views\\Form\\ViewsFormMainForm'); // Cache the built form to prevent it from being rebuilt prior to validation // and submission, which could lead to data being processed incorrectly, // because the views rows (and thus, the form elements as well) have changed // in the meantime. $form_state->setCached(); $form = array(); $query = $this->requestStack->getCurrentRequest()->query->all(); $query = UrlHelper::filterQueryParameters($query, array(), ''); $options = array('query' => $query); $form['#action'] = $view->hasUrl() ? $view->getUrl()->setOptions($options)->toString() : Url::fromRoute('<current>')->setOptions($options)->toString(); // Tell the preprocessor whether it should hide the header, footer, pager... $form['show_view_elements'] = array('#type' => 'value', '#value' => $step == 'views_form_views_form' ? TRUE : FALSE); $form_object = $this->getFormObject($form_state); $form += $form_object->buildForm($form, $form_state, $view, $output); return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { // Run it through the handler's submit function. $form_state['handler']->submitOptionsForm($form['options'], $form_state); $item = $form_state['handler']->options; $types = ViewExecutable::getHandlerTypes(); // For footer/header $handler_type is area but $type is footer/header. // For all other handle types it's the same. $handler_type = $type = $form_state['type']; if (!empty($types[$type]['type'])) { $handler_type = $types[$type]['type']; } $override = NULL; $executable = $form_state['view']->getExecutable(); if ($executable->display_handler->useGroupBy() && !empty($item['group_type'])) { if (empty($executable->query)) { $executable->initQuery(); } $aggregate = $executable->query->getAggregationInfo(); if (!empty($aggregate[$item['group_type']]['handler'][$type])) { $override = $aggregate[$item['group_type']]['handler'][$type]; } } // Create a new handler and unpack the options from the form onto it. We // can use that for storage. $handler = Views::handlerManager($handler_type)->getHandler($item, $override); $handler->init($executable, $executable->display_handler, $item); // Add the incoming options to existing options because items using // the extra form may not have everything in the form here. $options = $form_state['values']['options'] + $form_state['handler']->options; // This unpacks only options that are in the definition, ensuring random // extra stuff on the form is not sent through. $handler->unpackOptions($handler->options, $options, NULL, FALSE); // Add any dependencies as the handler is saved. Put it here so // it does not need to be declared in defineOptions(). if ($dependencies = $handler->getDependencies()) { $handler->options['dependencies'] = $dependencies; } // Add the module providing the handler as a dependency as well. $handler->options['dependencies']['module'][] = $handler->definition['provider']; // Store the item back on the view $executable->setHandler($form_state['display_id'], $form_state['type'], $form_state['id'], $handler->options); // Ensure any temporary options are removed. if (isset($form_state['view']->temporary_options[$type][$form_state['id']])) { unset($form_state['view']->temporary_options[$type][$form_state['id']]); } // Write to cache $form_state['view']->cacheSet(); }
/** * Submit handler for adding new item(s) to a view. */ public function submitItemAdd($form, FormStateInterface $form_state) { $type = $form_state->get('type'); $types = ViewExecutable::getHandlerTypes(); $section = $types[$type]['plural']; $display_id = $form_state->get('display_id'); // Handle the override select. list($was_defaulted, $is_defaulted) = $this->getOverrideValues($form, $form_state); if ($was_defaulted && !$is_defaulted) { // We were using the default display's values, but we're now overriding // the default display and saving values specific to this display. $display =& $this->getExecutable()->displayHandlers->get($display_id); // setOverride toggles the override of this section. $display->setOverride($section); } elseif (!$was_defaulted && $is_defaulted) { // We used to have an override for this display, but the user now wants // to go back to the default display. // Overwrite the default display with the current form values, and make // the current display use the new default values. $display =& $this->getExecutable()->displayHandlers->get($display_id); // optionsOverride toggles the override of this section. $display->setOverride($section); } if (!$form_state->isValueEmpty('name') && is_array($form_state->getValue('name'))) { // Loop through each of the items that were checked and add them to the view. foreach (array_keys(array_filter($form_state->getValue('name'))) as $field) { list($table, $field) = explode('.', $field, 2); if ($cut = strpos($field, '$')) { $field = substr($field, 0, $cut); } $id = $this->getExecutable()->addHandler($display_id, $type, $table, $field); // check to see if we have group by settings $key = $type; // Footer,header and empty text have a different internal handler type(area). if (isset($types[$type]['type'])) { $key = $types[$type]['type']; } $item = array('table' => $table, 'field' => $field); $handler = Views::handlerManager($key)->getHandler($item); if ($this->getExecutable()->displayHandlers->get('default')->useGroupBy() && $handler->usesGroupBy()) { $this->addFormToStack('handler-group', $display_id, $type, $id); } // check to see if this type has settings, if so add the settings form first if ($handler && $handler->hasExtraOptions()) { $this->addFormToStack('handler-extra', $display_id, $type, $id); } // Then add the form to the stack $this->addFormToStack('handler', $display_id, $type, $id); } } if (isset($this->form_cache)) { unset($this->form_cache); } // Store in cache $this->cacheSet(); }