How to toggle Symfony's php-translation/symfony-bundle EditInPlace
I followed this documentation for Edit In Place, and setup the Activator, and it works!
However, I will be using this on the production site and allowing access via a ROLE_TRANSLATOR
Authorization. This is also working, but I don't want the web interface always "on"
How would I go about enabling it via some sort of link or toggle?
My thoughts, it would be simple to just add a URL parameter, like ?trans=yes
and then in the activator;
return ($this->authorizationChecker->isGranted(['ROLE_TRANSLATOR']) && $_GET['trans'] == 'yes');
Obviously, $_GET
would not work, I didn't even try.
- How do I generate a link to simply reload THIS page with the extra URL parameter
- How do I check for that parameter within the "Activator"
or, is there a better way?
symfony symfony4
add a comment |
I followed this documentation for Edit In Place, and setup the Activator, and it works!
However, I will be using this on the production site and allowing access via a ROLE_TRANSLATOR
Authorization. This is also working, but I don't want the web interface always "on"
How would I go about enabling it via some sort of link or toggle?
My thoughts, it would be simple to just add a URL parameter, like ?trans=yes
and then in the activator;
return ($this->authorizationChecker->isGranted(['ROLE_TRANSLATOR']) && $_GET['trans'] == 'yes');
Obviously, $_GET
would not work, I didn't even try.
- How do I generate a link to simply reload THIS page with the extra URL parameter
- How do I check for that parameter within the "Activator"
or, is there a better way?
symfony symfony4
I think I may scrap this question, maybe, but I belive this is what I am looking for referencing-services as Twig global Variables Now I just need to learn more about how services work!
– Chad
Nov 14 '18 at 20:03
add a comment |
I followed this documentation for Edit In Place, and setup the Activator, and it works!
However, I will be using this on the production site and allowing access via a ROLE_TRANSLATOR
Authorization. This is also working, but I don't want the web interface always "on"
How would I go about enabling it via some sort of link or toggle?
My thoughts, it would be simple to just add a URL parameter, like ?trans=yes
and then in the activator;
return ($this->authorizationChecker->isGranted(['ROLE_TRANSLATOR']) && $_GET['trans'] == 'yes');
Obviously, $_GET
would not work, I didn't even try.
- How do I generate a link to simply reload THIS page with the extra URL parameter
- How do I check for that parameter within the "Activator"
or, is there a better way?
symfony symfony4
I followed this documentation for Edit In Place, and setup the Activator, and it works!
However, I will be using this on the production site and allowing access via a ROLE_TRANSLATOR
Authorization. This is also working, but I don't want the web interface always "on"
How would I go about enabling it via some sort of link or toggle?
My thoughts, it would be simple to just add a URL parameter, like ?trans=yes
and then in the activator;
return ($this->authorizationChecker->isGranted(['ROLE_TRANSLATOR']) && $_GET['trans'] == 'yes');
Obviously, $_GET
would not work, I didn't even try.
- How do I generate a link to simply reload THIS page with the extra URL parameter
- How do I check for that parameter within the "Activator"
or, is there a better way?
symfony symfony4
symfony symfony4
edited Nov 15 '18 at 15:34
Chad
asked Nov 13 '18 at 20:56
ChadChad
360416
360416
I think I may scrap this question, maybe, but I belive this is what I am looking for referencing-services as Twig global Variables Now I just need to learn more about how services work!
– Chad
Nov 14 '18 at 20:03
add a comment |
I think I may scrap this question, maybe, but I belive this is what I am looking for referencing-services as Twig global Variables Now I just need to learn more about how services work!
– Chad
Nov 14 '18 at 20:03
I think I may scrap this question, maybe, but I belive this is what I am looking for referencing-services as Twig global Variables Now I just need to learn more about how services work!
– Chad
Nov 14 '18 at 20:03
I think I may scrap this question, maybe, but I belive this is what I am looking for referencing-services as Twig global Variables Now I just need to learn more about how services work!
– Chad
Nov 14 '18 at 20:03
add a comment |
1 Answer
1
active
oldest
votes
The "proper" way to do this, as I have discovered more about "services" is to do the logic diectly in the RoleActivator.php file.
referencing documentation for How to Inject Variables into all Templates via Referencing Services I came up with the following solution;
src/Security/RoleActivator.php
<?php
namespace AppSecurity;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreAuthorizationAuthorizationCheckerInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationCredentialsNotFoundException;
use TranslationBundleEditInPlaceActivatorInterface;
use SymfonyComponentTranslationTranslatorInterface;
use SymfonyComponentHttpFoundationRequestStack;
class RoleActivator implements ActivatorInterface
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* @var TranslatorInterface
*/
private $translate;
/**
* @var RequestStack
*/
private $request;
private $params;
private $path;
private $flag = null;
public function __construct(AuthorizationCheckerInterface $authorizationChecker, TranslatorInterface $translate, RequestStack $request)
$this->authorizationChecker = $authorizationChecker;
$this->translate = $translate;
$this->request = $request;
/**
* @inheritdoc
*/
public function checkRequest(Request $request = null)
if ($this->flag === null) $this->setFlag($request);
try
return ($this->authorizationChecker->isGranted(['ROLE_TRANSLATOR']) && $this->flag);
catch (AuthenticationCredentialsNotFoundException $e)
return false;
public function getText()
if ($this->flag === null) $this->setFlag();
return ($this->flag) ? 'linkText.translate.finished' : 'linkText.translate.start'; // Translation key's returned
public function getHref()
if ($this->flag === null) $this->setFlag();
$params = $this->params;
if ($this->flag)
unset($params['trans']);
else
$params['trans'] = 'do';
$queryString = '';
if (!empty($params))
$queryString = '?';
foreach ($params as $key => $value)
$queryString.= $key.'='.$value.'&';
$queryString = rtrim($queryString, '&');
return $this->path.$queryString;
private function setFlag(Request $request = null)
if ($request === null)
$request = $this->request->getCurrentRequest();
$this->flag = $request->query->has('trans');
$this->params = $request->query->all();
$this->path = $request->getPathInfo();
configpackagestwig.yaml
twig:
# ...
globals:
EditInPlace: '@EditInPlace_RoleActivator'
configservices.yaml
services:
# ...
EditInPlace_RoleActivator:
class: AppSecurityRoleActivator
arguments: ["@security.authorization_checker"]
So What I added over and above the php-translation example is the getText
and getHref
methods and corresponding private
variables being set in the checkRequest
and read there after.
Now in my twig template (in the header) I just use
% if is_granted('ROLE_TRANSLATOR') %
<a href=" EditInPlace.Href "> EditInPlace.Text </a>
% endif %
Add the new keys to the translation files and your done. the trans=do
query parameter is toggled on and off with each click of the link. You could even add toggling styles with a class name, just copy the getText
method to something like getClass
and return string a
or b
with the Ternary.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289369%2fhow-to-toggle-symfonys-php-translation-symfony-bundle-editinplace%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The "proper" way to do this, as I have discovered more about "services" is to do the logic diectly in the RoleActivator.php file.
referencing documentation for How to Inject Variables into all Templates via Referencing Services I came up with the following solution;
src/Security/RoleActivator.php
<?php
namespace AppSecurity;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreAuthorizationAuthorizationCheckerInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationCredentialsNotFoundException;
use TranslationBundleEditInPlaceActivatorInterface;
use SymfonyComponentTranslationTranslatorInterface;
use SymfonyComponentHttpFoundationRequestStack;
class RoleActivator implements ActivatorInterface
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* @var TranslatorInterface
*/
private $translate;
/**
* @var RequestStack
*/
private $request;
private $params;
private $path;
private $flag = null;
public function __construct(AuthorizationCheckerInterface $authorizationChecker, TranslatorInterface $translate, RequestStack $request)
$this->authorizationChecker = $authorizationChecker;
$this->translate = $translate;
$this->request = $request;
/**
* @inheritdoc
*/
public function checkRequest(Request $request = null)
if ($this->flag === null) $this->setFlag($request);
try
return ($this->authorizationChecker->isGranted(['ROLE_TRANSLATOR']) && $this->flag);
catch (AuthenticationCredentialsNotFoundException $e)
return false;
public function getText()
if ($this->flag === null) $this->setFlag();
return ($this->flag) ? 'linkText.translate.finished' : 'linkText.translate.start'; // Translation key's returned
public function getHref()
if ($this->flag === null) $this->setFlag();
$params = $this->params;
if ($this->flag)
unset($params['trans']);
else
$params['trans'] = 'do';
$queryString = '';
if (!empty($params))
$queryString = '?';
foreach ($params as $key => $value)
$queryString.= $key.'='.$value.'&';
$queryString = rtrim($queryString, '&');
return $this->path.$queryString;
private function setFlag(Request $request = null)
if ($request === null)
$request = $this->request->getCurrentRequest();
$this->flag = $request->query->has('trans');
$this->params = $request->query->all();
$this->path = $request->getPathInfo();
configpackagestwig.yaml
twig:
# ...
globals:
EditInPlace: '@EditInPlace_RoleActivator'
configservices.yaml
services:
# ...
EditInPlace_RoleActivator:
class: AppSecurityRoleActivator
arguments: ["@security.authorization_checker"]
So What I added over and above the php-translation example is the getText
and getHref
methods and corresponding private
variables being set in the checkRequest
and read there after.
Now in my twig template (in the header) I just use
% if is_granted('ROLE_TRANSLATOR') %
<a href=" EditInPlace.Href "> EditInPlace.Text </a>
% endif %
Add the new keys to the translation files and your done. the trans=do
query parameter is toggled on and off with each click of the link. You could even add toggling styles with a class name, just copy the getText
method to something like getClass
and return string a
or b
with the Ternary.
add a comment |
The "proper" way to do this, as I have discovered more about "services" is to do the logic diectly in the RoleActivator.php file.
referencing documentation for How to Inject Variables into all Templates via Referencing Services I came up with the following solution;
src/Security/RoleActivator.php
<?php
namespace AppSecurity;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreAuthorizationAuthorizationCheckerInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationCredentialsNotFoundException;
use TranslationBundleEditInPlaceActivatorInterface;
use SymfonyComponentTranslationTranslatorInterface;
use SymfonyComponentHttpFoundationRequestStack;
class RoleActivator implements ActivatorInterface
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* @var TranslatorInterface
*/
private $translate;
/**
* @var RequestStack
*/
private $request;
private $params;
private $path;
private $flag = null;
public function __construct(AuthorizationCheckerInterface $authorizationChecker, TranslatorInterface $translate, RequestStack $request)
$this->authorizationChecker = $authorizationChecker;
$this->translate = $translate;
$this->request = $request;
/**
* @inheritdoc
*/
public function checkRequest(Request $request = null)
if ($this->flag === null) $this->setFlag($request);
try
return ($this->authorizationChecker->isGranted(['ROLE_TRANSLATOR']) && $this->flag);
catch (AuthenticationCredentialsNotFoundException $e)
return false;
public function getText()
if ($this->flag === null) $this->setFlag();
return ($this->flag) ? 'linkText.translate.finished' : 'linkText.translate.start'; // Translation key's returned
public function getHref()
if ($this->flag === null) $this->setFlag();
$params = $this->params;
if ($this->flag)
unset($params['trans']);
else
$params['trans'] = 'do';
$queryString = '';
if (!empty($params))
$queryString = '?';
foreach ($params as $key => $value)
$queryString.= $key.'='.$value.'&';
$queryString = rtrim($queryString, '&');
return $this->path.$queryString;
private function setFlag(Request $request = null)
if ($request === null)
$request = $this->request->getCurrentRequest();
$this->flag = $request->query->has('trans');
$this->params = $request->query->all();
$this->path = $request->getPathInfo();
configpackagestwig.yaml
twig:
# ...
globals:
EditInPlace: '@EditInPlace_RoleActivator'
configservices.yaml
services:
# ...
EditInPlace_RoleActivator:
class: AppSecurityRoleActivator
arguments: ["@security.authorization_checker"]
So What I added over and above the php-translation example is the getText
and getHref
methods and corresponding private
variables being set in the checkRequest
and read there after.
Now in my twig template (in the header) I just use
% if is_granted('ROLE_TRANSLATOR') %
<a href=" EditInPlace.Href "> EditInPlace.Text </a>
% endif %
Add the new keys to the translation files and your done. the trans=do
query parameter is toggled on and off with each click of the link. You could even add toggling styles with a class name, just copy the getText
method to something like getClass
and return string a
or b
with the Ternary.
add a comment |
The "proper" way to do this, as I have discovered more about "services" is to do the logic diectly in the RoleActivator.php file.
referencing documentation for How to Inject Variables into all Templates via Referencing Services I came up with the following solution;
src/Security/RoleActivator.php
<?php
namespace AppSecurity;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreAuthorizationAuthorizationCheckerInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationCredentialsNotFoundException;
use TranslationBundleEditInPlaceActivatorInterface;
use SymfonyComponentTranslationTranslatorInterface;
use SymfonyComponentHttpFoundationRequestStack;
class RoleActivator implements ActivatorInterface
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* @var TranslatorInterface
*/
private $translate;
/**
* @var RequestStack
*/
private $request;
private $params;
private $path;
private $flag = null;
public function __construct(AuthorizationCheckerInterface $authorizationChecker, TranslatorInterface $translate, RequestStack $request)
$this->authorizationChecker = $authorizationChecker;
$this->translate = $translate;
$this->request = $request;
/**
* @inheritdoc
*/
public function checkRequest(Request $request = null)
if ($this->flag === null) $this->setFlag($request);
try
return ($this->authorizationChecker->isGranted(['ROLE_TRANSLATOR']) && $this->flag);
catch (AuthenticationCredentialsNotFoundException $e)
return false;
public function getText()
if ($this->flag === null) $this->setFlag();
return ($this->flag) ? 'linkText.translate.finished' : 'linkText.translate.start'; // Translation key's returned
public function getHref()
if ($this->flag === null) $this->setFlag();
$params = $this->params;
if ($this->flag)
unset($params['trans']);
else
$params['trans'] = 'do';
$queryString = '';
if (!empty($params))
$queryString = '?';
foreach ($params as $key => $value)
$queryString.= $key.'='.$value.'&';
$queryString = rtrim($queryString, '&');
return $this->path.$queryString;
private function setFlag(Request $request = null)
if ($request === null)
$request = $this->request->getCurrentRequest();
$this->flag = $request->query->has('trans');
$this->params = $request->query->all();
$this->path = $request->getPathInfo();
configpackagestwig.yaml
twig:
# ...
globals:
EditInPlace: '@EditInPlace_RoleActivator'
configservices.yaml
services:
# ...
EditInPlace_RoleActivator:
class: AppSecurityRoleActivator
arguments: ["@security.authorization_checker"]
So What I added over and above the php-translation example is the getText
and getHref
methods and corresponding private
variables being set in the checkRequest
and read there after.
Now in my twig template (in the header) I just use
% if is_granted('ROLE_TRANSLATOR') %
<a href=" EditInPlace.Href "> EditInPlace.Text </a>
% endif %
Add the new keys to the translation files and your done. the trans=do
query parameter is toggled on and off with each click of the link. You could even add toggling styles with a class name, just copy the getText
method to something like getClass
and return string a
or b
with the Ternary.
The "proper" way to do this, as I have discovered more about "services" is to do the logic diectly in the RoleActivator.php file.
referencing documentation for How to Inject Variables into all Templates via Referencing Services I came up with the following solution;
src/Security/RoleActivator.php
<?php
namespace AppSecurity;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreAuthorizationAuthorizationCheckerInterface;
use SymfonyComponentSecurityCoreExceptionAuthenticationCredentialsNotFoundException;
use TranslationBundleEditInPlaceActivatorInterface;
use SymfonyComponentTranslationTranslatorInterface;
use SymfonyComponentHttpFoundationRequestStack;
class RoleActivator implements ActivatorInterface
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* @var TranslatorInterface
*/
private $translate;
/**
* @var RequestStack
*/
private $request;
private $params;
private $path;
private $flag = null;
public function __construct(AuthorizationCheckerInterface $authorizationChecker, TranslatorInterface $translate, RequestStack $request)
$this->authorizationChecker = $authorizationChecker;
$this->translate = $translate;
$this->request = $request;
/**
* @inheritdoc
*/
public function checkRequest(Request $request = null)
if ($this->flag === null) $this->setFlag($request);
try
return ($this->authorizationChecker->isGranted(['ROLE_TRANSLATOR']) && $this->flag);
catch (AuthenticationCredentialsNotFoundException $e)
return false;
public function getText()
if ($this->flag === null) $this->setFlag();
return ($this->flag) ? 'linkText.translate.finished' : 'linkText.translate.start'; // Translation key's returned
public function getHref()
if ($this->flag === null) $this->setFlag();
$params = $this->params;
if ($this->flag)
unset($params['trans']);
else
$params['trans'] = 'do';
$queryString = '';
if (!empty($params))
$queryString = '?';
foreach ($params as $key => $value)
$queryString.= $key.'='.$value.'&';
$queryString = rtrim($queryString, '&');
return $this->path.$queryString;
private function setFlag(Request $request = null)
if ($request === null)
$request = $this->request->getCurrentRequest();
$this->flag = $request->query->has('trans');
$this->params = $request->query->all();
$this->path = $request->getPathInfo();
configpackagestwig.yaml
twig:
# ...
globals:
EditInPlace: '@EditInPlace_RoleActivator'
configservices.yaml
services:
# ...
EditInPlace_RoleActivator:
class: AppSecurityRoleActivator
arguments: ["@security.authorization_checker"]
So What I added over and above the php-translation example is the getText
and getHref
methods and corresponding private
variables being set in the checkRequest
and read there after.
Now in my twig template (in the header) I just use
% if is_granted('ROLE_TRANSLATOR') %
<a href=" EditInPlace.Href "> EditInPlace.Text </a>
% endif %
Add the new keys to the translation files and your done. the trans=do
query parameter is toggled on and off with each click of the link. You could even add toggling styles with a class name, just copy the getText
method to something like getClass
and return string a
or b
with the Ternary.
edited Nov 16 '18 at 19:51
answered Nov 15 '18 at 16:05
ChadChad
360416
360416
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289369%2fhow-to-toggle-symfonys-php-translation-symfony-bundle-editinplace%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think I may scrap this question, maybe, but I belive this is what I am looking for referencing-services as Twig global Variables Now I just need to learn more about how services work!
– Chad
Nov 14 '18 at 20:03