Skip to content

Instantly share code, notes, and snippets.

@arfaram
Last active August 25, 2017 12:15
Show Gist options
  • Save arfaram/797e56d25f203420ee1634db09c58a55 to your computer and use it in GitHub Desktop.
Save arfaram/797e56d25f203420ee1634db09c58a55 to your computer and use it in GitHub Desktop.

#you will often use the following commands when you are working with eZPlatform or Symfony2

Refresh assetic (css and js)

php app/console assetic:dump --env=dev web
php app/console assetic:dump --env=prod web
php app/console assetic:watch

Clear cache

php app/console cache:clear --env=dev
php app/console cache:clear --env=prod

Create assets

php app/console assets:install web

OR as symlink

php app/console assets:install web --symlink

list of all routes SF > 2.7

php app/console debug:router

create bundle

php app/console generate:bundle

Create Translation file for the custom ezplatform bundle

php app/console translation:update --force --output-format=xlf en ProjectAppBundle
  • This will create following file: src/Project/AppBundle/Resources/translations/messages.en.xlf
  • Check folder write access bevor

see also:https://github.com/ezsystems/ezpublish-kernel/blob/master/doc/specifications/template/ez_functions.md

Add Twig block

 {% block content %}{% endblock %}

Include Twig Template

From /view folder:

{% include 'ProjectAppBundle::page_head_style.html.twig' %}

From subfolder: /view/subfolder

{% include 'ProjectAppBundle:subfolder:page_head_style.html.twig' %}

using parameters:

{% include 'ProjectAppBundle::page_head_style.html.twig' with {'foo' : bar} %}

Twig Translation Text

- {{'text'|trans}}
- {{ '< h3 >foo<\ h3 >' | trans |  raw }}
- {% trans %}Search for "%searchText%" returned %searchCount% matches{% endtrans %}

Twig: Get Content Type field Value

TextLine[ezstring]

{{ ez_content_name(content) }}
or
{{content.getFieldValue('FILED-NAME')}}
see also: 
{{content.getField('FILED-NAME')}}

RichText[ezrichtext]

{{ ez_render_field(content, 'FILED-NAME') }}

Text Block[eztext]

{{ ez_render_field(content, 'FILED-NAME') }}

Have you saved HTML code in eztext? javascript , iframe code ? then use the magic raw filter:

{{content.getFieldValue('FILED-NAME')|raw('html')}}

Selection[ezselection]

 {{content.getFieldValue('FILED-NAME')}}

Image[ezimage]

 {{ ez_render_field(content, 'FILED-NAME') }}
 You need your own < img > Tag ? then:
 < img src="{{contentElement.getFieldValue('image').uri}}" alt="{{contentElement.getFieldValue('image').alternativeText}}" / >

content Relation (multiples)[ezobjectrelationlist]

 {{content.getFieldValue('images')}}   -> Return only the objectIds (content IDs) 

Exapmle calling Controller from template:

{{ render( controller( "ProjectAppBundle:ObjectList:getObjectData", { 'tpl': "ProjectAppBundle::target.html.twig", 'siteaccess': ezpublish.siteaccess, parameters:{'foo': bar } } )) }}

Correspondant Controller:

namespace Portal\MainBundle\Controller;

use eZ\Bundle\EzPublishCoreBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
//...
class ObjectListController extends Controller{
//...
 public function getObjectDataAction( $tpl, $siteaccess , $parameters ){
 $response = new Response;
 //...
 $data = $somedata ;
 //...
 return $this->render(
				$tpl,
				array( 	
						'content' => $data, 
						'parameters' => $parameters,
					 ),$response
						
		);
 }
}

Caching Controller response for 24 hours

$response = new Response;
$response->setPublic();
$response->setSharedMaxAge( 86400 );

Or also:

$response = new Response;
$response = $this->render(
			$tpl,
			array( 	'content' => $content)
			);	
$response->setPublic();
$response->setSharedMaxAge( 86400 );			
return $response ;

Do not allow Caching

$response = new Response;
$response->setPrivate();  //something like user data
$response->headers->addCacheControlDirective('no-cache', true);
$response->headers->addCacheControlDirective('max-age', 0);
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('no-store', true);

Get variable from parametres.yml in the Controller

In parametres.yml:
parameters:
    settings.site.email:  me@ez.no

In controller:
  $resolver = $this->getConfigResolver();
  $to = $resolver->getParameter( 'email', 'settings', 'site' );

another way:

In parametres.yml:
parameters:
	to: me@ez.no

In controller:
  $to = $container->getParameter('to' );

Access Twig global Variable

In parametres.yml:

parameters:
    to: me@ez.no

In config.yml:

twig:
    globals:
        version: '%to%'

In Twig Template

{{ to }}

Load Location data

$locationService = $this->getRepository()->getLocationService();     
$location = $locationService->loadLocation($LocationId);

Load Object data

$content  = $this->getRepository()->getContentService()->loadContent( $ObjectId );

Get RichText Field in Controller

$content  = $this->getRepository()->getContentService()->loadContent( $ObjectId );
$xmlTextValue = $content->getFieldValue( 'THE-RICHTEXT-FIELD-NAME' );
/** @var \eZ\Publish\Core\FieldType\XmlText\Converter\Html5 $html5Converter */
$html5Converter = $this->get( 'ezpublish.fieldType.ezxmltext.converter.html5' );
$html = $html5Converter->convert( $xmlTextValue->xml );

OR without HTML format:

$content->getFieldValue('THE-RICHTEXT-FIELD-NAME')->xml->textContent;

Use Logging

try{
....
}catch ( Exception $e )
{
  $this->get( 'logger' )->error( "MyControllerName: Oh Error : {$e->getMessage()}" );
  ...
}

symfony2 Mapping a URL to a Controller

 /**
     * @Route("/hello/{name}", name="hello")
     */
    public function indexAction($name)
    {
        return new Response('Hello '.$name.'!');
    }

symfony2 Render a Template without a custom Controller

acme_privacy:
    path: /privacy
    defaults:
        _controller: FrameworkBundle:Template:template
        template:    static/privacy.html.twig

symfony2 Generate route url in the controller

return $this->redirect(($this->generateUrl('acme_privacy')));

$_GET and $_POST variable

// $_GET parameters
$request->query->get('name');

// $_POST parameters
$request->request->get('name');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment