Pass array from template to partial - templates

My showAction() looks like this:
public function showAction() {
$form = $this->formRepository->findByUid($this->settings['form']);
$arguments = $this->request->getArguments();
$this->view->assignMultiple(
array(
'form' => $form,
'arguments' => $arguments
)
);
}
In the template of this action I try to give 'arguments' to all my partials:
<f:render partial="FormElement/{formElement.type}" arguments="{formElement:formElement, arguments:arguments}" />
The debug of {arguments} in the template shows me that everything is fine. If I debug the {arguments} inside the partials, it returns NULL. Even if I give all arguments to the partials (arguments="{_all}"), NULL will be returned by debugging {arguments} inside the partials.
Everything else works fine!

Your code looks fine to me. You might want to check your php.ini file for the following setting "max_input_nesting_level". Sometimes, if this parameter is set to a low value and your handling a complex object you run into this restriction. You might want to change it to 128 oder even 256. (Don't forget to restart your apache after changing your php.ini)

Related

Get name of next doctrine migration

How could I get the name / version of the next migration to execute? Something similar to migrations:latest but more like migrations:next. I need this as input to another command so it needs to be parseable output (can't really just use migrations:status).
You can use the Configuration object of the Doctrine migrations bundle. This is even (somewhat) documented as custom configuration.
Here is a minimal code example that works for me:
public function migrationVersionAction(EntityManagerInterface $em, ParameterBagInterface $parameters) {
$connection = $em->getConnection();
$configuration = new \Doctrine\Migrations\Configuration\Configuration($connection);
$configuration->setMigrationsNamespace($parameters->get('doctrine_migrations.namespace'));
$configuration->setMigrationsDirectory($parameters->get('doctrine_migrations.dir_name'));
$configuration->setMigrationsTableName($parameters->get('doctrine_migrations.table_name'));
return new JsonResponse([
'prev' => $configuration->resolveVersionAlias('prev'),
'current' => $configuration->resolveVersionAlias('current'),
'next' => $configuration->resolveVersionAlias('next'),
'latest' => $configuration->resolveVersionAlias('latest')
]);
}
You might want to set the remaining parameters as well though, especially if they differ from the defaults. For this, the configuration documentation might help in addition to the link above.

Symfony 3.2 - set environment variables in runtime [duplicate]

In my config.yml I have this:
parameters:
gitek.centro_por_defecto: 1
Now, I want to change this value from my controller using a form, like this:
public function seleccionAction(Request $request)
{
$entity = new Centro();
$form = $this->createForm(new SeleccionType(), $entity);
$centro = $this->container->getParameter('gitek.centro_por_defecto');
if ($this->getRequest()->getMethod() == 'POST') {
$form->bind($this->getRequest());
if ($form->isValid()) {
$miseleccion = $request->request->get('selecciontype');
$this->container->setParameter('gitek.centro_por_defecto', $miseleccion['nombre']);
// return $this->redirect($this->generateUrl('admin_centro'));
}
}
return $this->render('BackendBundle:Centro:seleccion.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
I´m getting Impossible to call set() on a frozen ParameterBag. error all the time.
Any help or clue?
You can't modify Container once it has been compiled, which is done before invoking the controller.
The DIC parameters are intended for configuration purposes - not a replacement for global variables. In addition it seems you want to persist some kind of permanent modification. In that case consider using session if it's a per-user modification or persisting it (e.g. into DB) if it's supposed to be application-wide.
If you need to modify DIC parameters or services, you can do so using a compiler pass. More info on how to write custom compiler passes can be found at:
http://symfony.com/doc/master/cookbook/service_container/compiler_passes.html
You can set $_ENV variables and get that after
putenv("VAR=1");
And to get
getenv("VAR");

Can you make WooCommerce shortcodes from template parts?

I'm interested in creating shortcodes that basically fill in template parts of the WooCommerce checkout. For example, in functions.php of my child theme:
function shortcode_review_order() {
//get the template part from woocommerce/templates/checkout/review-order.php
wc_get_template_part('checkout/review-order');
}
add_shortcode( 'custom_review_order', 'shortcode_review_order' );
...and then in my page...
<div>[custom_review_order]</div>
When I tried this, nothing appeared in my checkout page.
Is this even possible?
there are few things wrong in your code...
first, you should add a shortcode using the init hook.
add_action( 'init', 'add_shortcodes' );
function add_shortcodes(){
add_shortcode( 'custom_review_order', 'shortcode_review_order' );
}
then you lack the .php part of the template. Also it needs the array parameter like below. And you might get more accurate result using wc_get_template.
function shortcode_review_order(){
wc_get_template( 'checkout/review-order.php', array( 'checkout' => WC()->checkout() ) );
}
To know more on how to correctly use it's template, search for each on the plugin. You'll see how it's being used. And you can get a hint on how you can use it for yourself.
I found that wc_get_template echos out the template, instead, for shortcodes returning the template is better. You can use:
$string = wc_get_template_html( $template_name, $args, $template_path, $default_path );
It is "Like wc_get_template, but returns the HTML instead of outputting."
https://docs.woocommerce.com/wc-apidocs/function-wc_get_template_html.html

how could I add a post var as a param of a template tag in magento?

Inside the template account_new_confirmation, which is the template of the confirmation mail the user receives after he creates a new account, I need to add a param inside the tag
{{store url="customer/account/confirm/" _query_id=$customer.id _query_key=$customer.confirmation _query_back_url=$back_url _query_myparam="TEST"}}
this is working for a string producing the following link to be written inside the confirmation mail as expected:
http://baseurl/customer/account/confirm/?id=12345&key=donkey&back_url=monkey&myparam=TEST
but I cannot figure out how to replace the string TEST with the value of a param I have in request post.
I mean the user reach this point after having filled a form sent with POST action. Inside this form I have a checkbox named FOO and I need to add its value (true or false) to _query_myparam on my example above.
I tried with
_query_param=$foo
and with
_query_param=$this->getRequest()->getPost('foo')
but they both were too easy to work.
Anyone knows how to solve this?
I found it myself :D
You have to change the method sendNewAccountEmail of the class Mage_Customer_Model_Customer which you can find in app/code/core/Mage/Customer/Model/Customer.php
You need to add new variables available inside the template inside:
array('customer' => $this, 'back_url' => $backUrl)
So for your need this would be changed in:
array('customer' => $this, 'back_url' => $backUrl, 'foo' => Mage::app()->getRequest()->getPost('foo'))
Then you can add this variable to the template param as
{{store url="customer/account/confirm/" _query_id=$customer.id _query_key=$customer.confirmation _query_back_url=$back_url _query_myparam=$foo}}
This will produce the following link:
http://baseurl/customer/account/confirm/?id=12345&key=donkey&back_url=monkey&myparam=on
when checkbox FOO is checked.
Thanks

Drupal preprocess function and session

I set a session var at template preprocess function in a theme I use, but the first time I open the site I cant read the session var, if I refresh the page it works fine, anybody know what can be the problem??
This is the code I use in the preprocess function:
function m_preprocess(&$vars, $hook) {
$default_location = array(
'country_code' => 'ec',
'province_code' => 'p',
'province' => 'Pichincha',
'city' => 'Quito',
'city_path' => 'lugares/u/ec/p/*'
);
if (isset($_COOKIE['proximity_path'])) $default_location['proximity_path'] = $_COOKIE['proximity_path'];
$default_location['path'] = isset($_COOKIE['sort-by']) && $_COOKIE['sort-by']=='proximity'? $_COOKIE['proximity_path'] : $default_location['city_path'];
$_SESSION['location'] = $default_location;
}
A couple of things:
Try dsm($_SESSION); to see what is the var content when the site first load.
I don't know where you create $_COOKIE['proximity_path'], but it is not in the code you show.
Check for typos
The template pre-process function is called before node.tpl.php (that's why it is called pre-process) and that is why the value of the $_SESSION variable is not available in the template preprocess function.
Like you pointed out, it works after the first page load. This happens when only after $_SESSION variable is set in the node body (using the PHP filter)