Commit dea7af3f authored by RoloDMonkey's avatar RoloDMonkey Committed by pifagor

Issue #2159207 by RoloDMonkey, capogeannis, DamienMcKenna, StephanieFuda, Dave...

Issue #2159207 by RoloDMonkey, capogeannis, DamienMcKenna, StephanieFuda, Dave Reid, klausi, patbranch, bernig, ericwenger, pifagor, wjackson, chegor: Provide XML Sitemap fields to views
parent dfe03af4
<?php
/**
* @file
* Functional tests for providing xmlsitemap fields to views.
*/
class XmlsitemapViewsFieldsTest extends DrupalWebTestCase {
/**
* Information about these tests, for the testing UX.
*
* @return array
* The information.
*/
public static function getInfo() {
return array(
'name' => 'XML sitemap views fields',
'description' => 'Functional tests for providing xmlsitemap fields to views.',
'group' => 'XML sitemap',
'dependencies' => array('views'),
);
}
/**
* Setup before any tests are run.
*
* @see DrupalWebTestCase::setUp()
*/
protected function setUp($modules = array()) {
$modules[] = 'views';
$modules[] = 'xmlsitemap_node';
parent::setUp($modules);
}
/**
* Test that views can show XML sitemap settings on nodes.
*/
public function testNodeSettings() {
// Create a node.
$node = $this->drupalCreateNode();
// Create view with xmlsitemap fields.
$view = $this->nodeView();
$view->save();
// Load view, check that xmlsitemap fields are not overridden.
$this->drupalGet('xmlsitemap-node-fields');
$this->assertRaw('<span class="xmlsitemap-priority">0.5</span>');
$this->assertRaw('<span class="xmlsitemap-priority-override">No</span>');
$this->assertRaw('<span class="xmlsitemap-inclusion">No</span>');
$this->assertRaw('<span class="xmlsitemap-inclusion-override">No</span>');
// Override xmlsitemap fields for node.
$node->xmlsitemap['priority'] = 0.6;
$node->xmlsitemap['priority_override'] = 1;
$node->xmlsitemap['status'] = 1;
$node->xmlsitemap['status_override'] = 1;
node_save($node);
// Re-load view, check that xmlsitemap fields are overridden.
$this->drupalGet('xmlsitemap-node-fields');
$this->assertRaw('<span class="xmlsitemap-priority">0.6</span>');
$this->assertRaw('<span class="xmlsitemap-priority-override">Yes</span>');
$this->assertRaw('<span class="xmlsitemap-inclusion">Yes</span>');
$this->assertRaw('<span class="xmlsitemap-inclusion-override">Yes</span>');
}
/**
* An exported view.
*
* @return view
* A view of nodes that displays the XML sitemap fields.
*/
private function nodeView() {
$view = new view();
$view->name = 'xmlsitemap_views_fields_nodes';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'XML sitemap node fields';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'XML sitemap node fields';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['style_options']['default_row_class'] = FALSE;
$handler->display->display_options['style_options']['row_class_special'] = FALSE;
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
$handler->display->display_options['fields']['title']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['title']['link_to_node'] = FALSE;
/* Field: XML sitemap: Inclusion */
$handler->display->display_options['fields']['status']['id'] = 'status';
$handler->display->display_options['fields']['status']['table'] = 'xmlsitemap_node';
$handler->display->display_options['fields']['status']['field'] = 'status';
$handler->display->display_options['fields']['status']['element_type'] = 'span';
$handler->display->display_options['fields']['status']['element_class'] = 'xmlsitemap-inclusion';
$handler->display->display_options['fields']['status']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['status']['not'] = 0;
/* Field: XML sitemap: Inclusion override */
$handler->display->display_options['fields']['status_override']['id'] = 'status_override';
$handler->display->display_options['fields']['status_override']['table'] = 'xmlsitemap_node';
$handler->display->display_options['fields']['status_override']['field'] = 'status_override';
$handler->display->display_options['fields']['status_override']['element_type'] = 'span';
$handler->display->display_options['fields']['status_override']['element_class'] = 'xmlsitemap-inclusion-override';
$handler->display->display_options['fields']['status_override']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['status_override']['not'] = 0;
/* Field: XML sitemap: Priority */
$handler->display->display_options['fields']['priority']['id'] = 'priority';
$handler->display->display_options['fields']['priority']['table'] = 'xmlsitemap_node';
$handler->display->display_options['fields']['priority']['field'] = 'priority';
$handler->display->display_options['fields']['priority']['element_type'] = 'span';
$handler->display->display_options['fields']['priority']['element_class'] = 'xmlsitemap-priority';
$handler->display->display_options['fields']['priority']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['priority']['separator'] = '';
/* Field: XML sitemap: Priority override */
$handler->display->display_options['fields']['priority_override']['id'] = 'priority_override';
$handler->display->display_options['fields']['priority_override']['table'] = 'xmlsitemap_node';
$handler->display->display_options['fields']['priority_override']['field'] = 'priority_override';
$handler->display->display_options['fields']['priority_override']['element_type'] = 'span';
$handler->display->display_options['fields']['priority_override']['element_class'] = 'xmlsitemap-priority-override';
$handler->display->display_options['fields']['priority_override']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['priority_override']['not'] = 0;
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'xmlsitemap-node-fields';
return $view;
}
}
......@@ -4,6 +4,8 @@ package = XML sitemap
core = 7.x
files[] = xmlsitemap.xmlsitemap.inc
files[] = xmlsitemap.test
files[] = tests/xmlsitemap_views_fields.test
recommends[] = robotstxt
configure = admin/config/search/xmlsitemap
test_dependencies[] = robotstxt:robotstxt
test_dependencies[] = views:views
......@@ -292,6 +292,16 @@ function xmlsitemap_robotstxt() {
}
}
/**
* Implements hook_views_api().
*/
function xmlsitemap_views_api() {
return array(
'api' => 3,
);
}
/**
* Internal default variables for xmlsitemap_var().
*/
......
<?php
/**
* @file
* Provides information about the data being made available for views.
*/
/**
* Implements hook_views_data().
*
* Uses the xmlsitemap functions to load information about entity types that
* are configured for xmlsitemap. Creates separate table aliases so that views
* can display, filter and sort each entity type.
*/
function xmlsitemap_views_data() {
$data = array();
$link_info = xmlsitemap_get_link_info();
foreach ($link_info as $info) {
if (!empty($info['fieldable']) && $info['fieldable']) {
$table_alias = 'xmlsitemap_' . $info['base table'];
$data[$table_alias]['table'] = _xmlsitemap_table_data($info);
$field_data = _xmlsitemap_field_data();
foreach ($field_data as $field_name => $field) {
$data[$table_alias][$field_name] = $field;
}
}
}
return $data;
}
/**
* Table information.
*
* @param array $info
* Information about how the entity type is represented by xmlsitemap.
*
* @return array
* An array of table data based on the information about the entity type.
* Most importantly it returns join data for the table alias, which is
* different each time.
*/
function _xmlsitemap_table_data(array $info) {
$table_data = array();
$table_data['group'] = t('XML sitemap');
// Gets the base table (node, user, etc.) from $info.
$table_data['join'][$info['base table']] = array(
'table' => 'xmlsitemap',
'field' => 'id',
// Gets the field name (nid, uid, etc.) from $info.
'left_field' => $info['entity keys']['id'],
'extra' => array(
array(
'field' => 'type',
// Also filters on the entity type (node, user, etc.),
// not to be confused with the base table name above.
'value' => $info['type'],
'operator' => '=',
),
),
);
return $table_data;
}
/**
* Field information.
*
* @return array
* Returns the field information for four fields (status, status override,
* priority, priority override) that can be shown in views. These are always
* the same no matter what entity type the view is showing.
*/
function _xmlsitemap_field_data() {
$field_data = array();
$field_data['status'] = array(
'title' => t('Inclusion'),
'help' => t('Is this included in the XML sitemap?'),
'field' => array(
'handler' => 'views_handler_field_boolean',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_boolean_operator',
),
);
$field_data['status_override'] = array(
'title' => t('Inclusion override'),
'help' => t('Was the XML sitemap inclusion overridden, or left as the default?'),
'field' => array(
'handler' => 'views_handler_field_boolean',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_boolean_operator',
),
);
$field_data['priority'] = array(
'title' => t('Priority'),
'help' => t('The XML sitemap priority.'),
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
$field_data['priority_override'] = array(
'title' => t('Priority override'),
'help' => t('Was the XML sitemap priority overridden, or left as the default?'),
'field' => array(
'handler' => 'views_handler_field_boolean',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_boolean_operator',
),
);
return $field_data;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment