How to toggle Symfony's php-translation/symfony-bundle EditInPlace










1















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.



  1. How do I generate a link to simply reload THIS page with the extra URL parameter

  2. How do I check for that parameter within the "Activator"

or, is there a better way?










share|improve this question
























  • 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
















1















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.



  1. How do I generate a link to simply reload THIS page with the extra URL parameter

  2. How do I check for that parameter within the "Activator"

or, is there a better way?










share|improve this question
























  • 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














1












1








1








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.



  1. How do I generate a link to simply reload THIS page with the extra URL parameter

  2. How do I check for that parameter within the "Activator"

or, is there a better way?










share|improve this question
















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.



  1. How do I generate a link to simply reload THIS page with the extra URL parameter

  2. How do I check for that parameter within the "Activator"

or, is there a better way?







symfony symfony4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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













1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer





























      1














      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.






      share|improve this answer



























        1












        1








        1







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 19:51

























        answered Nov 15 '18 at 16:05









        ChadChad

        360416




        360416



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto