Commit bfdc741e authored by alexpott's avatar alexpott

Issue #2458601 by Denchev, Berdir: statistics library is not loaded if node...

Issue #2458601 by Denchev, Berdir: statistics library is not loaded if node template doesn't print it
parent 42da7fa4
<?php
/**
* @file
* Contains \Drupal\statistics\Tests\StatisticsAttachedTest.
*/
namespace Drupal\statistics\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\node\Entity\Node;
/**
* Tests if statistics.js is loaded when content is not printed.
*
* @group statistics
*/
class StatisticsAttachedTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'statistics');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(['type' => 'page']);
// Install "statistics_test_attached" and set it as the default theme.
$theme = 'statistics_test_attached';
\Drupal::service('theme_handler')->install(array($theme));
$this->config('system.theme')
->set('default', $theme)
->save();
// Installing a theme will cause the kernel terminate event to rebuild the
// router. Simulate that here.
\Drupal::service('router.builder')->rebuildIfNeeded();
}
/**
* Tests if statistics.js is loaded when content is not printed.
*/
public function testAttached() {
$node = Node::create([
'type' => 'page',
'title' => 'Page node',
'body' => 'body text'
]);
$node->save();
$this->drupalGet('node/' . $node->id());
$this->assertRaw('core/modules/statistics/statistics.js', 'Statistics library is available');
}
}
......@@ -39,9 +39,9 @@ function statistics_help($route_name, RouteMatchInterface $route_match) {
*/
function statistics_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
$build['statistics_content_counter']['#attached']['library'][] = 'statistics/drupal.statistics';
$build['#attached']['library'][] = 'statistics/drupal.statistics';
$settings = array('data' => array('nid' => $node->id()), 'url' => Url::fromUri('base:' . drupal_get_path('module', 'statistics') . '/statistics.php')->toString());
$build['statistics_content_counter']['#attached']['drupalSettings']['statistics'] = $settings;
$build['#attached']['drupalSettings']['statistics'] = $settings;
}
}
......
<article{{ attributes }}>
{{ title_prefix }}
{% if not page %}
<h2{{ title_attributes }}>
<a href="{{ url }}" rel="bookmark">{{ label }}</a>
</h2>
{% endif %}
{{ title_suffix }}
{% if display_submitted %}
<footer>
{{ author_picture }}
<div{{ author_attributes }}>
{% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}
{{ metadata }}
</div>
</footer>
{% endif %}
<div{{ content_attributes }}>
{{ content.body }}
</div>
</article>
name: 'Statistics test attached theme'
type: theme
description: 'Theme for testing attached library'
version: VERSION
core: 8.x
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