I'm searching for a solution how I can implement a bundle into my base-template so that it will be visible on every page of my project without including it into every single Template/Bundle.
Is this possible? It's a language switch and I thought about building a bundle for it, so that it can interact directly to set the locale for all links etc...
Edit: Maybe I need to set it up as a service??
My problem is; how to get the language Selector into the base Template without any route ?
So at least I figured out how to handle this. Maybe its not a beauty, but for now its working so that I can checkout a better solution
public function localize_route($locale = NULL) {
// Merge query parameters and route attributes
$attributes = array_merge($this->request->query->all(), $this->request->attributes->get('_route_params'));
// Set/override locale
$attributes['_locale'] = $locale ?: \Locale::getDefault();
return $this->router->generate($this->request->attributes->get('_route'), $attributes);
}
as it's described here http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route
So that it works
Related
In Silverstripe 4 how can we use different HTMLEditor configs ?
I'd like to have two different HTMLEditorFields in the same environment.
One with all functionality and all Buttons.
And another one (MyCustomHTMLEditorField) with reduced functionality and e.g. only 3 buttons (underline, italic, bold).
How do you use ::set_active ?
How to extend HTMLEditorConfig ?
Yes, i've read the documentation but How can this be archived?
There can be multiple configs, which should always be created / accessed using HtmlEditorConfig::get(). You can then set the currently active config using set_active().
Can you provide an Example?
Any help welcome.
The documentation at https://docs.silverstripe.org/en/4/developer_guides/forms/field_types/htmleditorfield/ goes to length about how to define and use HTMLEditorConfig sets.
HTMLEditorConfig::set_active() is really for if you're intending to set an editor for use in multiple HTMLEditorFields in a given form or admin section, or to override the config for all admin sections. It more-or-less sets the 'default' config. That won't be so useful in your case, as it sounds like you want to set a configuration for a given HTMLEditorField.
Setting configuration
This is detailed in the documentation at https://docs.silverstripe.org/en/4/developer_guides/forms/field_types/htmleditorfield/#adding-and-removing-capabilities
Usually this is done in your _config.php (or in some separate class which then gets called from _config.php - unless you only need it in a very specific section, in which case you could set this up or call your special class from that admin's init() method).
The examples in the documentation here modify the default ('cms') config. This is useful if you want to modify the configuration that is used by default. So do this for your config that you've referred to as having "all functionality and all Buttons".
Setting up a new HTMLEditorConfig
Calling HTMLEditorConfig::get($configName) will get the existing config based on the name you pass it - but if there is no existing config, it will create a new one. So you could call $customConfig = HTMLEditorConfig::get('some-custom-config') - now your $customConfig variable holds a new configuration that you can configure however you so choose (insert buttons, enable plugins, define whitelisted elements, etc).
Using configuration
This is discussed in the documentation here: https://docs.silverstripe.org/en/4/developer_guides/forms/field_types/htmleditorfield/#specify-which-configuration-to-use
I've already mentioned the set_active() - if you're going to set up multiple HTMLEditorFields in a form for example you can use that in your getCMSFields() method:
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', HMLEditorField::create('Content')); // This will use the default 'cms' config.
HTMLEditorConfig::set_active('some-custom-config');
$fields->addFieldToTab('Root.Main', HMLEditorField::create('OtherContent')); // This will use the 'some-custom-config' config that you defined.
// Don't forget to reset to the default.
HTMLEditorConfig::set_active('cms');
return $fields;
}
You can also, as explained in the docs, set your config by name for a given HTMLEditorField which is preferred if you're using a non-default for one one field.
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', HMLEditorField::create('Content')); // This will use the default 'cms' config.
$fields->addFieldToTab('Root.Main', HMLEditorField::create('OtherContent')->setEditorConfig('some-custom-config')); // This will use the 'some-custom-config' config that you defined.
return $fields;
}
Note that the syntax I used here is slightly different to the docs - this allows you to avoid passing in the second and third constructor arguments that you're not changing from the defaults. Keeps things a little tidier.
Disclaimer: All of the code above was written from memory, so it may need some tweaks to work correctly.
I am trying to implemt the following instruction, as to have Enum type somehow
Shame on me, but I have not an idea on how/where I go to "register [the defined] type with Type::addType('<enummyfield>', 'MyProject\DBAL\<EnumMyfield>Type')".
EDIT Answer 1 helps. It seems I need too:
to move definition of EnumMyfield to directory MyBundle\Doctrine\DBAL\Types\Type (with appropriate use declarations)
to update app\config\config.yml with lines
types:
<myfield>: <mybundle>\Doctrine\DBAL\Types\Type\<EnumMyfield>Type
since I wish to have a Select on the Form side, to define:
->add('MyField','choice', array('label'=>'Select please', 'choices'=>array('A'=>'A','B'=>'B')), within my MyentityType\buildForm().
With respect to the last point, if I just use choices'=>array('A','B'), values for the select options are rendered as numbers (0,1), and I run into an error (I am not sure why)
your comments/advises are welcome
Just a recap, useful for others (maybe); I will highlight were you're blocked
Create a directory Doctrine\DBAL\Types
Define your new DBAL type there like shown into your link
Register it into your bundle main file (*) <--- this is what you're missing
Use it into entity definition
(*) You have a file inside your bundle named YourBundleNameBundle.php this file is used to register the bundle. If you want to register your custom type also, put inside this bundle the string Type::addType('enum', 'MyProject\DBAL\EnumType')".
So, something like
public function boot()
{
if (false === Type:hasType('enum')) {
$em = $this->container->get('doctrine.orm.entity_manager');
Type::addType('enum', 'Path\To\Bundle\Doctrine\DBAL\Types\EnumType');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum','enum');
}
}
Don't forget the use statement
use Doctrine\DBAL\Types\Type;
at the top of the file
I have a symfony 2 project that supports 2 languages in both the backend and the front end. And I have enabled translation fallback in the configuration to allow the admin to view all the database records regardless of what language he chooses to use in the backend. However, for the front end of the website I wish to disable the fallback. How can I override the translation fallback from true to false in only the controllers I want?
I am using Gedmo translatable in my entities to translate specific columns.
Thank you.
The locale of the current user is stored in the request and is accessible via the request object like this:
use Symfony\Component\HttpFoundation\Request;
public function indexAction(Request $request)
{
$locale = $request->getLocale();
$request->setLocale('en_US');
}
More details here
I think this is what you are looking for.
Using this you can override locale settings.
If you want to set fallback locale you can do that also, using:
$translator->setFallbackLocale(array('en'));
More Details given here
Hope this will help you.
Well I found it http://blog.lazycloud.net/symfony2-doctrine2-extentions-translatable/
Apparently I should add to my entity a $locale attribute and set above it the annotation * #Gedmo\Locale . and then I can use that to override the default fallback of the entity by seting the locale to whatever language I wish then calling $em->refresh($entity)
Can i create helper function like codeigniter in symfony2?
I want one function which should print array inside pre tag like
public function print_in_pre_tag($array) {
echo "<pre>";
print_r($array);
echo "</pre>";
}
I often print array like in that format to check the values.
Please suggest some solution and let me know where can i keep the function?
Edit 1: If i call like print_in_pre_tag($array); inside any controller
above function should invoke.
You should create a service (helper in codeIgniter) for that.
Create a folder called Services in your bundle. Create a file in that folder called "PrintManager.php" (or however you want to call it - but make sure the first is capital)
Then inside PrintManager.php you put:
namespace Company\MyBundle\Services;
class PrintManager {
public function print_in_pre_tag($array) {
echo "<pre>";
print_r($array);
echo "</pre>";
} }
Then in your services.yml you set the file:
parameters:
print_manager.class: Company\MyBundle\Services\PrintManager (notice, no .php extension)
services:
print_manager:
class: "%print_manager.class%"
And then in your controller you can just call it like this:
$printManager = $this->get('print_manager');
$printManager->print_in_pre_tag($array);
Btw the best thing you can do is let your service handle the functional part and let it return the result to your controller and from there you work with the results.
like: $text = $printManager->print_in_pre_tag($array);
Actually, I do not understand why people recommend Services.
Services have to contains business logic.
For you task use Helpers. They are static, and you do not need instance!
You should use the LadybugBundle which exactly does what you want. It's much more easier to use as you can debug with calls like:
ld($array);
ldd($array);
Moreover, those helpers are available anywhere in your PHP code without requesting or defining a service. Finally it also works to debug in the console, Ajax calls, Twig templates...
Actually, the question is too old to answer, and actually my answer is not about a Helper, it is about the function that you need. You need a pretty var_dump().
From Symfony 2.6, we have the VarDumper Component. You can use it wherever you need, whenever you need, in your php code, of course.
You have to use the function dump(). You can see the dump in the Developer Bar, and also in a redirection page. The output of the dump, it is better formated, because you have arrows to expand all the inners arrays.
Just to let you know guys
To be specific, I don't really know what I am doing, but I'm trying to reference $this from another file.
I'm working with Wordpress(subscribe2 plugin) and trying to get the function of subscribing to specific categories from the plugin and putting it into my own signup form.
The ability to do this is already in the settings for the plugin, so I'd like to put them in my own form, but they look like this:
$this->get_usermeta_keyname('s2_subscribed')
so my question: How do I reference "$this" in my own file? I have been googling it all morning, but all of my results see "$this" as "this" and don't show me what I want.
$this refers to the current class context. So if you have a class named MyClass, within that class you can access functions and elements using $this. Outside of that class, $this has no context and won't work, but if you have instantiated the class and if the functions or variables are public, then you can access them via a reference to the class.
Example:
<?php
Class MyClass {
$class_var = "Class Var Value";
function class_func_1() {
print $this->class_var;
$this->class_func_2();
}
function class_func_2() {
print "Class Func 2";
}
}
?>
So the dummy class above uses $this to reference its own elements. But outside the class you cannot reference them with this. But if we instantiate an instance of the class, we can access them by the reference to the instance of the class:
<?php
print $this->class_var; // Fails miserably
$this->class_func_1(); // Also fails
$class_instance = new MyClass();
print $class_instance->class_var; // Var access works
$class_instance->class_func_1(); // Call method works
?>
Since this is a wordpress function, I'm curious if you are trying to access something in a plugin (which might use a class) or possibly something in a purchased theme. Either way, there should be a reference to the instance of the class somewhere in the file. Once you have this variable, then you can access the actions and contents of the class.
With more information, I think we can probably provide better direction...
Update:
Ok, it appears you are using the subscribe2 plugin. At the bottom of the plugin file (subscribe2.php) is this line:
$mysubscribe2 = new subscribe2();
What this is doing is creating a global variable that points to an instance of the class. So instead of using $this->method_name() you could use $mysubscribe2->method_name().
However, looking at the file you posted - it almost looks like you grabbed a portion of the plugin code and just put it into a template. I'm not sure what you are trying to accomplish but from here it appears you are heading down a rough path...