Skip to content

Commit

Permalink
Add a real favicon manager for performance reasons.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacerider committed Aug 2, 2017
1 parent 8edb96b commit 9159d2b
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 3 deletions.
11 changes: 8 additions & 3 deletions real_favicon.module
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ function real_favicon_help($route_name, RouteMatchInterface $route_match) {
* Implements hook_page_attachments_alter().
*/
function real_favicon_page_attachments_alter(array &$attachments) {
if ($real_favicon = real_favicon_load_by_theme()) {
$tags = $real_favicon->getValidTagsAsString();
$theme = \Drupal::theme()->getActiveTheme()->getName();
$realFaviconManager = \Drupal::service('real_favicon.manager');

if ($tags = $realFaviconManager->getTags($theme)) {
// Remove default favicon from html_head_link.
if (!empty($attachments['#attached']['html_head_link'])) {
foreach ($attachments['#attached']['html_head_link'] as $i => $item) {
Expand All @@ -53,7 +55,10 @@ function real_favicon_page_attachments_alter(array &$attachments) {
[
'#type' => 'markup',
'#markup' => $tags,
'#allowed_tags' => ['link', 'meta']
'#allowed_tags' => ['link', 'meta'],
'#cache' => [
'tags' => $realFaviconManager->getCacheTags(),
],
],
'real_favicon'
];
Expand Down
5 changes: 5 additions & 0 deletions real_favicon.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
real_favicon.manager:
class: Drupal\real_favicon\RealFaviconManager
arguments: ['@entity_type.manager', '@config.factory', '@cache.data']

109 changes: 109 additions & 0 deletions src/RealFaviconManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Drupal\real_favicon;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\Cache;

/**
* Class RealFaviconManager.
*/
class RealFaviconManager implements RealFaviconManagerInterface {

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
public $entityTypeManager;

/**
* The real faicon configuration.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;

/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;

/**
* The cache id.
*
* @var string
*/
protected $cid = 'real_favicon.favicon';

/**
* The cache tags.
*
* @var array
*/
protected $cacheTags = [
'config:real_favicon.settings',
'config:real_favicon_list',
];

/**
* Constructs a new RealFaviconManager object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache) {
$this->entityTypeManager = $entity_type_manager;
$this->config = $config_factory->get('real_favicon.settings');
$this->cache = $cache;
}

/**
* {@inheritdoc}
*/
public function getTags($theme_id) {
$tags = NULL;
$enabled = $this->config->get('themes');
if (!empty($enabled[$theme_id])) {
$cid = $this->cid . '.tags.' . $theme_id;
if ($cache = $this->cache->get($cid)) {
$tags = $cache->data;
}
else {
if ($favicon = $this->loadFavicon($theme_id)) {
$tags = $favicon->getValidTagsAsString();
}
$this->cache->set($cid, $tags, Cache::PERMANENT, $this->cacheTags);
}
}
return $tags;
}

/**
* Load a favicon.
*/
public function loadFavicon($theme_id) {
$favicon = NULL;
$enabled = $this->config->get('themes');
if (!empty($enabled[$theme_id])) {
$favicon = $this->entityTypeManager->getStorage('real_favicon')->load($enabled[$theme_id]);
}
return $favicon;
}

/**
* {@inheritdoc}
*/
public function getCacheTags() {
return $this->cacheTags;
}

}
37 changes: 37 additions & 0 deletions src/RealFaviconManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\real_favicon;

/**
* Interface RealFaviconManagerInterface.
*/
interface RealFaviconManagerInterface {

/**
* Get cache tags for a theme as a string.
*
* @param string $theme_id
* The theme id.
*
* @return string|null
* The tags as HTML ready for output.
*/
public function getTags($theme_id);

/**
* Get the real favicon entity assiciated with a theme.
*
* @param string $theme_id
* The theme id.
*
* @return \Drupal\real_favicon\Entity\RealFavicon|null
* The real favicon entity.
*/
public function loadFavicon($theme_id);

/**
* Get the cache tags for a theme favicon.
*/
public function getCacheTags();

}

0 comments on commit 9159d2b

Please sign in to comment.