diff --git a/README.md b/README.md index b739fac..6609b88 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,20 @@ URL Embed can be installed via the [standard Drupal installation process](http:/ * Enter the URL that you want to embed. * Optionally, choose to align left, center or right. +## Cache +URL Embed may cache the HTML markup downloaded for each URL. This prevent to request each URL again when a content is edited and re-saved. +Drupal will cache content with external content permanently by default. + +* HTML markup downloaded for each url embed are cached by default for 1 hour. +* You can override this setting by adding in your settings.php : $config['url_embed.settings']['cache_expiration'] = VALUE; +* VALUE must be an integer and is the cache expiration time in seconds. +* Set VALUE to 0 to disable the cache. +* Set VALUE to -1 to have a permanent cache for the markup of URL embed. +* Examples : + * $config['url_embed.settings']['cache_expiration'] = -1; # Permanent cache. + * $config['url_embed.settings']['cache_expiration'] = 0; # No cache. + * $config['url_embed.settings']['cache_expiration'] = 7200; # Markup cached during 2 hours. + ## Embedding URLs without WYSIWYG Users should be embedding URLs using the CKEditor WYSIWYG button as described above. This section is more technical about the HTML markup that is used to embed the actual URL. diff --git a/config/install/url_embed.settings.yml b/config/install/url_embed.settings.yml new file mode 100644 index 0000000..69f2b78 --- /dev/null +++ b/config/install/url_embed.settings.yml @@ -0,0 +1 @@ +cache_expiration: 3600 diff --git a/config/schema/url_embed.schema.yml b/config/schema/url_embed.schema.yml new file mode 100644 index 0000000..59dbe9e --- /dev/null +++ b/config/schema/url_embed.schema.yml @@ -0,0 +1,6 @@ +url_embed.settings: + type: config_object + mapping: + cache_expiration: + label: 'Cache expiration' + type: integer diff --git a/src/Plugin/Field/FieldFormatter/LinkEmbedFormatter.php b/src/Plugin/Field/FieldFormatter/LinkEmbedFormatter.php index 2806c04..e203d15 100644 --- a/src/Plugin/Field/FieldFormatter/LinkEmbedFormatter.php +++ b/src/Plugin/Field/FieldFormatter/LinkEmbedFormatter.php @@ -34,10 +34,11 @@ class LinkEmbedFormatter extends FormatterBase { foreach ($items as $delta => $item) { if ($url = $item->getUrl()->toString()) { try { - if ($info = $this->urlEmbed()->getEmbed($url)) { + $url_output = \Drupal::service('url_embed')->getUrlCode($url); + if ($url_output) { $element[$delta] = array( '#type' => 'inline_template', - '#template' => $info->getCode(), + '#template' => $url_output, ); } } diff --git a/src/Plugin/Filter/UrlEmbedFilter.php b/src/Plugin/Filter/UrlEmbedFilter.php index 3875810..00d8493 100644 --- a/src/Plugin/Filter/UrlEmbedFilter.php +++ b/src/Plugin/Filter/UrlEmbedFilter.php @@ -28,7 +28,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class UrlEmbedFilter extends FilterBase implements ContainerFactoryPluginInterface { use DomHelperTrait; - use UrlEmbedHelperTrait; + + /** + * \Drupal\url_embed\UrlEmbedInterface definition. + * + * @var \Drupal\url_embed\UrlEmbedInterface + */ + protected $url_embed; /** * Constructs a UrlEmbedFilter object. @@ -44,7 +50,7 @@ class UrlEmbedFilter extends FilterBase implements ContainerFactoryPluginInterfa */ public function __construct(array $configuration, $plugin_id, $plugin_definition, UrlEmbedInterface $url_embed) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->setUrlEmbed($url_embed); + $this->url_embed = $url_embed; } /** @@ -73,9 +79,7 @@ class UrlEmbedFilter extends FilterBase implements ContainerFactoryPluginInterfa $url = $node->getAttribute('data-embed-url'); $url_output = ''; try { - if ($info = $this->urlEmbed()->getEmbed($url)) { - $url_output = $info->getCode(); - } + $url_output = $this->url_embed->getUrlCode($url); } catch (\Exception $e) { watchdog_exception('url_embed', $e); diff --git a/src/UrlEmbed.php b/src/UrlEmbed.php index c5e451f..dcf31fa 100644 --- a/src/UrlEmbed.php +++ b/src/UrlEmbed.php @@ -7,12 +7,38 @@ namespace Drupal\url_embed; +use Drupal\Core\Config\ConfigFactoryInterface; use Embed\Embed; +use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Component\Datetime\TimeInterface; /** * A service class for handling URL embeds. */ class UrlEmbed implements UrlEmbedInterface { + use UrlEmbedHelperTrait; + + /** + * Drupal\Core\Cache\CacheBackendInterface definition. + * + * @var \Drupal\Core\Cache\CacheBackendInterface + */ + protected $cacheBackend; + + /** + * Drupal\Component\Datetime\TimeInterface + * + * @var \Drupal\Component\Datetime\TimeInterface + */ + protected $time; + + /** + * Drupal\Core\Config\ConfigFactoryInterface definition. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; /** * @var array @@ -22,8 +48,11 @@ class UrlEmbed implements UrlEmbedInterface { /** * @{inheritdoc} */ - public function __construct(array $config = []) { + public function __construct(CacheBackendInterface $cache_backend, TimeInterface $time, ConfigFactoryInterface $config_factory, array $config = []) { $this->config = $config; + $this->cacheBackend = $cache_backend; + $this->time = $time; + $this->configFactory = $config_factory; } /** @@ -47,4 +76,29 @@ class UrlEmbed implements UrlEmbedInterface { return Embed::create($request, $config ?: $this->config); } + /** + * @{inheritdoc} + */ + public function getUrlCode($url) { + $data = ''; + $cid = 'url_embed:' . $url; + $expire = $this->configFactory->get('url_embed.settings')->get('cache_expiration'); + if ($expire != 0 && $cache = $this->cacheBackend->get($cid)) { + $data = $cache->data; + } + else { + if ($info = $this->urlEmbed()->getEmbed($url)) { + $data = $info->getCode(); + if ($expire != 0) { + $expiration = ($expire == Cache::PERMANENT) ? Cache::PERMANENT : $this->time->getRequestTime() + $expire; + $this->cacheBackend->set($cid, $data, $expiration); + } + } + + } + + return $data; + } + + } diff --git a/src/UrlEmbedHelperTrait.php b/src/UrlEmbedHelperTrait.php index 21ba8ba..18dccca 100644 --- a/src/UrlEmbedHelperTrait.php +++ b/src/UrlEmbedHelperTrait.php @@ -29,7 +29,7 @@ trait UrlEmbedHelperTrait { /** * The URL embed service. * - * @var \Drupal\url_embed\UrlEmbedService. + * @var \Drupal\url_embed\UrlEmbed. */ protected $url_embed; diff --git a/src/UrlEmbedInterface.php b/src/UrlEmbedInterface.php index c1f52bb..898c5a7 100644 --- a/src/UrlEmbedInterface.php +++ b/src/UrlEmbedInterface.php @@ -7,6 +7,9 @@ namespace Drupal\url_embed; +use Drupal\Core\Cache\DatabaseBackend; +use Drupal\Component\Datetime\TimeInterface; + /** * A service class for handling URL embeds. * @@ -14,8 +17,6 @@ namespace Drupal\url_embed; */ interface UrlEmbedInterface { - public function __construct(array $config = []); - public function getConfig(); public function setConfig(array $config); @@ -36,4 +37,15 @@ interface UrlEmbedInterface { */ public function getEmbed($request, array $config = []); + /** + * Get the HTML code for an URL embed. + * + * @param string $url + * The URL to embed. + * + * @return null|string + * the HTML code of the URL embed. + */ + public function getUrlCode($url); + } diff --git a/url_embed.install b/url_embed.install index 4d4d434..aa602cb 100644 --- a/url_embed.install +++ b/url_embed.install @@ -55,3 +55,15 @@ function url_embed_update_8001() { $old_embed_button->delete(); } } + +/** + * Sets a default cache expiration time setting for URL embed. + */ +function url_embed_update_8002() { + $config_factory = \Drupal::configFactory(); + $config = $config_factory->getEditable('url_embed.settings'); + if ($config->isNew()) { + $data = ['cache_expiration' => 3600]; + $config->setData($data)->save(); + } +} diff --git a/url_embed.services.yml b/url_embed.services.yml index c79474a..b6d2284 100644 --- a/url_embed.services.yml +++ b/url_embed.services.yml @@ -1,3 +1,4 @@ services: url_embed: class: Drupal\url_embed\UrlEmbed + arguments: ['@cache.data', '@datetime.time', '@config.factory']