Get base_path in twig file -- Drupal 8 - drupal

How can i get the base path of drupal installation in twig file?
For example i need to get http://localhost/drupal/.
I have tried
{{ base_path }}
But it's returning empty.
{{ directory }}
This is returning the theme path. but i need the base path to complete the url. Is there any way to do this?

#Ditto P S To get base path on twig you declare a variable in .theme file inside a preprocess
andthen simply use 'base_path_success' variable in twig as
{{ base_path_success }}
You can declare variable inside any of the preprocess like
function ttnd_preprocess_node(&$variables) {}
OR
function ttnd_preprocess_block(&$variables) {}
Here ttnd is themename.
For more information you can use the below link
https://drupal.stackexchange.com/questions/205289/base-path-drupal-8

You can get the hostname, "localhost/drupal/", directly from the getHost() request:
$host = \Drupal::request()->getHost();
In some cases you might want to get the schema as well, fx http://localhost/drupal/:
$host = \Drupal::request()->getSchemeAndHttpHost();
Source : https://drupal.stackexchange.com/a/202811/

Related

Symfony 3 Translations Custom Domain

I'm trying to add custom domain into the project.
I have regions.locale.yaml file.
I'm trying load it in twig:
{{'united.kingdom'|trans|raw}}
But this doesn't work.
I think it has to be somehow declared that this file exists.
I found this in documentation:
// ...
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource('xlf', 'messages.fr.xlf', 'fr_FR');
$translator->addResource('xlf', 'admin.fr.xlf', 'fr_FR', 'admin');
$translator->addResource(
'xlf',
'navigation.fr.xlf',
'fr_FR',
'navigation'
);
But where should I put this to declare my regions.locale.yaml files globally?
Thanks
If you are using Symfony Standard, you don't have to declare your translation files, you just put them in app/Resources/translations.
The key is that when you want to translate using your custom domain, you just specify your domain, like this :
{{'united.kingdom'|trans({}, 'regions')|raw }}
or somewhere else in your code :
$translator->trans('united.kingdom', [], 'regions');

Error parameters Twig with Path() Symfony 2

I got error
No route found for "POST
/module/getinfo/0/0/1454306400000/1455256800000"
The code on index.html.twig:
var desde_=1454306400000;
var hasta_=1455256800000;
var url = "{{ path('module_getinfo') }}"+desde_+"/"+hasta_
to get something like this:
url = /module/getinfo/1454306400000/1455256800000
the routing.yml is:
module_getinfo:
pattern: /getinfo/{desde}/{hasta}/
defaults: { _controller: AcmeDemoBundle:Module/Module:getInfo,desde:0,hasta:0}
I want to create a custom variable on javascript, what can I do ?
Thank you !
PD. Sorry for my english, I'm still learning jejeje
If you don't pass the values of the route placeholders to the path() function, it'll use the default values (both set to 0).
If you can't pass the values, because they are only available in JavaScript, consider using string replacing techniques:
var url = "{{ path('module_getinfo', { desde: '%desde%', hasta: '%hasta%' }) }}"
.replace('%desde%', desde_)
.replace('%hasta%', hasta_)
;
A simple solution is to use what #wouter J said
But a cleaner solution is to use something like fos js routing which allows you to generate the routes from java script

Access to global parameters in Symfony2 and Twig

I would like to search some parameters in my parameters.yml file from a template in twig, depending on a variable. I have tried the following but it didn't work:
parameters.yml
twig:
globals:
status:
0: Paused
1: Running
2: Closed
template.html.twig
(game.status value can be 1, 2 or 3)
{% set var_status = game.status %}
{% set var_statustext = status.get(var_status) %}
<p>Status: {{ var_statustext }}</p>
Also I would like to access this parameters in the controller.
How could I do it? Thanks in advance.
You're looking for a way to access the value of the the global variable status (type => array) for a given key that is itself stored in another variable game.status (type => integer/string ).
Assuming game.status returns 1 ...
Then you can output Running using:
{{ attribute(status, game.status) }}
The attribute function is what you're looking for.

Get config data in Yaml (bespoke variables) in any Symfony2 controller

I want to do this in the main app/config/config.yml file:
variables:
a: 1
email: jim#email.com
variable2: hello
And this in any controller:
$variables = **get Yaml config data**
echo $variables['email'];
Of course I can't, but is anything like this possible?
I've seen where you can set global variables for access by Twig, but not seen how a Symfony2 controller can get them.
Of course if there's a better method as well then please mention that as this to me seems a good way of doing it.
Use parameters in parameters.yml for that:
parameters:
email: boss#acme.com
This way you'll be able to get it in a controller this way:
$email = $this->container->getParameter('email');
You can also create a Twig global in config.yml to have access to it from Twig using the same parameter:
twig:
globals:
email: %email%
And in Twig:
{{ email }}

Is is possible to have symfony2 log missing translation strings so that I know what needs adding to my xilff files?

I have a symfony project in which I've been through my twig templates and added {% trans %}...{% endtrans %} or adding translations like {{ title|trans }} where appropriate. I've also added a messages.de.xliff file and that is working perfectly for the few translations I have tried.
Is there a way I can get a list of strings missing from my xliff file? It's quite hard to keep track of every translation as I add it. It seems like it should log a failure to get a translation in a log file somewhere, but I've been googling a while and can't find anything.
Hi Try following May Be helpful.
https://github.com/schmittjoh/JMSTranslationBundle/blob/master/Resources/doc/index.rst
Very powerful tool and definitely takes care of you problem.
This is a very crappy patch to apply in vendor/symfony that does what I need. Probably not to be run on a production server!
diff --git a/src/Symfony/Component/Translation/MessageCatalogue.php b/src/Symfony/Component/Translation/MessageCatalogue.php
index b55676f..98a5cba 100644
--- a/src/Symfony/Component/Translation/MessageCatalogue.php
+++ b/src/Symfony/Component/Translation/MessageCatalogue.php
## -128,6 +128,8 ## class MessageCatalogue implements MessageCatalogueInterface
return $this->fallbackCatalogue->get($id, $domain);
}
+ error_log('Translation not found: "' . $id . '"');
+
return $id;
}
My solution was to overwrite the Translator and MessageCatalogue classes.
Translator:
class RegisteringTranslator extends \Symfony\Component\Translation\Translator
{
protected function loadCatalogue($locale)
{
parent::loadCatalogue($locale);
if ( ! $this->catalogues[$locale] instanceof RegisteringMessageCatalogue) {
$registeringCatalogue = new RegisteringMessageCatalogue($locale);
$registeringCatalogue->addCatalogue($this->catalogues[$locale]);
$this->catalogues[$locale] = $registeringCatalogue;
}
}
}
Catalogue:
class RegisteringMessageCatalogue extends \Symfony\Component\Translation\MessageCatalogue
{
public function get($id, $domain = 'messages')
{
if ( ! $this->has($id, $domain)) {
error_log('Translation not found: "' . $id . '"');
}
return parent::get($id, $domain);
}
}
Of course you need to use the new Translator class.
Also not very nice because it uses the protected methods and properties of Translator class. But better than changing the Symfony code directly.
I know this is an old question, but I'm posting here just in case somebody still has the same problem.
Starting from Symfony 2.6, you'll find a very nice addition to the web debug toolbar that shows how many translations you're missing.
By clicking it, the profiler will display a detailed list of missing translation.
Works out of the box, without any configuration.
Normally you should be able to use the Symfony command debug:translation via app/console.
Something like this:
$ php app/console debug:translation --only-missing <locale> <Bundle Name>
A concrete example would be:
$ php app/console debug:translation --only-missing nl AppBundle
That would output:
----------- ---------- ------------------------------------------------------------------------------------------------------- ------------------------------------------
State Domain Id Message Preview (nl)
----------- ---------- ------------------------------------------------------------------------------------------------------- ------------------------------------------
missing messages Create a clean selection Create a clean selection
missing messages New Selection New Selection
missing messages login.labels.geoserver_url login.labels.geoserver_url

Resources