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

EZP-31307: As an Editor I want to configure link attribute for a custom tag #112

Closed
wants to merge 7 commits into from
32 changes: 31 additions & 1 deletion src/bundle/Resources/config/fieldtype_services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,47 @@ services:
- '%ezplatform.ezrichtext.custom_tags%'
- '%ezplatform.ezrichtext.custom_styles%'

EzSystems\EzPlatformRichText\eZ\RichText\HrefResolver:
arguments:
- '@ezpublish.api.service.location'
- '@ezpublish.api.service.content'
- '@ezpublish.urlalias_router'
calls:
- ['setLogger', ['@?logger']]

ezrichtext.converter.link:
class: EzSystems\EzPlatformRichText\eZ\RichText\Converter\Link
arguments: ['@ezpublish.api.service.location', '@ezpublish.api.service.content', '@ezpublish.urlalias_router', '@?logger']
arguments:
- '@ezpublish.api.service.location'
- '@ezpublish.api.service.content'
- '@ezpublish.urlalias_router'
- '@EzSystems\EzPlatformRichText\eZ\RichText\HrefResolver'
- '@?logger'
tags:
- {name: ezpublish.ezrichtext.converter.output.xhtml5, priority: 0}

EzSystems\EzPlatformRichText\eZ\RichText\Template\TemplateRegistry:
factory: ['EzSystems\EzPlatformRichText\eZ\RichText\Template\TemplateRegistry', 'createFromConfig']
arguments:
$config: '%ezplatform.ezrichtext.custom_tags%'

EzSystems\EzPlatformRichText\eZ\RichText\Template\Attribute\Handler\AttributeHandlerDispatcher:
arguments:
- !tagged ezplatform.richtext.template.attribute.handler

EzSystems\EzPlatformRichText\eZ\RichText\Template\Attribute\Handler\LinkAttributeHandler:
arguments:
- '@EzSystems\EzPlatformRichText\eZ\RichText\HrefResolver'
tags:
- {name: ezplatform.richtext.template.attribute.handler}

ezrichtext.converter.template:
class: EzSystems\EzPlatformRichText\eZ\RichText\Converter\Render\Template
arguments:
- '@ezrichtext.renderer'
- '@ezrichtext.converter.output.xhtml5'
- '@EzSystems\EzPlatformRichText\eZ\RichText\Template\TemplateRegistry'
- '@EzSystems\EzPlatformRichText\eZ\RichText\Template\Attribute\Handler\AttributeHandlerDispatcher'
- '@?logger'
tags:
- {name: ezpublish.ezrichtext.converter.output.xhtml5, priority: 10}
Expand Down
77 changes: 23 additions & 54 deletions src/lib/eZ/RichText/Converter/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,55 @@
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter;
use EzSystems\EzPlatformRichText\eZ\RichText\HrefResolverInterface;
use Psr\Log\LoggerInterface;
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException as APIUnauthorizedException;
use DOMDocument;
use DOMXPath;

class Link implements Converter
{
/**
* @deprecated since version 2.5.9, to be removed in 3.0
*
* @var \eZ\Publish\API\Repository\LocationService
*/
protected $locationService;

/**
* @deprecated since version 2.5.9, to be removed in 3.0
*
* @var \eZ\Publish\API\Repository\ContentService
*/
protected $contentService;

/**
* @deprecated since version 2.5.9, to be removed in 3.0
*
* @var \eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter
*/
protected $urlAliasRouter;

/**
* @deprecated since version 2.5.9, to be removed in 3.0
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

public function __construct(LocationService $locationService, ContentService $contentService, UrlAliasRouter $urlAliasRouter, LoggerInterface $logger = null)
{
/** @var \EzSystems\EzPlatformRichText\eZ\RichText\HrefResolverInterface */
private $hrefResolver;

public function __construct(
LocationService $locationService,
ContentService $contentService,
UrlAliasRouter $urlAliasRouter,
HrefResolverInterface $hrefResolver,
LoggerInterface $logger = null
) {
$this->locationService = $locationService;
$this->contentService = $contentService;
$this->urlAliasRouter = $urlAliasRouter;
$this->hrefResolver = $hrefResolver;
$this->logger = $logger;
}

Expand All @@ -68,56 +84,6 @@ public function convert(DOMDocument $document)

/** @var \DOMElement $link */
foreach ($xpath->query($xpathExpression) as $link) {
// Set resolved href to number character as a default if it can't be resolved
$hrefResolved = '#';
$href = $link->getAttribute('xlink:href');
$location = null;
preg_match('~^(.+://)?([^#]*)?(#.*|\\s*)?$~', $href, $matches);
list(, $scheme, $id, $fragment) = $matches;

if ($scheme === 'ezcontent://') {
try {
$contentInfo = $this->contentService->loadContentInfo($id);
$location = $this->locationService->loadLocation($contentInfo->mainLocationId);
$hrefResolved = $this->urlAliasRouter->generate($location) . $fragment;
} catch (APINotFoundException $e) {
if ($this->logger) {
$this->logger->warning(
'While generating links for richtext, could not locate ' .
'Content object with ID ' . $id
);
}
} catch (APIUnauthorizedException $e) {
if ($this->logger) {
$this->logger->notice(
'While generating links for richtext, unauthorized to load ' .
'Content object with ID ' . $id
);
}
}
} elseif ($scheme === 'ezlocation://') {
try {
$location = $this->locationService->loadLocation($id);
$hrefResolved = $this->urlAliasRouter->generate($location) . $fragment;
} catch (APINotFoundException $e) {
if ($this->logger) {
$this->logger->warning(
'While generating links for richtext, could not locate ' .
'Location with ID ' . $id
);
}
} catch (APIUnauthorizedException $e) {
if ($this->logger) {
$this->logger->notice(
'While generating links for richtext, unauthorized to load ' .
'Location with ID ' . $id
);
}
}
} else {
$hrefResolved = $href;
}

$hrefAttributeName = 'xlink:href';

// For embeds set the resolved href to the separate attribute
Expand All @@ -128,6 +94,9 @@ public function convert(DOMDocument $document)
$hrefAttributeName = 'href_resolved';
}

// Set resolved href to number character as a default if it can't be resolved
$hrefResolved = $this->hrefResolver->resolve($link->getAttribute('xlink:href'));

$link->setAttribute($hrefAttributeName, $hrefResolved);
}

Expand Down
43 changes: 36 additions & 7 deletions src/lib/eZ/RichText/Converter/Render/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use DOMXPath;
use EzSystems\EzPlatformRichText\eZ\RichText\Converter;
use EzSystems\EzPlatformRichText\eZ\RichText\Converter\Render;
use EzSystems\EzPlatformRichText\eZ\RichText\Template\Attribute\Handler\AttributeHandler;
use EzSystems\EzPlatformRichText\eZ\RichText\Template\TemplateRegistry;
use EzSystems\EzPlatformRichText\eZ\RichText\RendererInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand All @@ -25,29 +27,37 @@ class Template extends Render implements Converter
{
const LITERAL_LAYOUT_LINE_BREAK = "\n";

/**
* @var \EzSystems\EzPlatformRichText\eZ\RichText\Converter
*/
/** @var \EzSystems\EzPlatformRichText\eZ\RichText\Converter */
private $richTextConverter;

/**
* @var \Psr\Log\LoggerInterface
*/
/** @var \EzSystems\EzPlatformRichText\eZ\RichText\Template\TemplateRegistry */
private $templateRegistry;

/** @var \EzSystems\EzPlatformRichText\eZ\RichText\Template\Attribute\Handler\AttributeHandler */
private $attributeHandler;

/** @var \Psr\Log\LoggerInterface */
private $logger;

/**
* RichText Template converter constructor.
*
* @param \EzSystems\EzPlatformRichText\eZ\RichText\RendererInterface $renderer
* @param \EzSystems\EzPlatformRichText\eZ\RichText\Converter $richTextConverter
* @param \EzSystems\EzPlatformRichText\eZ\RichText\Template\TemplateRegistry $templateRegistry
* @param \EzSystems\EzPlatformRichText\eZ\RichText\Template\Attribute\Handler\AttributeHandler $attributeHandler
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
RendererInterface $renderer,
Converter $richTextConverter,
TemplateRegistry $templateRegistry,
AttributeHandler $attributeHandler,
LoggerInterface $logger = null
) {
$this->richTextConverter = $richTextConverter;
$this->templateRegistry = $templateRegistry;
$this->attributeHandler = $attributeHandler;
$this->logger = $logger ?? new NullLogger();

parent::__construct($renderer);
Expand Down Expand Up @@ -99,7 +109,7 @@ protected function processTemplate(DOMDocument $document, DOMXPath $xpath, DOMEl
$templateType = $template->hasAttribute('type') ? $template->getAttribute('type') : 'tag';
$parameters = [
'name' => $templateName,
'params' => $this->extractConfiguration($template),
'params' => $this->processConfiguration($templateName, $this->extractConfiguration($template)),
];

$contentNodes = $xpath->query('./docbook:ezcontent', $template);
Expand Down Expand Up @@ -235,4 +245,23 @@ private function wrapContentWithLiteralLayout(DOMNode $rootNode, DOMNode $node):

return $rootNode->appendChild($literalLayoutNode);
}

private function processConfiguration(string $templateName, array $params): array
{
if ($this->templateRegistry->has($templateName)) {
$template = $this->templateRegistry->get($templateName);

foreach ($params as $name => $value) {
if ($template->hasAttribute($name)) {
$attribute = $template->getAttribute($name);

if ($this->attributeHandler->supports($template, $attribute)) {
$params[$name] = $this->attributeHandler->process($template, $attribute, $value);
}
}
}
}

return $params;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformRichText\eZ\RichText\Exception\Template;

use RuntimeException;
use Throwable;

final class AttributeNotFoundException extends RuntimeException
{
/** @var string */
private $templateName;

/** @var string */
private $attributeName;

public function __construct(string $templateName, string $attributeName, int $code = 0, Throwable $previous = null)
{
parent::__construct(sprintf('Could not find template attribute %s::%s', $templateName, $attributeName), $code, $previous);

$this->templateName = $templateName;
$this->attributeName = $attributeName;
}

public function getTemplateName(): string
{
return $this->templateName;
}

public function getAttributeName(): string
{
return $this->attributeName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformRichText\eZ\RichText\Exception\Template;

use RuntimeException;
use Throwable;

final class TemplateNotFoundException extends RuntimeException
{
/** @var string */
private $name;

public function __construct(string $name, int $code = 0, Throwable $previous = null)
{
parent::__construct(sprintf('Could not find template %s', $name), $code, $previous);

$this->name = $name;
}

public function getName(): string
{
return $this->name;
}
}
Loading