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-29848: Make existing AdminUI tabs extendable by allowing template path and parameters change #728

Merged
merged 3 commits into from
Dec 7, 2018
Merged
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
8 changes: 8 additions & 0 deletions src/bundle/Resources/config/services/tabs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ services:
abstract: true
lazy: true

EzSystems\EzPlatformAdminUi\Tab\AbstractEventDispatchingTab:
parent: EzSystems\EzPlatformAdminUi\Tab\AbstractTab
abstract: true
autowire: true
autoconfigure: false
public: false
lazy: true

EzSystems\EzPlatformAdminUi\Tab\AbstractRouteBasedTab:
parent: EzSystems\EzPlatformAdminUi\Tab\AbstractTab
abstract: true
Expand Down
153 changes: 82 additions & 71 deletions src/bundle/Resources/views/content/tab/versions/tab.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,98 @@
{% form_theme form_version_remove_draft '@ezdesign/form_fields.html.twig' %}
{% form_theme form_version_remove_archived '@ezdesign/form_fields.html.twig' %}

{% if draft_pager.currentPageResults is not empty %}
<section>
{{ form_start(form_version_remove_draft, {
'action': path('ezplatform.version.remove'),
'attr': { 'class': 'ez-toggle-btn-state', 'data-toggle-button-id': '#delete-translations-' ~ form_version_remove_draft.remove.vars.id }
}) }}
{% include '@ezdesign/parts/table_header.html.twig' with { headerText: 'tab.versions.draft_under_edit'|trans()|desc('Draft under edit'), tools: tab.table_header_tools(form_version_remove_draft) } %}
{{ include('@ezdesign/content/tab/versions/table.html.twig', {
'versions': draft_pager.currentPageResults,
'is_draft': true,
'form': form_version_remove_draft,
'haveToPaginate': draft_pager.haveToPaginate,
'content_is_user': content_is_user
}) }}
{{ form_end(form_version_remove_draft) }}
{% set table_template_path = table_template_path|default('@ezdesign/content/tab/versions/table.html.twig') %}
{% set show_drafts_table = show_drafts_table|default(draft_pager.currentPageResults is not empty) %}

{% if draft_pager.haveToPaginate %}
<div class="row justify-content-center align-items-center mb-2">
<span class="ez-pagination__text">
{{ 'pagination.viewing'|trans({
'%viewing%': draft_pager.currentPageResults|length,
'%total%': draft_pager.nbResults}, 'pagination')|desc('Viewing <strong>%viewing%</strong> out of <strong>%total%</strong> items')|raw }}
</span>
</div>
<div class="row justify-content-center align-items-center ez-pagination__btn mb-4">
{{ pagerfanta(draft_pager, 'ez',{
'routeName': draft_pagination_params.route_name,
'routeParams': draft_pagination_params.route_params|merge({
'_fragment': constant('EzSystems\\EzPlatformAdminUi\\Tab\\LocationView\\VersionsTab::URI_FRAGMENT'),
}),
'pageParameter': '[page][version_draft]'
{% block tab_content %}
{% block table_wrapper_drafts %}
{% if show_drafts_table %}
<section>
{{ form_start(form_version_remove_draft, {
'action': path('ezplatform.version.remove'),
'attr': { 'class': 'ez-toggle-btn-state', 'data-toggle-button-id': '#delete-translations-' ~ form_version_remove_draft.remove.vars.id }
}) }}
</div>
{% block table_drafts %}
{% include '@ezdesign/parts/table_header.html.twig' with {
'headerText': 'tab.versions.draft_under_edit'|trans()|desc('Draft under edit'),
'tools': tab.table_header_tools(form_version_remove_draft)
} %}
{% include table_template_path with {
'versions': draft_pager.currentPageResults,
'is_draft': true,
'form': form_version_remove_draft,
'haveToPaginate': draft_pager.haveToPaginate,
'content_is_user': content_is_user
} %}

{% if draft_pager.haveToPaginate %}
<div class="row justify-content-center align-items-center mb-2">
<span class="ez-pagination__text">
{{ 'pagination.viewing'|trans({ '%viewing%': draft_pager.currentPageResults|length, '%total%': draft_pager.nbResults }, 'pagination')|desc('Viewing <strong>%viewing%</strong> out of <strong>%total%</strong> items')|raw }}
</span>
</div>
<div class="row justify-content-center align-items-center ez-pagination__btn mb-4">
{{ pagerfanta(draft_pager, 'ez', {
'routeName': draft_pagination_params.route_name,
'routeParams': draft_pagination_params.route_params|merge({
'_fragment': constant('EzSystems\\EzPlatformAdminUi\\Tab\\LocationView\\VersionsTab::URI_FRAGMENT'),
}),
'pageParameter': '[page][version_draft]'
}) }}
</div>
{% endif %}
{% endblock %}
{{ form_end(form_version_remove_draft) }}
</section>
{% include '@ezdesign/content/modal_version_conflict.html.twig' %}
{% endif %}
</section>
{% include '@ezdesign/content/modal_version_conflict.html.twig' %}
{% endif %}
{% endblock %}

{% if published_versions is not empty %}
<section>
{% include '@ezdesign/parts/table_header.html.twig' with { headerText: 'tab.versions.published_version'|trans()|desc('Published version') } %}
{% block table_wrapper_published %}
{% if published_versions is not empty %}
{{ include('@ezdesign/content/tab/versions/table.html.twig', { 'versions': published_versions }) }}
{% else %}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review note: Same as below.

<p>
{{ 'tab.versions.no_permission'|trans()|desc('You don\'t have access to view the content item\'s versions') }}
</p>
<section>
{% block table_published %}
{% include '@ezdesign/parts/table_header.html.twig' with {
'headerText': 'tab.versions.published_version'|trans()|desc('Published version')
} %}
{% include table_template_path with { 'versions': published_versions } %}
{% endblock %}
</section>
{% endif %}
</section>
{% endif %}
{% endblock %}

{% if archived_versions is not empty %}
<section>
{{ form_start(form_version_remove_archived, {
'action': path('ezplatform.version.remove'),
'attr': { 'class': 'ez-toggle-btn-state', 'data-toggle-button-id': '#delete-translations-' ~ form_version_remove_archived.remove.vars.id }
}) }}
{% include '@ezdesign/parts/table_header.html.twig' with { headerText: 'tab.versions.archived_versions'|trans()|desc('Archived versions'), tools: tab.table_header_tools(form_version_remove_archived) } %}
{% block table_wrapper_archived %}
{% if archived_versions is not empty %}
Copy link
Contributor Author

@webhdx webhdx Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review note - this if statement is always true because it has been already checked a few lines above thus else block is never reached.

{{ include('@ezdesign/content/tab/versions/table.html.twig', {
'versions': archived_versions,
'form': form_version_remove_archived,
'is_archived': true,
'form_archived_version_restore': form_archived_version_restore,
'content_is_user': content_is_user
}) }}
{% else %}
<p>
{{ 'tab.versions.no_permission'|trans()|desc('You don\'t have access to view the content item\'s versions') }}
</p>
<section>
{{ form_start(form_version_remove_archived, {
'action': path('ezplatform.version.remove'),
'attr': { 'class': 'ez-toggle-btn-state', 'data-toggle-button-id': '#delete-translations-' ~ form_version_remove_archived.remove.vars.id }
}) }}
{% block table_archived %}
{% include '@ezdesign/parts/table_header.html.twig' with {
'headerText': 'tab.versions.archived_versions'|trans()|desc('Archived versions'),
'tools': tab.table_header_tools(form_version_remove_archived)
} %}
{% include table_template_path with {
'versions': archived_versions,
'form': form_version_remove_archived,
'is_archived': true,
'form_archived_version_restore': form_archived_version_restore,
'content_is_user': content_is_user
} %}
{% endblock %}
{{ form_end(form_version_remove_archived) }}
</section>
{% endif %}
{{ form_end(form_version_remove_archived) }}
</section>
{% endif %}

{{ form_start(form_archived_version_restore, {
'action': path('ezplatform.content.edit'),
'attr': { 'class': 'ez-edit-content-form'}
}) }}
{{ form_widget(form_archived_version_restore.language, {'attr': {'hidden': 'hidden'}}) }}
{{ form_end(form_archived_version_restore) }}
{{ form_start(form_archived_version_restore, {
'action': path('ezplatform.content.edit'),
'attr': { 'class': 'ez-edit-content-form'}
}) }}
{{ form_widget(form_archived_version_restore.language, {'attr': {'hidden': 'hidden'}}) }}
{{ form_end(form_archived_version_restore) }}
{% endblock %}
{% endblock %}

{% macro table_header_tools(form) %}
{% set modal_data_target = 'modal-' ~ form.remove.vars.id %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{% endif %}
<th>{{ 'tab.versions.table.version'|trans()|desc('Version') }}</th>
<th>{{ 'tab.versions.table.modified_language'|trans()|desc('Modified language') }}</th>
{% block custom_column_headers %}{% endblock %}
<th>{{ 'tab.versions.table.contributor'|trans()|desc('Contributor') }}</th>
{% if not is_draft_conflict %}
<th>{{ 'tab.versions.table.created'|trans()|desc('Created') }}</th>
Expand Down Expand Up @@ -48,6 +49,7 @@
<td class="ez-table__cell">
{{ admin_ui_config.languages.mappings[version.initialLanguageCode].name }}
</td>
{% block custom_columns %}{% endblock %}
<td class="ez-table__cell">
{% if version.author is not empty %}
{{ ez_content_name(version.author) }}
Expand Down
71 changes: 71 additions & 0 deletions src/lib/Tab/AbstractEventDispatchingTab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\EzPlatformAdminUi\Tab;

use EzSystems\EzPlatformAdminUi\Tab\Event\TabViewRenderEvent;
use EzSystems\EzPlatformAdminUi\Tab\Event\TabEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Twig\Environment;

/**
* Base class representing Tab using EventDisaptcher for extensibility.
*
* It extends AbstractTab by adding Event Dispatching before rendering view.
*/
abstract class AbstractEventDispatchingTab extends AbstractTab
{
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */
protected $eventDispatcher;

/**
* @param \Twig\Environment $twig
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
*/
public function __construct(
Environment $twig,
TranslatorInterface $translator,
EventDispatcherInterface $eventDispatcher
) {
parent::__construct($twig, $translator);

$this->eventDispatcher = $eventDispatcher;
}

/**
* {@inheritdoc}
*/
public function renderView(array $parameters): string
{
$event = new TabViewRenderEvent(
$this->getIdentifier(),
$this->getTemplate(),
$this->getTemplateParameters($parameters)
);
$this->eventDispatcher->dispatch(TabEvents::TAB_RENDER, $event);

return $this->twig->render(
$event->getTemplate(),
$event->getParameters()
);
}

/**
* @return string
*/
abstract public function getTemplate(): string;

/**
* @param mixed[] $contextParameters
*
* @return mixed[]
*/
abstract public function getTemplateParameters(array $contextParameters = []): array;
}
7 changes: 7 additions & 0 deletions src/lib/Tab/Event/TabEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ class TabEvents
* Happens just before rendering tab.
*/
const TAB_PRE_RENDER = 'ezplatform.tab.pre_render';

/**
* Is dispatched on tabs extending AbstractEventDispatchingTab.
*
* Allows to manipulate template path and parameters before rendering by Twig.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we allow changing the identifier than the comment should be changed.

*/
const TAB_RENDER = 'ezplatform.tab.render';
}
75 changes: 75 additions & 0 deletions src/lib/Tab/Event/TabViewRenderEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\EzPlatformAdminUi\Tab\Event;

use Symfony\Component\EventDispatcher\Event;

class TabViewRenderEvent extends Event
{
/** @var string */
private $tabIdentifier;

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

/** @var array */
private $parameters;

/**
* @param string $tabIdentifier
* @param string $template
* @param array $parameters
*/
public function __construct(string $tabIdentifier, string $template, array $parameters = [])
{
$this->tabIdentifier = $tabIdentifier;
$this->template = $template;
$this->parameters = $parameters;
}

/**
* @return string
*/
public function getTabIdentifier(): string
{
return $this->tabIdentifier;
}

/**
* @return string
*/
public function getTemplate(): string
{
return $this->template;
}

/**
* @param string $template
*/
public function setTemplate(string $template): void
{
$this->template = $template;
}

/**
* @return array
*/
public function getParameters(): array
{
return $this->parameters;
}

/**
* @param array $parameters
*/
public function setParameters(array $parameters): void
{
$this->parameters = $parameters;
}
}
Loading