Symfony2 and Twig Dump Issue - symfony

I am running into an issues with dump() in Twig.
I am not able to completely dump the values of the object that I am returning to my twig template. My object, as defined below, is built up of a product object, qty key/val, OnOrder key/val and avgUnitCost key/val.
I AM able to use dump(qty), dump(OnOrder), dump(avgUnitCost) and see the values of these.
I AM NOT able to use dump() on product to see the key/val of the product object. All I get is a white page of death.
I have read elsewhere on stack that it is a memory issue in the php.ini file. This does not seem to fix the issue, I set mine 1024M and it still times out and gives me the white screen.
I have also read this guys article on the same issue: http://hectorpinol.com/twig-debug-in-symfony-2/ ... He thinks it is a "bidirectional association problem".
In any case, here is the code that I am using to pass the object and render my twig template...
return $this->render('TestBundle:Event:view.html.twig', array(
'heading' => 'View Product',
'product' => $product,
'qty' => $qty,
'OnOrder' => $OnOrder,
'avgUnitCost' => $avgUnitCost,
));
Here is the guts of my question:
How can one effectively use twig to access the elements of an object, whether it be dump or some other method. I need to be able to see all of the elements in the object so that I can place them on the page as I need.
Thanks so much for your help!!!

Check LadybugBundle. You can dump everything.

Try adding a break point in twig_var_dump:
/vendor/twig/twig/lib/Twig/Extension/Debug.php (at the bottom)
Then you can use the functionality of your debugger...

Look at this answer: https://stackoverflow.com/a/29302069/4102223
It is my approach to solve this problem, only few lines must to be changed in one place (it is easier because no need to include new bundles and read its documentation).

Related

Stop filtering WordPress function wp_insert_post

I have to make a few minor changes to a website built in WordPress, and I came across some problems with the wp_insert_content function. This function sanatizes input, taking some HTML tags completely out (like ) and deleting attributes on others. I would like to switch this off, and everywhere I find a link to http://pp19dd.com/2010/06/unfiltered-wp_insert_post/ where the solution is to add
'filter' => true
to the posts input array.
This article is from 2010 though, and from what I found an extra line was added in 2011 (http://web.archiveorange.com/archive/v/TDTh42SUwDEc1GFmSrvU). This line reads:
unset( $postarr[ 'filter' ] );
and is called right after merging the input with the defaults, and before sanitizing. It seems to me that this line cancels the 'filter' => true statement above. Indeed, sanitizing happens if the line is there, and disappears if the line is taken out.
So the easy solution would be to add the 'filter' => true and delete the extra line in the function. Problem though is that I have no idea where else the function is used, and I wonder if it's smart at all to hack right in the WP code. An update of WP would restore it anyway. So ... is there any other way to stop this function from sanitizing the input?
It is possible that some other plugin may be trying to sanitize the content, in such case remove all the filters is the only option, try the following:
remove_all_filters("content_save_pre");
$post_id = wp_insert_post($post);
Other possible filter tags are:
pre_content
pre_post_content
content_pre
Try them one by one also if it does not work.
i'm also in the process of importing posts with s in the content body.
i've been searching all morning for a solution, there have been some discussions on the topic. so far
kses_remove_filters();
has done nothing for me, neither did commenting these two lines:
unset( $postarr[ 'filter' ] ); // overrides 'filter' => true since 3.0 i guess
$postarr = sanitize_post($postarr, 'db');
in wp_insert_post() in post.php (defaults at line 2704).
there's another interesting bit at line 2874:
$data = apply_filters('wp_insert_post_data', $data, $postarr);
which seems like could be the source of our troubles, but i'm too much of a newbie to figure this out on my own.
really, truly, desperately need help! oh, and i can't comment, sorry for the spam action.
i would like to keep this thread open and find a solution.
I guess it worked before i posted, but the content i was importing already had HTML tags stripped
the easiest workaround would be to manually overwrite the content after inserting the post, but this only works for isolated environments or for importing
$wpdb->update($wpdb->posts, array("post_content"=>$content->content), array("ID"=>$postid), array("%s"), array("%d"));
anyhow,
remove_all_filters("content_save_pre");
did the trick!

What do i put in get_permalink(get_option(' here???')

I'm trying to work out what the option value is in get_permalink(get_option('jr_dashboard_page_id') I'm trying to follow this convention but cant work where it comes from - dashboard is the post-name in the DB what the rest of it means or how its works I cant figure out...
Any help would be great..
The Options API stores key-value pairs. For example, add_option( 'key', 'value' ); could be used. In that case, get_option( 'key' ); would return value.
So at some point a plugin or the themes that you use defined jr_dashboard_page_id to be something. Perhaps via a meta box or something like that. There is not enough information in this question to say exactly what "key" you need. It's like saying, I have an associative array $array['key'] what key should I use? We simply can't know because we don't know how array looks and we don't know what kind of value you expect.

Accessing values in Drupal's $form_values from a custom Drupal Form

EDIT: Seems like my "array-crawling" skills were not enough, thanks for the suggestions.
Moreover, I found out that I was checking the $discounttype condition with a plain "=" instead of a double "==". I guess banging your head on the same block of code for 3 hours makes you dumb and miss the most obvious errors.
First thing first, I'm on Drupal 6.
I have created a form with the following markup:
$form["cart_".$index] = array(
'#type' => 'image_button',
'#src'=> 'files/imghome/sidebar-add-demo.gif',
'#attributes' => array('rel' => '#item', 'class' => 'buybutton', 'title' => $discounttype),
'#prefix'=>'<p class="renewprop">'.$newren.' for '.$node_abb->field_tipo_abb_value.':</p><p class="renewblock"><span class="pricetag">'.$node_abb->field_prezzo_value.''.$discounttype.'</span>',
'#suffix' =>'</p>' ,
'#submit' =>array('usercp_form_submit'),
);
The form renders correctly, as you can see from this picture: http://cl.ly/3D2C2h1t1m2B351L1T31
(the N and R values beside the € symbol are actually the value of the $discounttype variable, just for checking it)
Each white box is basically an istance of the beforementioned form.
I need to pass the value of the $discounttype variable on each submit, so I decided to set it as the title of the submit button.
My problem is that in the submit function itself I cannot access the value of the 'title' attribute contained in the #attributes array. Mainly because probably I don't know the right syntax.
So far I've tried
$foo = $form_values['attributes']['title'];
$foo = $form_values['#attributes']['title'];
$foo = $form_values['attributes']['#title'];
And every other possible combination, but probably I'm just doing it wrong.
It's actually an hour that I'm crawling the web searching for an asnwer but I came up with anything.
first, you should mention form element ID.
so, you can access submit button by $form_state["cart_".$index]['#attributes']['title'];
but actually, why don't you use hidden field ('#type' => 'hidden') ?
I believe you have to use $form_state instead of $form_values. Give this a try:
$foo = $form_state['clicked_button']['#attributes']['title'];
I recommend using the Devel module while developing for Drupal. It is an extremely helpful tool during development, allowing you to see all the queries run when a page loads, stop a redirect to debug, and much more.

Most efficient way to have a 50 state drop down box in Drupal Forms

I know that one way is to have a table in database with all the states and then you would read it in your form. Is there any easier way in your opinion guys ?
I feel bad asking for something like this since it is so elementary however I would suppose something as simple like this would already be implemented in Drupal.
No need to hit the database. Build yourself a function that returns an array of the states.
$form['state'] = array(
'#type' => 'select',
'#options' => mymodule_states_list(),
'#title' => t('State'),
);
function mymodule_states_list() {
return array(
'AL' => 'Alabama',
'AK' => 'Alaska',
...
'WY' => 'Wyoming',
);
}
If you're building the form using Drupal's FormAPI you could just include the array of states in your module code since the names and abbreviations shouldn't be changing any time soon.
If you're trying to keep your code clean, you could save an array of states as a Drupal variable using variable_set() and then retrieve it using variable_get(). You really shouldn't need to bother with the database for that kind of thing.
That is one way to do it, sure. You can store a list of states as a variable, and call it.
$options = variable_get('mymodule_us_states', 0);
So long as it's an array. You could also have an internal function that returns the states.
Or store it in a flat file and read it in. Eg
Flat file =
$us_states_options = array(
'AL' => 'Alabama',
'AK' => 'Alaska',
//...etc
)
Function:
include_once(drupal_get_path('module', 'module_name') .'/us_states.inc');
All pretty ugly, but you at least can edit that file independently, and may work well if you have a larger list. Theres a million ways you could have the list included - using exec, fgetcsv/file, etc....
But I think ceejayoz solution is the nicest. I'd probably spin out those sorts of utility functions into a seperate include to keep it clean myself.
The Country codes API module also provides a Regions codes API module which include US states. With this module, you can get an array suitable for a Form API select element by calling regions_api_iso2_get_options_array('US').
Just remembered this question as I was experimenting with the Geonames module. Among tons of other features, Geonames includes the function "geonames_us_states()" which returns an array of U.S. states keyed by the states' two letter code.

Drupal form editing and conditional fields

I have the following in my template.php file:
function theme098_theme() {
return array(
'email_node_form' => array(
'arguments' => array('form' => NULL),
)
);
}
and...
function theme098_email_node_form($form) {
return drupal_render($form);
}
I've excluded the code where i actually modify the form and cut it down so that no modifications happen. Two problems occur:
The order of items (i.e. their weights) is messed up. The save button is at the top etc. Even if I try to edit the form by setting the weight, the save button STILL appears at the top.
The real problem: Conditional fields doesn't work. For some reason, I think this overwrites what other modules are supposed to do? I'm not sure
Can anyone shed some light?
3 things.
In this case you should probably use hook_form_alter(), if you want to change the order or change the form, instead of using a theme function to alter it. Keep presentation and logic separated.
When you define theme functions with hook theme, you should call them theme_xxx instead of themename_/ modulename_.
Modules (and possible themes) have a weight weight that determines the order they are run with functions like hook_*_alter. Look at the install file for the devel module to see how this is done.
One or more of these things should help you out.

Resources