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

[NodeBundle] link select language problem #1159

Closed
slaci opened this issue May 13, 2016 · 3 comments
Closed

[NodeBundle] link select language problem #1159

slaci opened this issue May 13, 2016 · 3 comments

Comments

@slaci
Copy link
Contributor

slaci commented May 13, 2016

The url chooser works only with multilanguage sites now: https://github.com/Kunstmaan/KunstmaanBundlesCMS/blob/master/src/Kunstmaan/NodeBundle/Resources/views/Widgets/selectLink.html.twig#L83

In this file: data-base-url={{ "/" ~ app.request.locale }} is the problematic piece, cause locale is not needed in the url on single language settings.

The fix should be simple, just {{ multilanguage ? "/" ~ app.request.locale : '' }}, but that multilanguage parameter is not accessible in twig (and cant access the container either).

The other parameters are defined in config.yml, which is in a different package, and putting the multilanguage: %multilanguage% line there would be very easy but also a bc break.

So, what do you think, is it okey to fix this way or have a better idea?


Other approach would be a prod url generator, which just replaces the scriptname from the generated url (this is big hax and not recommended). A twig extension in NodeTwigExtension would be for this (with needs_context => true):

public function getProdPathForNodeUrl($context, $urlOrNode, $locale = null, $routeParams = array(), $relative = false, $prodEnv = 'prod')
    {
        $request = $this->requestStack->getMasterRequest();

        if ($urlOrNode instanceof Node) {
            $urlOrNode->getNodeTranslation($locale);
        }

        if ($urlOrNode instanceof NodeTranslation) {
            $locale = $locale ?: $urlOrNode->getLang();
            $urlOrNode = $urlOrNode->getUrl();
        }

        $routeParams = array_merge($routeParams, array(
            'url'       => $urlOrNode,
            '_locale'   => $locale ?: $request->attributes->get('_locale'),
        ));

        $path = $this->generator->generate(
            '_slug',
            $routeParams,
            $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH
        );

        if ($context['app']->getEnvironment() !== $prodEnv) {
            $path = str_replace($request->getScriptName(), '', $path);
        }

        return $path;
    }
@jockri
Copy link
Contributor

jockri commented May 13, 2016

I think we can even do this for getting the base url:

{{ path('_slug', {'url': ''}) }}

@slaci
Copy link
Contributor Author

slaci commented May 13, 2016

But when some1 select the link in dev mode, then the base will be /app_dev.php/ and that will be saved I guess. This is why i posted that ugly scriptname remove hack (but still don't like it).

@iknowfoobar
Copy link
Contributor

I just submitted a pull request that fixes this #1224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants