Skip to content

Instantly share code, notes, and snippets.

@dpobel
Created March 14, 2016 10:26
Show Gist options
  • Save dpobel/03bee3b6c45591a1ca0a to your computer and use it in GitHub Desktop.
Save dpobel/03bee3b6c45591a1ca0a to your computer and use it in GitHub Desktop.
Custom tag
  • .xsl in app/Resources/
  • *.html.twig in app/Resources/views/
  • app/config/ezplatform.yml
  • src/DP/TestCommandsBundle/Command/CreateRichTextCustomTagCommand.php
<?php
namespace DP\TestCommandsBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CreateRichTextCustomTagCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName( 'dp:createrichtextcustomtag' )
->setDescription( 'Create RichText with HTML entities' );
}
protected function execute( InputInterface $input, OutputInterface $output )
{
$repository = $this->getContainer()->get( 'ezpublish.api.repository' );
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$contentTypeService = $repository->getContentTypeService();
$repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) );
$parentLocationId = 99;
$contentTypeIdentifier = 'blog_post';
$name = "Custom tag " . date('H:i:s');
$summary = '<?xml version="1.0" ?><section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">';
$summary .= '<p>Test sub: <sub>Test</sub> after</p>';
$summary .= '<blockquote><p>A quote</p></blockquote>';
$summary .= '<iframe data-ezcustomtag="youtube" width="560" height="360" src="http://www.youtube.com/embed/hvip2Hgka6E" frameborder="0" allowfullscreen=""/>';
$summary .= '<p>Normal paragraph after</p>';
$summary .= '</section>';
try
{
$contentType = $contentTypeService->loadContentTypeByIdentifier( $contentTypeIdentifier );
$contentCreateStruct = $contentService->newContentCreateStruct( $contentType, 'eng-GB' );
$contentCreateStruct->setField( 'title', $name );
$contentCreateStruct->setField( 'body', $summary );
// instantiate a location create struct from the parent location
$locationCreateStruct = $locationService->newLocationCreateStruct( $parentLocationId );
// create a draft using the content and location create struct and publish it
$draft = $contentService->createContent( $contentCreateStruct, array( $locationCreateStruct ) );
$content = $contentService->publishVersion( $draft->versionInfo );
// print out the content
print_r( $content );
}
// Content type or location not found
catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
{
$output->writeln( $e->getMessage() );
}
// Invalid field value
catch ( \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $e )
{
var_dump( $e->getFieldErrors() );
//$output->writeln( $e->getMessage() );
}
// Required field missing or empty
catch ( \eZ\Publish\API\Repository\Exceptions\ContentValidationException $e )
{
$output->writeln( $e->getMessage() );
}
}
}
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
exclude-result-prefixes="ezcustom"
version="1.0">
<xsl:variable name="outputNamespace" select="''"/>
<xsl:template match="eztemplate[@data-ezname='youtube']">
<iframe>
<xsl:attribute name="data-ezcustomtag">youtube</xsl:attribute>
<xsl:attribute name="width"><xsl:value-of select="ezconfig/ezvalue[@key='width']"/></xsl:attribute>
<xsl:attribute name="height"><xsl:value-of select="ezconfig/ezvalue[@key='height']"/></xsl:attribute>
<xsl:attribute name="src"><xsl:value-of select="ezconfig/ezvalue[@key='src']"/></xsl:attribute>
<xsl:attribute name="frameborder">0</xsl:attribute>
<xsl:attribute name="allowfullscreen"/>
</iframe>
</xsl:template>
</xsl:stylesheet>
ezpublish:
# Repositories configuration, setup default repository to support solr if enabled
repositories:
default:
storage: ~
search:
engine: %search_engine%
connection: default
# Siteaccess configuration, with one siteaccess per default
siteaccess:
list: [site]
groups:
site_group: [site]
default_siteaccess: site
match:
URIElement: 1
# System settings, grouped by siteaccess and/or siteaccess group
system:
site_group:
# These reflect the current installers, complete installation before you change them. For changing var_dir
# it is recommended to install clean, then change setting before you start adding binary content, otherwise you'll
# need to manually modify your database data to reflect this to avoid exceptions.
var_dir: var/site
languages: [eng-GB]
fieldtypes:
ezrichtext:
# docbook to html5 ("html5output")
# extends vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/FieldType/RichText/Resources/stylesheets/docbook/xhtml5/output/core.xsl
output_custom_tags:
- {path: ../app/Resources/output_youtube.xsl}
# html5edit to docbook
# extends vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/FieldType/RichText/Resources/stylesheets/xhtml5/edit/docbook.xsl
input_custom_tags:
- {path: app/Resources/input_youtube.xsl}
# docbook to html5edit
# extends vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/FieldType/RichText/Resources/stylesheets/docbook/xhtml5/edit/core.xsl
# WTF: ../ required
edit_custom_tags:
- {path: ../app/Resources/edit_youtube.xsl}
tags:
youtube:
template: app/Resources/views/youtube.html.twig
config:
wtf: true
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ezxhtml5="http://ez.no/namespaces/ezpublish5/xhtml5/edit"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
exclude-result-prefixes="ezxhtml5"
xmlns="http://docbook.org/ns/docbook"
version="1.0">
<xsl:output indent="yes" encoding="UTF-8"/>
<xsl:template match="ezxhtml5:iframe[@data-ezcustomtag='youtube']">
<eztemplate name="youtube">
<ezconfig>
<ezvalue key="width"><xsl:value-of select="@width"/></ezvalue>
<ezvalue key="height"><xsl:value-of select="@height"/></ezvalue>
<ezvalue key="src"><xsl:value-of select="@src"/></ezvalue>
</ezconfig>
<!-- Seems to break the parser <ezcontent></ezcontent> -->
<!-- It seems to be either ezconfig or ezcontent but not both -->
</eztemplate>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:docbook="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
exclude-result-prefixes="docbook xlink ezxhtml ezcustom"
version="1.0">
<xsl:output indent="yes" encoding="UTF-8"/>
<xsl:variable name="outputNamespace" select="''"/>
<xsl:template match="docbook:eztemplate[@name='youtube']">
<iframe frameborder="0" allowfullscreen="allowfullscreen">
<xsl:attribute name="width">
<xsl:value-of select="docbook:ezconfig/docbook:ezvalue[@key='width']" />
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="docbook:ezconfig/docbook:ezvalue[@key='height']" />
</xsl:attribute>
<xsl:attribute name="src">
<xsl:value-of select="docbook:ezconfig/docbook:ezvalue[@key='src']" />
</xsl:attribute>
</iframe>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment