Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

191: HTML 2 Media feature integration. #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions modules/oe_webtools_html2media/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## 1. Description

This module allows pages to be converted in several formats (png, pdf, ...)
by providing a wrapper for Webtools HTML 2 Media service.
The Webtools service creates a static copy of the url, hosts the binary file on its server and returns its path.
see https://webgate.ec.europa.eu/fpfis/wikis/display/webtools/HTML+2+Media

## 1. Usage

Enable the permission 'Use webtools html2media version'

### As site builder.

Use the block "OpenEuropa Webtools PDF version" to get the pdf version of the current page.

### As developer.

#### Use a link

provide a link to the url /oe-webtools-html2media with a least the page url to be converted.
compulsory parameter:
- url: the url of the page to convert
optional parameters:
- output_format: pdf by default
- format: A4 by default
- orientation: portrait by default
- load_delay: 200 in miliseconds

see for reference https://webgate.ec.europa.eu/fpfis/wikis/display/webtools/HTML+2+Media+-+Technical+details

#### Use a service to retrieve the binary url on webtools server.
use function getMedia(string $page_url, array $options = array()) same $options as for link. 1 extra parameter:
- $verify_url the url is tested and passed to the webservice only if it returns http code 200. default to TRUE.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
url: 'https://europa.eu/webtools/rest/html2m/convert.php'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
oe_webtools_html2media.settings:
type: config_object
label: 'Webtools HTML2Media settings'
mapping:
url:
type: label
label: 'Webtols html2media webservice url'
description: 'A URL that will be called on request.'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: 'OpenEuropa Webtools HTML 2 Media'
type: module
description: 'Implements webtools HTML 2 Media services'
core_version_requirement: ^8.8 || ^9
package: 'ATOF'
php: 7.1
17 changes: 17 additions & 0 deletions modules/oe_webtools_html2media/oe_webtools_html2media.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @file
* Contains oe_webtools_html2media.module.
*/

/**
* Implements hook_theme().
*/
function oe_webtools_html2media_theme() {
return [
'oe_webtools_html2media_block_link' => [
'variables' => ['url' => NULL, 'params' => NULL],
],
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use webtools html2media version:
title: 'Use webtools html2media version'
restrict access: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
oe_webtools_html2media.webtools:
path: '/oe-webtools-html2media'
defaults:
_controller: '\Drupal\oe_webtools_html2media\Controller\WebtoolsToMediaController::getPage'
_title: 'HTML 2 Media'
requirements:
_permission: 'use webtools html2media version'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
oe_webtools_html2media.webtools:
class: Drupal\oe_webtools_html2media\WebtoolsHtmlToMedia
arguments: ['@config.factory', '@logger.factory', '@messenger', '@http_client']
tags:
- {name: webtools}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Drupal\oe_webtools_html2media\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Url;
use Drupal\oe_webtools_html2media\WebtoolsHtmlToMedia;

/**
* Controller routines for Grow Print Version routes.
*/
class WebtoolsToMediaController extends ControllerBase {

/**
* ATOF webtools.
*
* @var \Drupal\oe_webtools_html2media\WebtoolsHtmlToMedia
*/
protected $webtools;

/**
* PrintVersionController constructor.
*
* @param \Drupal\oe_webtools_html2media\WebtoolsHtmlToMedia $webtools
* Webtools.
*/
public function __construct(WebtoolsHtmlToMedia $webtools) {
$this->webtools = $webtools;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('oe_webtools_html2media.webtools')
);
}

/**
* Callback for webtools version.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return string
* returns the binary, which will end up being a page.
*/
public function getPage(Request $request) {
// Get url parameters.
$params = $request->query->all();

// Set url to convert.
$url = $params['url'] ?? '';
if (empty($url)) {
return [
'#markup' => $this->t('Error: please provide the url of the page to convert'),
];
}

$print_url = $this->webtools->getMedia($url, $params);

if (!empty($print_url)) {
$response_headers = ['Cache-Control' => 'no-cache, no-store, must-revalidate'];
$response = new TrustedRedirectResponse($print_url, 302, $response_headers);
$response->addCacheableDependency($print_url);
return $response;
}
else {
// Allow user to try again in case of temporary failure.
$current_url = Url::fromRoute('<current>', $params)->setAbsolute()->toString();
return [
'#markup' => $this->t('Error processing your request. Please <a href=":url">Try again</a>', [':url' => $current_url]),
];

}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Drupal\oe_webtools_html2media\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Url;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Provides a pdf version link block.
*
* @Block(
* id = "oe_webtools_html2media_pdf_version",
* admin_label = @Translation("OpenEuropa Webtools PDF version")
* )
*/
class PdfVersionBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
* Request stack.
*
* @var Symfony\Component\HttpFoundation\RequestStack
*/
protected $request;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('request_stack')
);
}

/**
* Plugin constructor.
*
* @param array $configuration
* Configuration.
* @param string $plugin_id
* Plugin id.
* @param mixed $plugin_definition
* Plugin definition.
* @param Symfony\Component\HttpFoundation\RequestStack $request
* Request params instance.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->request = $request->getCurrentRequest();
}

/**
* {@inheritdoc}
*/
public function build() {

// Get current page url.
$params = $this->request->query->all();
$current_url = Url::fromRoute('<current>', $params)->setAbsolute()->toString();

$controller_url = Url::fromRoute('oe_webtools_html2media.webtools',
[
'url' => $current_url,
])->toString();

return [
'#theme' => 'oe_webtools_html2media_block_link',
'#url' => $controller_url,
];
}

}
Loading